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

4

5

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

Classicize answered 29/12, 2016 at 15:20 Comment(1)
Where do you call this code from? I think I answered similar question. Please search my answersCaught
R
9

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;

}
Robinette answered 15/7, 2017 at 9:0 Comment(2)
This didnt work for me yet i am using the scanner page :-(Natatorial
Worked for me . Thanks heaps RiteshSomersault
A
0

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;
}
Afire answered 30/12, 2016 at 7:46 Comment(1)
how i can add a Customoverlay to that. i need a back button on the page. it doesnt let me use scanner.CustomOverlay = viewtest as every example i saw was usingClassicize
H
0

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
    }
}
Halfwitted answered 11/2, 2020 at 18:0 Comment(0)
L
0

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

Levin answered 9/6, 2020 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.