Xamarin.iOS ZXing.Net.Mobile 条码扫描器

And*_*ian 3 barcode-scanner xamarin.ios zxing ios xamarin

我正在尝试将条形码扫描仪功能添加到我的 xamarin.ios 应用程序中。我正在从 Visual Studio 进行开发,并添加了来自 xamarin 组件商店的 Zxing.Net.Mobile 组件。

我已经实现了它,如示例所示:

ScanButton.TouchUpInside += async (sender, e) => {
            //var options = new ZXing.Mobile.MobileBarcodeScanningOptions();
            //options.AutoRotate = false;
            //options.PossibleFormats = new List<ZXing.BarcodeFormat>() {
            //    ZXing.BarcodeFormat.EAN_8, ZXing.BarcodeFormat.EAN_13
            //};

            var scanner = new ZXing.Mobile.MobileBarcodeScanner(this);
            //scanner.TopText = "Hold camera up to barcode to scan";
            //scanner.BottomText = "Barcode will automatically scan";
            //scanner.UseCustomOverlay = false;
            scanner.FlashButtonText = "Flash";
            scanner.CancelButtonText = "Cancel";
            scanner.Torch(true);
            scanner.AutoFocus();

            var result = await scanner.Scan(true);
            HandleScanResult(result);
        };

void HandleScanResult(ZXing.Result result)
    {
        if (result != null && !string.IsNullOrEmpty(result.Text))
            TextField.Text = result.Text;
    }
Run Code Online (Sandbox Code Playgroud)

问题是,当我点击扫描按钮时,捕获视图会正确显示,但如果我尝试捕获条形码,则不会发生任何事情,并且扫描仪似乎无法识别任何条形码。

有人经历过这个问题吗?我怎样才能让它发挥作用?

在此先感谢您的帮助!

小智 6

我在这里回答了类似的问题。我无法扫描条形码,因为默认相机分辨率设置得太低。本案例的具体实现是:

ScanButton.TouchUpInside += async (sender, e) => {
        var options = new ZXing.Mobile.MobileBarcodeScanningOptions {
            CameraResolutionSelector = HandleCameraResolutionSelectorDelegate
        };

        var scanner = new ZXing.Mobile.MobileBarcodeScanner(this);
        .
        .
        .
        scanner.AutoFocus();

        //call scan with options created above
        var result = await scanner.Scan(options, true);
        HandleScanResult(result);
    };
Run Code Online (Sandbox Code Playgroud)

然后是 的定义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];
}
Run Code Online (Sandbox Code Playgroud)