Windows 8デスクトップアプリケーション(WinForms .NET 4.5)内でMediaCaptureAPIを使用しようとしています。APIを使用して写真を撮ることはできますが、写真が非常に暗くなります。また、MediaCaptureAPIがカメラのフラッシュを自動的にトリガーしているようには見えません。
私は、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とDesktop)のせいであると私は考え始めています。