在Windows 8桌面应用上使用MediaCapture

时间:2013-01-25 15:56:23

标签: c# windows-8 webcam winrt-xaml windows-store-apps

在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 < 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,例如使用以下代码:

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

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

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

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

预览XAML控件是

<Image x:Name="preview1" Margin="850,90,102,362" />

答案 2 :(得分:0)

  

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

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

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

相关问题