Can ZXing be stopped or dispose so i can use it again?

4.3k Views Asked by At

im using ZXing.Net.Mobile for Forms like this

                    var scanPage = new ZXingScannerPage();

                    scanPage.OnScanResult += (result) => {
                        // Stop scanning
                        scanPage.IsScanning = false;
                        // Pop the page and show the result
                        Device.BeginInvokeOnMainThread(async () => {
                          //  await Navigation.PopAsync();
                            await Navigation.PushModalAsync(new Pages.DataGridPage(PladsId));

                        });
                    };

from https://components.xamarin.com/gettingstarted/zxing.net.mobile.forms

but after i have scanned once the carmera is frozen when i try again i have tried to Dispose/stop the scanner but without success

can ZXing be stopped or dispose so i can use it again ?

im using visual studio 2015 community, xamarin.Forms 2.3.3.168, Syncfusion 14.4.0.15 and ZXing.Net.Mobile 2.1.47. running it on a sony xperia z3 with Android version 6.0.1 and using API 23

Any help is deeply appreciated

4

There are 4 best solutions below

2
On BEST ANSWER

Found the solution....

Use IsScanning=true only once... In ScannerView Constructor or in OnAppearing of the Page..

_zxing = new ZXingScannerView
    {
        VerticalOptions = LayoutOptions.Center,
        HorizontalOptions = LayoutOptions.Center,
        HeightRequest = 250,
        WidthRequest = 250,
        IsAnalyzing = true,
        IsScanning = true,

    };

Don't write anything in OnDisappearing...

protected override void OnDisappearing()
{
   // _zxing.IsScanning = false;
    base.OnDisappearing();

}

IsAnalysing to be set false once scanning complete and should be set true in OnAppearing...

_zxing.OnScanResult += (result) =>       
Device.BeginInvokeOnMainThread(async () =>
{
     if (!string.IsNullOrWhiteSpace(result.Text))
     {
         _zxing.IsAnalyzing = false;
         await OnGettingResult(result.Text);
     }
});

protected override void OnAppearing()
{
    base.OnAppearing();
    _zxing.IsAnalyzing = true;

   //Not required if already set while intialization
   //_zxing.IsScanning = true;

}
1
On

Why don't you use it with async? After its done scanning, it returns me back in the navigation stack. It looks like your using the generic Scanner Page so the following solution would work.

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

var result = await scanner.Scan();

if (result != null)
{
    variableToAssign = result.Text;
}
0
On

there's a workaround that should be work at start of your page and start of your action you will unsubscribe

scanPage.OnScanResult -= YourAction();

after-action finished you should subscribe again for scanning

scanPage.OnScanResult += YourAction();

0
On

If like me, you wanted the scanner to not switch off after one scan, or only scan the same code once, then you want to check if you have already scanned a particular qr code.

var options = new MobileBarcodeScanningOptions();
var scans = new HashSet<string>();

options.PossibleFormats.Add(ZXing.BarcodeFormat.QR_CODE);
ZXingScannerPage scanPage = new ZXingScannerPage(options);
scanPage.Title = "Scan QR Code";

scanPage.OnScanResult += (result) =>
{
    //Only scan a particular QR code once
    if (!scans.Contains(result.Text))
    {
        scans.Add(result.Text);

        //etc
    }
}