Home > Blockchain >  how to make ZxingScanner start scanning?
how to make ZxingScanner start scanning?

Time:02-15

I am trying to implement barcode scanning in my Xamarin form, but no success.
I am able to get the camera working, but I don't see the red line on the screen and it simply refuses to scan anything

I tried this answer. I can see thru my camera, but no red line appears. But I can put the torch on and off

XAML code:

<Button  BackgroundColor="Chocolate" Clicked="Button_Clicked"/>
<zxing:ZXingScannerView 
    x:Name="_scanView" 
    OnScanResult="Handle_OnScanResult" 
    IsScanning="true" 
    IsAnalyzing="true"
    WidthRequest="200" 
    HeightRequest="200" />

C# Code:

private void Button_Clicked(object sender, EventArgs e)
{
    _scanView.ToggleTorch();
}

private void Handle_OnScanResult(ZXing.Result result)
{
    ChassisEntry.Text = result.Text;
}


// this is in the constructor of the page
MobileBarcodeScanningOptions options = new ZXing.Mobile.MobileBarcodeScanningOptions()
{
    TryHarder = true,
    PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.All_1D }
};
_scanView.Options = options;

What am I missing ?

EDIT

I have this in my MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    ZXing.Net.Mobile.Forms.Android.Platform.Init();
    LoadApplication(new App());
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
   Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
   ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);

   base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

CodePudding user response:

In my working project I used this xaml declaration:

<zxing:ZXingScannerView x:Name="qrCodeScannerView" 
                        OnScanResult="Handle_OnScanResult" 
                        IsScanning="true"
                        WidthRequest="1024" 
                        HeightRequest="400" />

Note, that I did not set isAnalyzing property in the xaml declaration as you did. As the page appears, the zxing control starts working and analyzing immediately.

Then in xaml.cs file

        public void Handle_OnScanResult(Result scanResult)
        {
                qrCodeScannerView.IsScanning = false;
                // processing scanResult.Text here
        }

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

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

That looks strange, but setting isScanning property also in xaml.cs file solved this task for me.

CodePudding user response:

Try removing the ZXingScannerView from your XAML completely. On a clicked event of a button add this code:

private async void ButtonScan(object sender, EventArgs e)
{
    PermissionStatus granted = await Permissions.CheckStatusAsync<Permissions.Camera>();
    if (granted != PermissionStatus.Granted)
    {
        _ = await Permissions.RequestAsync<Permissions.Camera>();
    }
    if (granted == PermissionStatus.Granted)
    {
        try
        {
            MobileBarcodeScanner scanner = new MobileBarcodeScanner();
            ZXing.Result result = await scanner.Scan();
            if (result != null && result.Text != "")
            {
                //You access your scanned text with result.Text
                scanner.Cancel(); // <--- This closes the scanner
            }
        }
        catch (Exception)
        {
            await DisplayAlert("Problem", "Something went wrong.", "ΟΚ");
        }
    }
    else
    {
        await DisplayAlert("Problem", "No permissions to use camera.", "ΟΚ");
    }
} 
  • Related