使用带有MediaCapture的CaptureElement自定义分辨率拍照

时间:2013-03-14 15:01:35

标签: c# image windows-store-apps

我正在使用CaptureElement在我的Windows应用商店中显示相机供稿。现在,当用户点击显示器时,我想将照片捕获为流,我使用下面的代码工作。不幸的是,返回的图像只有640 x 360的分辨率,但是相机(Surface RT)可以拍摄1280x800的图像,我想这样做。

我尝试过设置

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

但这不起作用。那么我该如何改变分辨率?

   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;
    }

1 个答案:

答案 0 :(得分:15)

所以我终于想出了如何通过这个来摆脱可怕的“HRESULT:0xC00D36B4”错误,部分归功于此处的代码: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/751b8d83-e646-4ce9-b019-f3c8599e18e0

我做了一些调整,这就是我在这里重新发布代码的原因

    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]);
        }
    }

虽然拍照,但请确保您始终使用.VideoPreview,而不是.Photo!

相关问题