c# - 使用 MediaCapture 从 CaptureElement 以自定义分辨率拍摄照片

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

最佳答案

所以我终于想出了如何解决这个问题并摆脱可怕的“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!

关于c# - 使用 MediaCapture 从 CaptureElement 以自定义分辨率拍摄照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15412772/

相关文章:

c# - ProxyCreationEnabled = false 是否等于 Entity Framework 中的 AsNoTracking() ?

jquery - JCrop 如何通过将图像拖动到 div 后面来裁剪图像(如 facebook 封面)

javascript - 如何访问Windows应用商店应用程序包中的文件

c# - 使用正则表达式删除文本部分 C#

c# - MethodBody.LocalVariables 计数令人困惑

python - 如何将 JPEG 图像插入 python Tkinter 窗口?

c# - 使用 Windows 帐户授权

android - Xamarin——支持 9 补丁?

c# - 正则表达式匹配 C# 中的单独整数列表

java - 如何使用java在旋转时保留图像的原始颜色