ZXing Barcode Scanner WinRT can't start StartPreviewAsync()

muetzenflo

I run the following lines to get a preview of my webcam:

...
public MediaCapture VideoCaptureSource { get; set; }
var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

if (cameras.Count == 1)
{
  settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[0].Id };
}
else
{
  settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[1].Id };
}

await _mediaCapture.InitializeAsync(settings);
VideoCaptureSource = _mediaCapture;
await VideoCaptureSource.StartPreviewAsync();
...

Here's my XAML:

<Canvas Width="640" Height="360">
  <CaptureElement Source="{Binding VideoCaptureSource}" Visibility="{Binding VideoCaptureVisibility}" Width="640" Height="360" />
  <Image Source="{Binding CaptureImageSource}" Visibility="{Binding CaptureImageVisibility, FallbackValue=Collapsed}" Width="640" Height="360" />
</Canvas>

Everything works fine until I call the StartPreviewAsyn()-Method. Unfortunately, I only get a not-so-useful error message: "An unexpected error has occurred in the operation requested." I tried to run the Code on my local machine as well as on the simulator. Has anyone an idea what I do wrong?

Environment is Windows 8.1, Caliburn.Micro, ZXing.Net 0.12.0.0, VS 2013

Christoph

I recently ran into the same problem. It seems that binding the video source is quite tricky, because you need to initialise, set and start the preview stream in the right order. I fixed the issue using an attached property:

/// <summary>
/// This utility class provides an attached property that enables us to
/// bind the source of a <see cref="CaptureElement"/>.
/// </summary>
public static class VideoSourceBinding {

    public static MediaCapture GetVideoSource(DependencyObject obj) {
        return (MediaCapture) obj.GetValue(VideoSourceProperty);
    }

    public static void SetVideoSource(DependencyObject obj,
            MediaCapture value) {
        obj.SetValue(VideoSourceProperty, value);
    }

    public static readonly DependencyProperty VideoSourceProperty
        = DependencyProperty.RegisterAttached("VideoSource",
        typeof(MediaCapture),
        typeof(VideoSourceBinding),
        new PropertyMetadata(null, onVideoSourcePropertyChanged));

    private static async void onVideoSourcePropertyChanged(
            DependencyObject d, DependencyPropertyChangedEventArgs e) {
        Debug.Assert(d is CaptureElement);
        Debug.Assert(e.Property == VideoSourceBinding.VideoSourceProperty);

        CaptureElement preview = d as CaptureElement;

        if (d != null) {
            if (preview.Source != null) {
                // If another camera was attached before, stop it.
                await preview.Source.StopPreviewAsync();
            }

            try {
                preview.Source = (MediaCapture) e.NewValue;
            } catch {
                // The property change occurred before the the video source
                // was properly initialised. In this case, we ignore the
                // change and wait for the source to fire the event again
                // once the initialisation was completed.
                // Important: The source must actually change in order for
                // the event to be fired (the attached property will detect
                // if the same object was re-set) and ignore this.
                preview.Source = null;
            }

            if (preview.Source != null) {
                // Start the preview stream.
                await preview.Source.StartPreviewAsync();
            }
        }
    }
}

The video source is then bound like this:

<CaptureElement u:VideoSourceBinding.VideoSource="{Binding VideoCapture}" />

Note that the order in which you do things in the view model is important:

public class ViewModel : Screen {
    // Note: The sample uses a Caliburn Micro base class - this is, however, not
    // a requirement.

    public MediaCapture VideoCapture {
        get;
        private set;
    }

