ZXing barcode reader

DerekHagenDerekHagen USMember ✭✭

I am having some issues with ZXing tool with Xamarin.Forms and on the IOS (ipad) platform. When I hold the IPad in the landscape position everything works find with scanning a barcode. When I hold the Ipad in the portrait (vertical) position and try to scan a barcode it doesn't work. It won't read the barcode until I rotate the barcode to a verticle (top/bottom) position. This seems to work fine on an Android no matter how I hold my phone. Just wondering if there is something I am missing?

Essentially my current code is as follows:

var scanPage = new ZXingScannerPage ();
// Navigate to our scanner page
await Navigation.PushAsync (scanPage);

scanPage.OnScanResult += (result) => 
{
    // Stop scanning
    scanPage.IsScanning = false;

    // Pop the page and show the result
    Device.BeginInvokeOnMainThread (async () => 
    {
        await Navigation.PopAsync ();        
        await DisplayAlert("Scanned Barcode", result.Text, "OK");
    });
};

There is a similar issues posted below but with no solution:
https://stackoverflow.com/questions/44017593/barcode-scanner-does-not-work-in-portrait-mode-xamarin-form

ZXing.Net.Mobile = 2.2.9
ZXing.Net.Mobile.Forms = 2.2.9
Xamarin.Forms = 2.3.4.231

Answers

  • Vaka.GopiNadhReddyVaka.GopiNadhReddy USMember ✭✭✭

    @DerekHagen

    i think it will help you. it worked fine in my case......

    ZXingScannerView zxing = new ZXingScannerView
    {
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand,
    };

            zxing.AutoFocus();
    
            zxing.OnScanResult += (result) =>
                Device.BeginInvokeOnMainThread(async () =>
                {
    
                    zxing.IsAnalyzing = false;
                    zxing.IsScanning = false;
    
                    await Navigation.PushModalAsync(new ResultPage(result.Text));
    
                });
    
  • DerekHagenDerekHagen USMember ✭✭

    @Vaka.GopiNadhReddy said:
    @DerekHagen

    i think it will help you. it worked fine in my case......

    ZXingScannerView zxing = new ZXingScannerView
    {
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand,
    };

          zxing.AutoFocus();
    
          zxing.OnScanResult += (result) =>
              Device.BeginInvokeOnMainThread(async () =>
              {
    
                  zxing.IsAnalyzing = false;
                  zxing.IsScanning = false;
    
                  await Navigation.PushModalAsync(new ResultPage(result.Text));
    
              });
    

    So you are saying in order for me to get this to work I need to use the ZXingScannerView instead of the ZXingScannerPage?

  • DerekHagenDerekHagen USMember ✭✭

    This still does not work. I can scan when holding the ipad in its horizontal position but not vertical.

        using Xamarin.Forms;
        using ZXing.Net.Mobile.Forms;
    
        namespace My.Pages
        {
            public class BarcodeScanPage : ContentPage
            {
                ZXingScannerView zxing;
                ZXingDefaultOverlay overlay;
    
                public BarcodeScanPage()
                {
                    zxing = new ZXingScannerView
                    {
                        HorizontalOptions = LayoutOptions.FillAndExpand,
                        VerticalOptions = LayoutOptions.FillAndExpand
                    };
    
                    zxing.AutoFocus();
                    zxing.OnScanResult += (result) =>
                        Device.BeginInvokeOnMainThread(async () => {
    
                            // Stop analysis until we navigate away so we don't keep reading barcodes
                            zxing.IsAnalyzing = false;
                            zxing.IsScanning = false;
    
                            // Show an alert
                            await DisplayAlert("Scanned Barcode", result.Text, "OK");
    
                            // Navigate away
                            await Navigation.PopAsync();
                        });
    
                    overlay = new ZXingDefaultOverlay
                    {
                        TopText = "Hold your phone up to the barcode",
                        BottomText = "Scanning will happen automatically",
                        ShowFlashButton = zxing.HasTorch,
                    };
                    overlay.FlashButtonClicked += (sender, e) => {
                        zxing.IsTorchOn = !zxing.IsTorchOn;
                    };
                    var grid = new Grid
                    {
                        VerticalOptions = LayoutOptions.FillAndExpand,
                        HorizontalOptions = LayoutOptions.FillAndExpand,
                    };
                    grid.Children.Add(zxing);
                    grid.Children.Add(overlay);
    
                    // The root page of your application
                    Content = grid;
                }
    
                protected override void OnAppearing()
                {
                    base.OnAppearing();
    
                    zxing.IsScanning = true;
                }
    
                protected override void OnDisappearing()
                {
                    zxing.IsScanning = false;
    
                    base.OnDisappearing();
                }
            }
        }
    
  • Vaka.GopiNadhReddyVaka.GopiNadhReddy USMember ✭✭✭

    @DerekHagen

    Then you have to open an issue in ZXing github

  • DerekHagenDerekHagen USMember ✭✭

    I put in a new issue located below. Hopefully I will get some guidance.

    https://github.com/Redth/ZXing.Net.Mobile/issues/572

  • ClapotiClapoti CAMember ✭✭
    edited July 6
  • JassimRahmaJassimRahma USMember ✭✭✭

    I have a problem with ZXing too.. I am using below code and installed the apk on Samsung Galaxy.. I am able to view the camera but the result of the QR Code is not showing and I am not getting any DisplayAlert..

    public void Scan()
    {
        try
        {
            scanner.Options = new MobileBarcodeScanningOptions()
            {
                UseFrontCameraIfAvailable = false, //update later to come from settings
                PossibleFormats = new List<BarcodeFormat>(),
                TryHarder = true,
                AutoRotate = false,
                TryInverted = true,
                DelayBetweenContinuousScans = 2000,
            };
    
            scanner.VerticalOptions = LayoutOptions.FillAndExpand;
            scanner.HorizontalOptions = LayoutOptions.FillAndExpand;
    
            // scanner.IsVisible = false;
    
            scanner.Options.PossibleFormats.Add(BarcodeFormat.QR_CODE);
            // scanner.Options.PossibleFormats.Add(BarcodeFormat.DATA_MATRIX);
            // scanner.Options.PossibleFormats.Add(BarcodeFormat.EAN_13);
    
    
            scanner.OnScanResult += (result) => {
                // Stop scanning
                scanner.IsAnalyzing = false;
                scanner.IsScanning = false;
    
                if (scanner.IsScanning)
                {
                    scanner.AutoFocus();
                }
    
                // Pop the page and show the result
                Device.BeginInvokeOnMainThread(async () => {
                    if (result != null)
                    {
                        await DisplayAlert("Scan Value", result.Text, "OK");
                    }
                });
            };
    
            mainGrid.Children.Add(scanner, 0, 1);
        }
        catch (Exception ex)
        {
            DisplayAlert("Scan Value", ex.ToString(), "Error");
        }
    }
    
Sign In or Register to comment.