简体   繁体   中英

How do I read 2D barcode PDF-417 using ZXing.net.mobile library for Xamarin.Android

I am developing an Android app using Xamarin.Android and the ZXing.net.mobile C# library.

I want to develop an Android app to scan 2D barcode in PDF-417 that is encoded in base64 string.

This is my first time to use this library and still have not found documentation for a walk-through on how to use the library.

I have followed the example here https://github.com/Redth/ZXing.Net.Mobile and here

Below is my activity code:

using Android.App;
using Android.Widget;
using Android.OS;
using ZXing;
using ZXing.Mobile;
using System;
using System.Collections.Generic;

namespace BarcodeScannerDemo
{
    [Activity(Label = "ID Scanner Demo", MainLauncher = true)]
    public class MainActivity : Activity
    {
        Button buttonScan;

        MobileBarcodeScanner scanner;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            MobileBarcodeScanner.Initialize(Application);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            scanner = new MobileBarcodeScanner();

            buttonScan = FindViewById<Button>(Resource.Id.buttonScan);

            buttonScan.Click += ButtonScan_Click;
        }



        private async void ButtonScan_Click(object sender, EventArgs e)
        {
            var scannerOptions = new MobileBarcodeScanningOptions
            {
                PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 },
                TryHarder = true
            };

            var overlay = LayoutInflater.Inflate(Resource.Layout.CustomOverlay, null);

            Button buttonFlash = overlay.FindViewById<Button>(Resource.Id.buttonFlash);
            Button buttonCancel = overlay.FindViewById<Button>(Resource.Id.buttonCancel);

            buttonCancel.Click += (btnCancelSender, btnCancelEventArgs) => scanner.Cancel();
            buttonFlash.Click += (btnFlashSender, btnFlashEventArgs) => scanner.ToggleTorch();

            scanner.UseCustomOverlay = true;
            scanner.CustomOverlay = overlay;

            scanner.AutoFocus();

            HandleResult(await scanner.Scan(this, scannerOptions));
        }

        private void HandleResult(ZXing.Result result)
        {
            var message = "No Barcode!";

            if (result != null)
            {
                message = $"{result.BarcodeFormat}";
            }

            Toast.MakeText(this, message, ToastLength.Long).Show();
        }
    }
}

Application compiles and installs on device, I don't get a runtime error but I don't get the scan result. Scanner just keeps on refocusing. I have restricted the barcode format to PDF-417.

What am I doing wrong?

得到了从骨头蟾蜍答复

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM