3

I am using Xamarin forms to write an iOS app and using the ZXing library to scan barcodes. I am trying to read a driver's license (PDF417) barcode, but the library is not able to recognize that barcode.

If I include UPC or other barcodes in the PossibleFormats, they are scanned correctly.

I am also certain the barcode I am trying to read is PDF417 barcode because Scandit is able to recognize it correctly while using only PDF417 barcode.

Here is the code I am using. What do I need to change so that the PDF417 barcode is recognized correctly?

async void Handle_Clicked (object sender, System.EventArgs e)
    {
        MobileBarcodeScanningOptions options = new MobileBarcodeScanningOptions ();
        options.PossibleFormats = new List<ZXing.BarcodeFormat> () {
            ZXing.BarcodeFormat.PDF_417
        };
        options.TryHarder = true;

        var scanPage = new ZXingScannerPage (options);


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

            // Pop the page and show the result
            Device.BeginInvokeOnMainThread (async () => {
                await Navigation.PopAsync ();
                await DisplayAlert ("Scanned Barcode", result.Text, "OK");
            });
        };

        // Navigate to our scanner page
        await Navigation.PushAsync (scanPage);
    }
xdev
  • 659
  • 11
  • 23
  • Does the version of ZXing you're using support PDF417? – Jason Mar 07 '17 at 20:08
  • Also, have you ran the barcode through any other scanners and got a result? Some barcodes can be nasty and contain garbage rows for example in the end. It's worth noting that PDF 417 support on ZXing is still in beta, so it might not be able to recognize even valid codes if the image is not good enough. – Timo Salomäki Mar 07 '17 at 20:27
  • The barcode is pretty clear and as I said earlier, Scandit can recognize it correctly. – xdev Mar 07 '17 at 21:29

1 Answers1

8

I ran into this exact same issue a few days ago and fixed it with the following. In the MobileBarcodeScanningOptions class there's a property of type CameraResolutionSelectorDelegate called CameraResolutionSelector. You can set this to return a higher camera resolution from a list of available resolutions. So my instantiation of MobileBarcodeScanningOptions looks like this:

var options = new MobileBarcodeScanningOptions {
            TryHarder = true,
            CameraResolutionSelector = HandleCameraResolutionSelectorDelegate,
            PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 }
        };

And my HandleCameraResolutionSelectorDelegate:

CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
    //Don't know if this will ever be null or empty
    if (availableResolutions == null || availableResolutions.Count < 1)
        return new CameraResolution () { Width = 800, Height = 600 };

    //Debugging revealed that the last element in the list
    //expresses the highest resolution. This could probably be more thorough.
    return availableResolutions [availableResolutions.Count - 1];
}

That's all I needed to change to get a driver's license (PDF417) barcode to scan.

Here's the source code for MobileBarcodeScanningOptions.cs from ZXing github.

bonetoad
  • 156
  • 1
  • 5
  • That did the trick. I am able to scan the PDF417 barcodes now. BTW, do you know how can I use the native iOS scanner (AVCaptureSession) from xamarin forms ? – xdev Mar 09 '17 at 06:23
  • Is this helpful: [Using Apple's AVCaptureSession (iOS7 Built in) Barcode Scanning](https://github.com/Redth/ZXing.Net.Mobile/blob/master/readme.md#using-apples-avcapturesession-ios7-built-in-barcode-scanning)? – bonetoad Mar 09 '17 at 17:41
  • @bonetoad I am new in Xamarin form, I have tried to implement above code but could not get success. Could you please help me if you have implemented any demo for the same. – Gagan_iOS May 16 '17 at 08:02