我正在尝试在 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)是罪魁祸首。