我正在尝试在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)应该受到责备。
答案 0 :(得分:2)
延迟回复,但如果将来有助于人们:黑暗图片通常是由于缺乏视频预览。相机驱动程序使用预览流来运行其3A算法(自动白平衡/聚焦/曝光)。目前让MediaCapture在桌面应用程序中预览有点挑战性。一种方法是创建一个D3DImage并依靠一些本地互操作来调用Media Foundation和DirectX。它不是微不足道的,但可以封装,因此C#API表面仍然很简单。这是一些代码示例: https://github.com/mmaitre314/MediaCaptureWPF
答案 1 :(得分:1)
您可以尝试按以下方式更改媒体设置,它应该更好:
settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.AudioAndVideo;
settings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.VideoPreview;
答案 2 :(得分:1)
await mc.InitializeAsync(new MediaCaptureInitializationSettings {
PhotoCaptureSource = PhotoCaptureSource.VideoPreview }
);
是解决方案。