【问题标题】:Use MediaCapture on a Windows 8 desktop app在 Windows 8 桌面应用程序上使用 MediaCapture
【发布时间】:2013-01-25 15:56:23
【问题描述】:

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

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

所以我尝试使用适用于 Metro 应用程序或桌面应用程序的 MediaCapture 类。根据此处找到的示例,它工作得很好: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 应用程序中使用另一个组件来显示相机的预览吗?

【问题讨论】:

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


    【解决方案1】:

    迟到的回复,但以防将来对人们有所帮助:在 WPF 中预览 MediaCapture 可以通过 D3DImage 和一些本机互操作完成(创建自定义媒体基础接收器,获取 DirectX 11 纹理,将它们转换为 DirectX 9)。它是一些代码,但可以封装,因此从 C# 调用它仍然很简单。这是一些代码示例: https://github.com/mmaitre314/MediaCaptureWPF

    【讨论】:

      【解决方案2】:

      拍完照片后,我会做以下事情:

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

      预览 XAML 控件是

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

      【讨论】:

        【解决方案3】:

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

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

        我已在 Windows 窗体应用程序中托管的 wpf 控件上使用它并且没有任何问题。只是我不得不继续使用 MediaCapture 来拍摄照片,而且我还遇到了平板电脑上的照片太暗的问题(在电脑上还可以)。似乎调用 capture.CapturePhotoToStreamAsync 会错误地拍摄您在预览中看到的照片。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多