ZXing.Net.Mobile: ZXing Scanner Camera is not activating in UWP and WP 8.1

I am using the example code from the ZXing page . Details are : Operating System : Windows 10 Pro N Visual Studio Enterprise 2015 Xamarin.Forms 2.3.1…114 ZXing.Net.Mobile 2.1.47 ZXing.Net.Mobile.Forms 2.1.4.7

I am using below code

btnScan.Clicked+=async(sender,e)=>{
var scanPage = new ZXingScannerPage ();
await Navigation.PushModalAsync (scanPage);
scanPage.OnScanResult += (result) => 
{
    scanPage.IsScanning = false; 
    Device.BeginInvokeOnMainThread (async () => 
    {
        await Navigation.PopModalAsync ();        
        await DisplayAlert("Scanned Barcode", result.Text, "OK");
    });
};

}

Also I have added the below line in the MainPage.xml file of the UWP project just before LoadApplication method call ZXing.Net.Mobile.Forms.WindowsUniversal.ZXingBarcodeImageViewRenderer.Init();

Also I have provided access to the WebCam from the application manifest.

When I run the application in Debug or Release mode in the emulator then it opens the scanner but small black/white /red /green square comes and computer webcam is not accessed. When I run it as windows 10 app then nothing comes, only blank screen comes. When I run it in the real windows 10 mobile, then scanner opens but camera doesn’t opens, the white screen with red line opens and camera doesn’t activates. The same thing happens with WIndows 8.1 also.

Any help would be deeply appreciated. I have wasted lot of time to explore the internet used almost every combination. If anyone have the solution please please provide me.

Note: The above code works fine for Android and I am able to scan the bar code and QR code.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

The only lines you actually need are: assembliesToInclude.Add(typeof(ZXing.Net.Mobile.Forms.WindowsUniversal.ZXingScannerViewRenderer).GetTypeInfo().Assembly); and assembliesToInclude.Add(typeof(ZXing.Net.Mobile.Forms.ZXingScannerPage).GetTypeInfo().Assembly);

However, if no camera is available, the screen just displays a white background, which causes the app to get rejected from the windows store.

@faceoffers28 , that did the trick! for anyone interested: just add these lines in your App.xaml.cs

// bugfix ZXing ref. https://github.com/Redth/ZXing.Net.Mobile/issues/409
List<Assembly> assembliesToInclude = new List<Assembly>();
assembliesToInclude.Add(typeof(ZXing.BarcodeReader).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Reader).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.BaseLuminanceSource).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Binarizer).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.BinaryBitmap).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Dimension).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.LuminanceSource).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.MultiFormatReader).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.PlanarYUVLuminanceSource).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Result).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.ResultPoint).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.RGBLuminanceSource).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.SupportClass).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Net.Mobile.Forms.ZXingBarcodeImageView).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Net.Mobile.Forms.ZXingScannerPage).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Net.Mobile.Forms.ZXingScannerView).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Net.Mobile.Forms.ZXingDefaultOverlay).GetTypeInfo().Assembly);    
assembliesToInclude.Add(typeof(ZXing.Mobile.MobileBarcodeScanner).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Mobile.MobileBarcodeScannerBase).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Mobile.MobileBarcodeScanningOptions).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Mobile.ScanPage).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Mobile.MobileBarcodeScannerBase).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Mobile.SoftwareBitmapLuminanceSource).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Mobile.ZXingScannerControl).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(ZXing.Net.Mobile.Forms.WindowsUniversal.ZXingScannerViewRenderer).GetTypeInfo().Assembly);

Xamarin.Forms.Forms.Init(e, assembliesToInclude);

@nicolabeghin have you added “webcam” permission?

Can confirm, the “assembliesToInclude” method fixed this issue for me.

In App.xaml.cs, procedure “OnLaunched”, replace Xamarin.Forms.Forms.Init(e); with

    // ZXing UWP fix for bug #409
    List<Assembly> assembliesToInclude = new List<Assembly>();
 
 
 
 assembliesToInclude.Add(typeof(ZXing.Net.Mobile.Forms.WindowsUniversal.ZXingScannerViewRenderer).GetTypeInfo().Assembly);
    assembliesToInclude.Add(typeof(ZXing.Net.Mobile.Forms.ZXingScannerPage).GetTypeInfo().Assembly);

    Xamarin.Forms.Forms.Init(e, assembliesToInclude);

You have to call different Init() functions on different platforms.

But the 2.1.47 NuGet packages don’t install the Winphone library to the win phone project. So no init is available.