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

时间:2016-08-17 11:06:58

标签: ios xamarin xamarin.ios zxing barcode-scanner

我正在尝试将条形码扫描程序功能添加到我的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;
    }

问题是当我点击扫描按钮时,捕获视图显示正确,但如果我尝试捕获条形码,则没有任何反应,扫描仪似乎无法识别任何条形码。

有人遇到过这个问题吗?我怎样才能使它发挥作用?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我回答了类似的问题here。我无法扫描条形码,因为默认的相机分辨率设置得太低。这种情况的具体实施是:

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

然后是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];
}