2
votes

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.

2
I found this Realtime filter demo for Windows and Windows 8.1 it's exactly what i'm looking for in very easy to use package! BUT! I can't understand how i can control the camera with this CameraPreviewImageSource. I need to be able to manually set the focus etc. In the sample application you can't change focus or anything. - marksu_la

2 Answers

0
votes

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.

0
votes

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();