    /// <summary>
    /// Starts the video preview.
    /// </summary>
    /// <remarks>
    /// Call this method whenever it is necessary to (re-) start the preview, eg
    /// if the page is activated or if the settings have changed.
    /// </remarks>
    private async void startVideo() {
        var captureSettings = new MediaCaptureInitializationSettings() {
            StreamingCaptureMode = StreamingCaptureMode.Video
        };
        // Set a NEW MediaCapture! Do not re-use the old one, because the property 
        // change event of the attached property will not fire otherwise.
        this.VideoCapture = new MediaCapture();
        await this.videoCapture.InitializeAsync(captureSettings);
        // Notify the UI about a new video source AFTER the initialisation completed. It 
        // is important to await the initialisation here.
        this.NotifyOfPropertyChange(() => this.VideoCapture);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ZXing Barcode Scanner WinRT can't start StartPreviewAsync()

From Dev

ZXing barcode scanner for .NET

From Dev

Unable to get result from ZXing barcode scanner

From Dev

ZXing barcode scanner in custom layout in fragment

From Dev

Android Zxing Barcode Scanner is not scanning correctly

From Dev

Integrating Zxing Barcode scanner to my android app

From Dev

Core.jar not in Zxing Core folder? - Zxing Barcode Scanner

From Dev

Can't use Barcode scanner in Cordova (plugin is installed).

From Dev

Why 1000 CCD Barcode Contact Scanner can't recognize zero?

From Dev

With Zxing retiring for iOS, which barcode-scanner to switch to?

From Dev

ZXing Barcode Scanner Intent: set DecodeHintType.ASSUME_GS1

From Dev

How to Customize the capture screen border of zxing barcode scanner from ViewFinder

From Dev

creating barcode scanner with zxing source code and core java

From Dev

How to start Scanner using the Flashlight with ZXing?

From Dev

How to Develop a Desktop Application using C# which can Utilize USB Barcode Scanner. How to start

From Dev

How to integrate Zxing Barcode Scanner without installing the actual zxing app (cannot resolve symbol: .android.CaptureActivity)?

From Dev

Phonegap barcode scanner: couldn't make it run

From Dev

Phonegap barcode scanner: couldn't make it run

From Dev

CaptureActivity from Zxing library didn't return when barcode is scanned

From Dev

Implement Realtime Barcode Scanner on Windows Phone 8.1 Runtime using ZXing and MFT

From Dev

Error using ZXing barcode scanner via intent from my custom camera view

From Dev

Unable to start actvity componentinfo - android app barcode scanner

From Dev

How can I set many formats for barcode scanner or exclude couple?

From Dev

how can i get product information from barcode scanner device

From Dev

Can't receive Broadcasts in WinRT

From Dev

Barcode Scanner for Rails App

From Dev

Barcode scanner for PhoneGap/cordova

From Dev

Honeywell android Barcode scanner

From Dev

Winforms keypress and barcode scanner

Related Related

  1. 1

    ZXing Barcode Scanner WinRT can't start StartPreviewAsync()

  2. 2

    ZXing barcode scanner for .NET

  3. 3

    Unable to get result from ZXing barcode scanner

  4. 4

    ZXing barcode scanner in custom layout in fragment

  5. 5

    Android Zxing Barcode Scanner is not scanning correctly

  6. 6

    Integrating Zxing Barcode scanner to my android app

  7. 7

    Core.jar not in Zxing Core folder? - Zxing Barcode Scanner

  8. 8

    Can't use Barcode scanner in Cordova (plugin is installed).

  9. 9

    Why 1000 CCD Barcode Contact Scanner can't recognize zero?

  10. 10

    With Zxing retiring for iOS, which barcode-scanner to switch to?

  11. 11

    ZXing Barcode Scanner Intent: set DecodeHintType.ASSUME_GS1

  12. 12

    How to Customize the capture screen border of zxing barcode scanner from ViewFinder

  13. 13

    creating barcode scanner with zxing source code and core java

  14. 14

    How to start Scanner using the Flashlight with ZXing?

  15. 15

    How to Develop a Desktop Application using C# which can Utilize USB Barcode Scanner. How to start

  16. 16

    How to integrate Zxing Barcode Scanner without installing the actual zxing app (cannot resolve symbol: .android.CaptureActivity)?

  17. 17

    Phonegap barcode scanner: couldn't make it run

  18. 18

    Phonegap barcode scanner: couldn't make it run

  19. 19

    CaptureActivity from Zxing library didn't return when barcode is scanned

  20. 20

    Implement Realtime Barcode Scanner on Windows Phone 8.1 Runtime using ZXing and MFT

  21. 21

    Error using ZXing barcode scanner via intent from my custom camera view

  22. 22

    Unable to start actvity componentinfo - android app barcode scanner

  23. 23

    How can I set many formats for barcode scanner or exclude couple?

  24. 24

    how can i get product information from barcode scanner device

  25. 25

    Can't receive Broadcasts in WinRT

  26. 26

    Barcode Scanner for Rails App

  27. 27

    Barcode scanner for PhoneGap/cordova

  28. 28

    Honeywell android Barcode scanner

  29. 29

    Winforms keypress and barcode scanner

HotTag

Archive