2

我正在尝试在 Windows 8 桌面应用程序 (WinForms .NET 4.5) 中使用 MediaCapture API。我可以使用 API 拍照,但照片显得很暗。此外,MediaCapture API 似乎没有按应有的方式自动触发相机闪光灯。

我已尝试根据 MSDN 文档将亮度、合同、白平衡和曝光设置为自动。这是相关的代码。

     _mediaCapture = new MediaCapture();

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

     // Find the highest resolution available
     ImageEncodingProperties resolutionMax = null;
     int max = 0;
     var resolutions = _mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo);
     foreach (IMediaEncodingProperties t in resolutions)
     {
        var properties = t as ImageEncodingProperties;
        if (properties != null)
        {
           var res = properties;
           if (res.Width * res.Height > max)
           {
              max = (int)(res.Width * res.Height);
              resolutionMax = res;
           }
        }
     }
     await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, resolutionMax);

     _mediaCapture.VideoDeviceController.Focus.TrySetAuto(true);

     _mediaCapture.VideoDeviceController.Brightness.TrySetAuto(true);

     _mediaCapture.VideoDeviceController.Contrast.TrySetAuto(true);

     _mediaCapture.VideoDeviceController.Exposure.TrySetAuto(true);

     _mediaCapture.VideoDeviceController.WhiteBalance.TrySetAuto(true);

     var imageProperties = ImageEncodingProperties.CreateJpeg();
     using (var fPhotoStream = new InMemoryRandomAccessStream())
     {
        // Take the photo and show it on the screen
        await _mediaCapture.CapturePhotoToStreamAsync(imageProperties, fPhotoStream);
        await fPhotoStream.FlushAsync();

        fPhotoStream.Seek(0);

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

        using (var byteStream = new MemoryStream(bytes))
        {
           return new Bitmap(byteStream);
        }
     }

任何指导将不胜感激。

编辑:我将此代码移植到 Metro 应用程序中,并且相机运行良好。我开始认为底层框架(Metro vs. Desktop)是罪魁祸首。

4

3 回答 3

2

迟到的回复,但以防万一将来对人们有所帮助:深色图片通常是由于缺少视频预览。相机驱动程序使用预览流来运行他们的 3A 算法(自动白平衡/对焦/曝光)。目前,让 MediaCapture 在桌面应用程序中进行预览有点挑战。一种方法是创建一个 D3DImage 并依靠一些本机互操作来调用 Media Foundation 和 DirectX。它不是微不足道的,但可以封装,因此 C# API 表面保持简单。这是一些代码示例: https ://github.com/mmaitre314/MediaCaptureWPF

于 2015-07-19T22:50:23.667 回答
1
await mc.InitializeAsync(new MediaCaptureInitializationSettings {
    PhotoCaptureSource = PhotoCaptureSource.VideoPreview }
);

是解决方案。

于 2013-11-19T11:46:10.647 回答
1

您可以尝试如下更改您的媒体设置,应该会更好:

settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.AudioAndVideo;
settings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.VideoPreview;
于 2013-10-04T09:43:00.927 回答