简体   繁体   中英

ZXing.net.mobile view can't scan barcodes

Update: It's a hardware problem. It works on a newer phone.

Added ZXing.Net.Mobile and ZXing.Net.Mobile.Forms to project.

Scanner view and camera looks fine. Flash button works. But OnScanResult is never raised. Tried various DataMatrix, PDF417, and QR codes.

Test phone is HTC M9.

ScannerPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
             x:Class="MyApp.ScannerPage">
    <ContentPage.Content>
        <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <zxing:ZXingScannerView
                x:Name="ScannerView"/>
            <zxing:ZXingDefaultOverlay
                x:Name="ScannerOverlay"
                TopText="Hold your phone up to the barcode"
                BottomText="Scanning will happen automatically"/>
        </Grid>
    </ContentPage.Content>
</ContentPage>

ScannerPage.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ScannerPage : ContentPage
{
    public ScannerPage()
    {
        InitializeComponent();

        ScannerView.Options = new ZXing.Mobile.MobileBarcodeScanningOptions
        {
            PossibleFormats = new List<BarcodeFormat>
            {
                BarcodeFormat.DATA_MATRIX,
                BarcodeFormat.PDF_417,
                BarcodeFormat.QR_CODE
            },
            TryHarder = true
        };

        ScannerView.OnScanResult += (result) =>
        {
            var x = 3; // Breakpoint here, never hit

            Device.BeginInvokeOnMainThread(async () =>
            {
                // Stop analysis until we navigate away so we don't keep reading barcodes
                //ScannerView.IsAnalyzing = false;

                // Show an alert
                await DisplayAlert("Scanned Barcode", result.Text, "OK");
            });
        };

        ScannerOverlay.ShowFlashButton = ScannerView.HasTorch;
        ScannerOverlay.FlashButtonClicked += (se, ev) => ScannerView.ToggleTorch();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        ScannerView.IsAnalyzing = true;
        ScannerView.IsScanning = true;
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        ScannerView.IsScanning = false;
    }
}

Maybe an hardware problem.. a low resolution camera? try with a more recent smartphone...

You can also try with "simplest" barcode (code128, ean8, ean13...)

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