在Windows 8桌面应用程序上使用MediaCapture [英] Use MediaCapture on a Windows 8 desktop app

查看:78
本文介绍了在Windows 8桌面应用程序上使用MediaCapture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows 8桌面应用程序上,我需要使用C#4.5中的相机拍照.

On a Windows 8 desktop app, I need to take a photo using the camera in C# 4.5.

我尝试使用CameraCaptureUI类,但在桌面应用程序上不可用.

I've tried to use the CameraCaptureUI class, but it is not available on a desktop app.

因此,我尝试使用MediaCapture类,该类可用于Metro应用程序或桌面应用程序.根据此处找到的示例,它效果很好: http://code.msdn.microsoft.com/windowsapps/media-capture-sample-adf87622/

So I try to use the MediaCapture class, which is available for Metro app or desktop app. It works great, based on the example found here : http://code.msdn.microsoft.com/windowsapps/media-capture-sample-adf87622/

var capture = new MediaCapture();
// Find the camera device id to use
string deviceId = "";
var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);
for (var i = 0; i < devices.Count; i++) {
     Console.WriteLine(devices[i]);
     deviceId = devices[i].Id;
}

// init the settings of the capture
var settings = new MediaCaptureInitializationSettings();
settings.AudioDeviceId = "";
settings.VideoDeviceId = deviceId;
settings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.Photo;
settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Video;
await capture.InitializeAsync(settings);

// Find the highest resolution available
VideoEncodingProperties resolutionMax = null;
int max = 0;
var resolutions = capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo);
for (var i = 0; i < resolutions.Count; i++) {
     VideoEncodingProperties res = (VideoEncodingProperties)resolutions[i];
     Console.WriteLine("resolution : " + res.Width + "x" + res.Height);
     if (res.Width * res.Height > max) {
          max = (int)(res.Width * res.Height);
          resolutionMax = res;
     }
}
await capture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, resolutionMax);

ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
var fPhotoStream = new InMemoryRandomAccessStream();

// THE 2 LINES I NEED TO ADD
// captureElement.Source = capture;
// await capture.StartPreviewAsync();

// Take the photo and show it on the screen
await capture.CapturePhotoToStreamAsync(imageProperties, fPhotoStream);
await fPhotoStream.FlushAsync();
fPhotoStream.Seek(0);

byte[] bytes = new byte[fPhotoStream.Size];
await fPhotoStream.ReadAsync(bytes.AsBuffer(), (uint)fPhotoStream.Size, InputStreamOptions.None);

BitmapImage bitmapImage = new BitmapImage();
MemoryStream byteStream = new MemoryStream(bytes);
bitmapImage.BeginInit();
bitmapImage.StreamSource = byteStream;
bitmapImage.EndInit();
image.Source = bitmapImage;

我可以使用相机拍照,但是在拍照之前无法显示预览.为了能够显示预览,我必须使用组件CaptureElement,例如,使用以下代码:

I can take a photo using the camera, but I'm unable to show a preview before taking the photo. To be able to show the preview, I have to use the component CaptureElement, for example with the following code :

captureElement.Source = mediaCapture;
await mediaCapture.startPreviewAsync();

不幸的是,我不能在非商店应用程序上使用CaptureElement.我可以在WPF或WinForm应用程序中使用另一个组件来显示摄像机的预览吗?

Unfortunately, I cannot use a CaptureElement on a non store app. Is there another component that I can use in a WPF or WinForm app, to be able to show the preview of the camera ?

推荐答案

最新答复,但以防将来对人们有所帮助:可以通过D3DImage和某些本机互操作来预览WPF中的MediaCapture(创建自定义Media Foundation Sink),抓取DirectX 11纹理,然后将其转换为DirectX 9).这是一些代码,但是可以封装,因此从C#调用仍然很简单.这是一些代码示例: https://github.com/mmaitre314/MediaCaptureWPF

Late reply, but in case that helps folks in the future: previewing MediaCapture in WPF can be done via D3DImage and some native interop (create a custom Media Foundation Sink, grab DirectX 11 textures, convert them to DirectX 9). It is a bit of code but can be encapsulated so it is still simple to call from C#. Here is some code sample: https://github.com/mmaitre314/MediaCaptureWPF

这篇关于在Windows 8桌面应用程序上使用MediaCapture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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