拍照与CaptureElement自定义的分辨率与MediaCapture [英] Take photo with custom resolution from CaptureElement with MediaCapture

查看:1917
本文介绍了拍照与CaptureElement自定义的分辨率与MediaCapture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显示摄像机饲料使用CaptureElement即时通讯我的Windows Store应用。现在我想,当用户点击显示,这是我得到了用下面的代码工作,拍摄照片作为流。不幸的是图像只返回的分辨率为640×360,但是摄像头(表面RT)可拍摄图像与1280×800,我想做哪个。



我尝试设置

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



但没有奏效。所以我怎么更改分辨率?



 专用异步无效captureElement_Tapped(对象发件人,TappedRoutedEventArgs E)
{
变种encodingProperties = ImageEncodingProperties.CreateJpeg();
//encodingProperties.Height = 800;
//encodingProperties.Width = 1280;
WriteableBitmap的WBMP;使用

(VAR的ImageStream =新InMemoryRandomAccessStream())
{
等待captureMgr.CapturePhotoToStreamAsync(encodingProperties,的ImageStream);
等待imageStream.FlushAsync();
imageStream.Seek(0);
WBMP =等待新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装置;

保护覆盖无效的OnNavigatedTo(NavigationEventArgs E)
{
设备=等待DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
this.mediaCapture =新MediaCapture();
如果(devices.Count()&0)
{
等待this.mediaCapture.InitializeAsync(新MediaCaptureInitializationSettings {VideoDeviceId = devices.ElementAt(1).ID,PhotoCaptureSource = Windows.Media .Capture.PhotoCaptureSource.VideoPreview});
SetResolution();
}
}


//这是如何设置的分辨率
公共异步无效SetResolution()
{
System.Collections.Generic.IReadOnlyList< IMediaEncodingProperties>资源;
解析度= this.mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview);
UINT maxResolution = 0;
INT indexMaxResolution = 0;

如果(res.Count> = 1)
{
的for(int i = 0; I< res.Count;我++)
{
VideoEncodingProperties VP =(VideoEncodingProperties)RES [I]

如果(vp.Width> maxResolution)
{
indexMaxResolution = I;
maxResolution = vp.Width;
的Debug.WriteLine(分辨率:+ vp.Width);
}
}
等待this.mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview,水库[indexMaxResolution]);
}
}



虽然拍照,请确保您始终与合作。 VideoPreview,不.Photo!


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

解决方案

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!

这篇关于拍照与CaptureElement自定义的分辨率与MediaCapture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