简体   繁体   中英

Capture WriteableBitmap from Windows Phone 8.1 WinRT Camera preview

My problem is this. How can I get a WriteableBitmap from the camera preview image in WP 8.1 WinRT?

I have done this in silverlight before, but i just can't seem to get it done in WinRT...

My goal is to scan for barcode in the camera preview image. I don't want to take a picture and scan for the barcode from there. I want to scan for the barcode from the camera preview.

I initialize my camera like this:

// 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
});

Then i setup the preview and start displaying it on the 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();

But now i would need to somehow get a snapshot of the camera image on PreviewElement so i can send it to ZXing for barcode decoding.

I have looked at countless number of samples, but most of them are for WP 8.0 or WP 8.1 Silverlight...

Any help would be appreciated.

To intercept the live stream you'll need to write a Media Foundation Transform (MFT) in C++ to plug into the rendering pipeline as a media extension .

The Media extensions sample demonstrates writing such a media extension. Also see Walkthrough: Creating a Windows Store app using WRL and Media Foundation

Once you have the frame you should be able to pass it to zxing for processing.

After looking into Lumia Imaging SDK... I found the sample for Real-time Filter Demo for Windows and Windows 8.1 this does exactly what i need in very simple code.

And to get control of the camera and do a focus for example i can just simply:

VideoDeviceController vdc = (VideoDeviceController)_cameraPreviewImageSource.VideoDeviceController;
await vdc.FocusControl.FocusAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM