locked
Set MediaCapture device back camera and resolution

    Question

  • I'm struggeling with MediaCapture to capture Photo's.

    I can take a photo, but can't set the maximum resolution and the desired camera.

    I found this code:

    private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
    {
        // get available devices for capturing pictures
        DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
            .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);
     
        if (deviceID != null) return deviceID;
        else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desiredCamera));
    }
     
    async private void InitCameraBtn_Click(object sender, RoutedEventArgs e)
    {
        var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
        captureManager = new MediaCapture();
     
        await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
            {
                StreamingCaptureMode = StreamingCaptureMode.Video,
                PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
                AudioDeviceId = string.Empty,
                VideoDeviceId = cameraID.Id
            });
     
        var maxResolution = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Width > (i2 as VideoEncodingProperties).Width ? i1 : i2);
        await captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, maxResolution);
    }
    
    

    In this code Visual Studio underlines .FirstOrDefault and .Aggregate

    When I visit MSDN, it looks like the mentioned .FirstOrDefault and .Aggregate are for Javascript only and not for c#. Is that correct? And so, how do I get the code working in c#?

    Monday, November 10, 2014 8:56 PM

Answers

  • Hello,

    I would recommend that you simplify the code greatly. Here is some pseudo code that might help:

    DeviceInformationCollection dic = await DeviceInformation.FindAllAsync
    
    foreach(DeviceInformation di in dic)
        if(di.EnclosureLocation != null && di.EnclosureLocation.Panel == desiredCamera;
    

    Something like that should work.

    I hope this helps,

    James


    Windows SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    • Marked as answer by DennisBakker71 Wednesday, November 12, 2014 6:43 PM
    Tuesday, November 11, 2014 2:12 AM
    Moderator