Shubham Shrivastava
Shubham Shrivastava

Reputation: 217

How to open and auto capture camera using MediaCapture class

we are trying to automatically capture an image from the webcam using MediaCapture class. We are trying to create an application which opens the camera, waits for a moment and captures the image in front of it without someone to tap the screen to capture. we tried using LowLagPhotoCapture class but does not work as desired. Sample code -

async private void InitMediaCapture()
{
    MediaCapture _mediaCapture = new MediaCapture();
        await _mediaCapture.InitializeAsync();
        _displayRequest.RequestActive();                        
        PreviewControlCheckIn.Source = _mediaCapture;
        await _mediaCapture.StartPreviewAsync();
        await Task.delay(500);
    CaptureImage();
}
async private void CaptureImage()
{
    storeFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync   ("TestPhoto.jpg",CreationCollisionOption.GenerateUniqueName);
        ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
        await _mediaCapture.CapturePhotoToStorageFileAsync(imgFormat, storeFile);
    await _mediaCapture.StopPreviewAsync();
}

Any info would be great, thanks in advance for the help.

Upvotes: 1

Views: 2681

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

I have completed your provided code and achieved your requirement. Please refer to the following code. Please note that you should declare camera and the microphone capabilities in your Universal Windows Platform (UWP) app's package manifest to access certain API.

async private void InitMediaCapture()
{
    _mediaCapture = new MediaCapture();
    var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
    var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };
    await _mediaCapture.InitializeAsync(settings);
    _displayRequest.RequestActive();
    PreviewControl.Source = _mediaCapture;
    await _mediaCapture.StartPreviewAsync();

    var picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
    _captureFolder = picturesLibrary.SaveFolder ?? ApplicationData.Current.LocalFolder;

    await Task.Delay(500);
    CaptureImage();
}

 async private void CaptureImage()
 {
     var storeFile = await _captureFolder.CreateFileAsync("PreviewFrame.jpg", CreationCollisionOption.GenerateUniqueName);
     ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
     await _mediaCapture.CapturePhotoToStorageFileAsync(imgFormat, storeFile);
     await _mediaCapture.StopPreviewAsync();
 }
 private static async Task<DeviceInformation> FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel)
 {
     // Get available devices for capturing pictures
     var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

     // Get the desired camera by panel
     DeviceInformation desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredPanel);

     // If there is no device mounted on the desired panel, return the first device found
     return desiredDevice ?? allVideoDevices.FirstOrDefault();
 }

The photo will be saved to Pictures library. And I have upload the code sample to github. Please check!

Upvotes: 4

Related Questions