Csharp/C#教程:在Windows 8桌面应用上使用MediaCapture分享


在Windows 8桌面应用上使用MediaCapture

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

我曾尝试使用CameraCaptureUI类,但它不适用于桌面应用程序。

所以我尝试使用MediaCapture类,它可用于Metro应用程序或桌面应用程序。 它的工作原理非常好,基于此处的示例: 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  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,例如使用以下代码:

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

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

延迟回复,但如果将来有助于人们:在WPF中预览MediaCapture可以通过D3DImage和一些原生互操作(创建自定义Media Foundation Sink,抓取DirectX 11纹理,将它们转换为DirectX 9)来完成。 它是一些代码,但可以封装,因此从C#调用仍然很简单。 以下是一些代码示例: https : //github.com/mmaitre314/MediaCaptureWPF

拍摄照片后,我会执行以下操作:

 _ms.Seek(0); var _bmp = new BitmapImage(); _bmp.SetSource(_ms); preview1.Source = _bmp; 

预览XAML控件是

  

不幸的是,我无法在非商店应用上使用CaptureElement。 是否有我可以在WPF或WinForm应用程序中使用的另一个组件,以便能够显示相机的预览?

对于在非WinRT应用程序中使用类似于CaptureElement的解决方案,我发现了这个: http ://wpfcap.codeplex.com/

我已经在Windows窗体应用程序中托管的wpf控件上使用它并且没有问题。 只有我必须继续使用MediaCapture来捕捉照片,我也有平板电脑上的图片太暗的问题(在PC上可以)。 似乎调用capture.CapturePhotoToStreamAsync同步获取预览中看到的照片。

上述就是C#学习教程:在Windows 8桌面应用上使用MediaCapture分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1043498.html

(0)
上一篇 2022年1月31日
下一篇 2022年1月31日

精彩推荐