MediaCapture Windows 8 桌面 - 照片较暗 [英] MediaCapture Windows 8 Desktop - Photo is Dark

查看:29
本文介绍了MediaCapture Windows 8 桌面 - 照片较暗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Windows 8 桌面应用程序 (WinForms .NET 4.5) 中使用 MediaCapture API.我可以使用 API 拍摄照片,但照片非常暗.此外,MediaCapture API 似乎没有像它应该的那样自动触发相机闪光灯.

I am attempting to use the MediaCapture API within a Windows 8 Desktop Application (WinForms .NET 4.5). I am able to take the photo using the API, but the photo comes out very dark. Also, it does not appear that the MediaCapture API is triggering the camera flash automatically as it should.

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

I have tried to set the Brightness, Contracts, WhiteBalance and exposure to automatic per MSDN documentation. Here is the relevant code.

     _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)是罪魁祸首.

推荐答案

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

Late reply, but in case that helps folks in the future: dark pictures are often due to the lack of video preview. Camera drivers use the preview stream to run their 3A algorithms (auto-whitebalance/focus/exposure). At the moment getting MediaCapture to preview in Desktop apps is a bit challenging. One way to do it is to create a D3DImage and rely on some native interop to call Media Foundation and DirectX. It is not trivial but can be encapsulated so the C# API surface remains simple. Here is some code sample: https://github.com/mmaitre314/MediaCaptureWPF

这篇关于MediaCapture Windows 8 桌面 - 照片较暗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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