0

我正在使用 Xamarin.Android 和 ZXing.net.mobile C# 库开发一个 Android 应用程序。

我想开发一个 Android 应用程序来扫描以 base64 字符串编码的 PDF-417 中的二维条码。

这是我第一次使用这个库,仍然没有找到关于如何使用这个库的文档。

我已经按照这里的例子https://github.com/Redth/ZXing.Net.Mobile这里

以下是我的活动代码:

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();
        }
    }
}

应用程序在设备上编译和安装,我没有收到运行时错误,但我没有得到扫描结果。扫描仪只是不断重新聚焦。我已将条形码格式限制为 PDF-417。

我究竟做错了什么?

4

1 回答 1

0

bonetoad回复中得到答案

于 2018-03-29T05:20:14.123 回答