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

查看:147
本文介绍了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.

我尝试将亮度,合同,WhiteBalance和曝光设置为自动每个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应用中,并且相机的工作效果非常好。我开始认为应该归咎于基础框架(地铁与台式机)。

推荐答案

最新答复,但以防将来对人们有所帮助:深色图片通常是由于缺少视频预览而导致的。相机驱动程序使用预览流来运行其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天全站免登陆