我的问题是这个。如何在WP 8.1 WinRT中从相机预览图像中获取可写位图?
我以前在silverlight中做过这件事,但我似乎无法在WinRT中完成。。。
我的目标是扫描相机预览图像中的条形码。我不想在那里拍照和扫描条形码。我想从相机预览中扫描条形码。
我这样初始化我的相机:
// First get the rear camera
var _rearCamera = await GetCameraDeviceInfoAsync(Windows.Devices.Enumeration.Panel.Back);
_mediaCapture = new MediaCapture();
// Set up the initialization settings to use rear camera
await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = string.Empty,
VideoDeviceId = _rearCamera.Id
});
然后我设置预览并开始在previewElement上显示它。
// Find the supported preview size that's closest to the desired size
var availableMediaStreamProperties =
_mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview)
.OfType<VideoEncodingProperties>()
.Where(p => p != null && !String.IsNullOrEmpty(p.Subtype) && supportedVideoFormats.Contains(p.Subtype.ToLower()))
.OrderByDescending(p => p.Width)
.ToList();
_previewFormat = availableMediaStreamProperties.FirstOrDefault();
// Start Preview stream
await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, _previewFormat);
_mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
previewElement.Source = _mediaCapture;
await _mediaCapture.StartPreviewAsync();
但现在我需要在PreviewElement上获得相机图像的快照,这样我就可以将其发送到ZXing进行条形码解码。
我看过无数的示例,但大多数都是针对WP 8.0或WP 8.1 Silverlight的。。。
如有任何帮助,我们将不胜感激。
要截取实时流,您需要用C++编写一个媒体基础转换(MFT),以作为媒体扩展插入渲染管道。
媒体扩展示例演示如何编写这样的媒体扩展。另请参阅演练:使用WRL和Media Foundation 创建Windows应用商店应用程序
一旦你有了框架,你应该能够把它传递给zxing进行处理。
查看Lumia Imaging SDK后。。。我找到了适用于Windows和Windows 8.1的实时过滤器演示的示例,这正是我在非常简单的代码中所需要的。
例如,为了控制相机并进行对焦,我可以简单地:
VideoDeviceController vdc = (VideoDeviceController)_cameraPreviewImageSource.VideoDeviceController;
await vdc.FocusControl.FocusAsync();