Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take photo with custom resolution from CaptureElement with MediaCapture

I'm displaying the camera feed im my Windows Store App using the CaptureElement. Now I'd like to capture a photo as a stream when the user taps the display, which I got working using the code below. Unfortunatly the image returned only has a resolution of 640 x 360, however the camera (Surface RT) can take images with 1280x800, which I'd like to do.

I tried setting

        encodingProperties.Height = 800;
        encodingProperties.Width = 1280;

but that didn't work. So how do I change the resolution?

   private async void captureElement_Tapped(object sender, TappedRoutedEventArgs e)
    {
        var encodingProperties = ImageEncodingProperties.CreateJpeg();
        //encodingProperties.Height = 800;
        //encodingProperties.Width = 1280;
        WriteableBitmap wbmp;

        using (var imageStream = new InMemoryRandomAccessStream())
        {
            await captureMgr.CapturePhotoToStreamAsync(encodingProperties, imageStream);
            await imageStream.FlushAsync();
            imageStream.Seek(0);
            wbmp = await new WriteableBitmap(1, 1).FromStream(imageStream);
        }

        capturedImage.Source = wbmp;
    }
like image 765
Thomas Avatar asked Mar 14 '13 15:03

Thomas


1 Answers

So I finally figured out how to come by this and also get rid of the dreaded "HRESULT: 0xC00D36B4" error, partly thanks to the code found here: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/751b8d83-e646-4ce9-b019-f3c8599e18e0

I made some adjustments, that's why I repost my code here

    MediaCapture mediaCapture;
    DeviceInformationCollection devices;

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
        this.mediaCapture = new MediaCapture();
        if (devices.Count() > 0)
        {
            await this.mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = devices.ElementAt(1).Id, PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.VideoPreview });
            SetResolution();
        }  
    }


    //This is how you can set your resolution
    public async void SetResolution()
    {
        System.Collections.Generic.IReadOnlyList<IMediaEncodingProperties> res;
        res = this.mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview);
        uint maxResolution = 0;
        int indexMaxResolution = 0;

        if (res.Count >= 1)
        {
            for (int i = 0; i < res.Count; i++)
            {
                VideoEncodingProperties vp = (VideoEncodingProperties)res[i];

                if (vp.Width > maxResolution)
                {
                    indexMaxResolution = i;
                    maxResolution = vp.Width;
                    Debug.WriteLine("Resolution: " + vp.Width);
                }
            }
            await this.mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, res[indexMaxResolution]);
        }
    }

Though taking photos, make sure you always work with .VideoPreview, not .Photo!

like image 182
Thomas Avatar answered Sep 18 '22 13:09

Thomas