Search code examples
iosxamarin.formszxing

ZXing barcode scanner in Xamarin Forms on iOS doesn't read barcodes


I'm working on a Xamarin Forms PCL project targeting Android and iOS that needs some basic barcode scanning functionality.

I got things up and running with the forms version of the ZXing.Net.Mobile package following this tutorial and referencing this guide and it works great on Android. However, on iOS the camera comes up but the OnScanResult event is never fired.

I've tested it on several iOS devices running several different versions (anywhere from 8.0 - 10.3) and fiddled with everything I can think of, referencing their sample projects as well. ZXing.Net.Mobile and ZXing.Net.Mobile.Forms nuget packages were installed in all projects.

Here's my scanner code, triggered from a button press on my main page:

var scanPage = new ZXingScannerPage();

scanPage.OnScanResult += (result) =>
{
    scanPage.IsScanning = false;

    Device.BeginInvokeOnMainThread(async () =>
    {
        await Navigation.PopAsync();
        await DisplayAlert("Scanned Barcode", result.Text, "OK");
    });
};

await Navigation.PushAsync(scanPage);

and AppDelegate.cs:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();

    global::ZXing.Net.Mobile.Forms.iOS.Platform.Init();

    LoadApplication(new App());

    return base.FinishedLaunching(app, options);
}

After two days of fighting with this, I feel like I'm at my wit's end. Has anyone else encountered similar problems? Thanks for any guidance!


Edit 1

Here's the device log output (Message column only) when clicking the button to trigger the above code. No log items are added while the camera is open and pointed at various barcodes. I don't see anything suspicious here, but maybe someone else does?

  • PERF: Alloc AVCaptureVideoPreviewLayer took 14.14 ms.
  • AppleH4CamIn::power_on_hardware
  • AppleH4CamIn::ISP_SelectBestMIPIFrequencyIndex_gated - channel: 0, index: 0
  • StartScanning
  • AppleH4CamIn::setPowerStateGated: 1
  • ZXingScannerView.Setup() took 0.167 ms.
  • ASL Sender Statistics
  • AppleH4CamIn::ISP_FlushInactiveDARTMappings: 0x00000000
  • AppleH4CamIn::ISP_SelectBestMIPIFrequencyIndex_gated - channel: 0, index: 0

Edit 2

I ran the sample Xamarin Forms PCL from the github repo and am having the same problems on iOS with the camera opening fine, but no barcode ever read. Therefore I'm thinking the problem must be with my Visual Studio and/or Xamarin setup. I'm running Visual Studio 2015 Community on the stable Xamarin channel.


Solution

  • I finally got it working with this project as a starting point. It's just a basic barcode scanning implementation using ZXing, but for whatever reason I could finally read barcodes on iOS.

    It's probably a version-related problem, but I couldn't narrow it down.

    Edit

    I posted this same question in the Xamarin Forums: https://forums.xamarin.com/discussion/comment/272209#Comment_272209

    It seems the answer was downgrading to ZXing version 2.1.47. I haven't done any work with the library for a few months so I haven't tested new versions.