How to stop the operation of CameraBarcodeReaderView from ZXing.Net.Maui library?

93 Views Asked by At

I implemented un CameraBarcodeReaderView of ZXing.Net.Maui for reader codes QR, for this i did:

I have two ContentPage's, one its my principal (MainPage) view and other second contentpage content the CameraBarcodeReaderView component (SecondPage). The Second page it's called through of a button from MainPage.

// Called SecondPage from button of MainPage
{
    await Navigation.PushModalAsync(new SecondPage(this));
}

When the CameraBarcodeReaderView detected a valid Code QR, returning the value of code QR to MainPage and returning to MainPage the focus of app.

    protected void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
    {           
        if (detected==0)
        { 
            detected = 1;
            ReturnMainPage(e.Results[0].Value);
        }
         
    }

    /// <summary>
    /// Returning to MainPage the code detected from camara
    /// </summary>
    /// <param name="code">Code detected from camara</param>
    public async void ReturnMainPage(string code)
    {
        pather_clients.code = code;                      
        await App.Current.MainPage.Navigation.PopModalAsync();
        WeakReferenceMessenger.Default.Send(MainPage);
    }

But the problem is that the CameraBarcodeReaderView component stays active and continuous reading QR Codes and I can't stop it.

How do I stop this CameraBarcodeReaderView component? or How do I Remove/Delete/Disponse the SecondPage? or Does anyone have a better way to implement the CameraBarcodeReaderView component?

2

There are 2 best solutions below

1
On BEST ANSWER

Your post states that the CameraBarcodeReaderView "stays active" and performs "continuous reading". There are two booleans that you can use to idle it, and the one I would personally use is IsDetecting, shown here with a binding to the view model (and there is also IsEnabled).

<Grid
    IsVisible="{Binding IsDetectingInternal}">
    <xzing:CameraBarcodeReaderView
        x:Name="barcodeScanner"
        VerticalOptions="Fill"
        BarcodesDetected="OnBarcodeDetected"
        IsDetecting="{Binding IsDetecting}">
        ...

For a working example of this, including a GitHub code repo, see my answer to this question because it does the very thing you're asking about.

0
On

You can add below in the constructor of the ContentPage to release the camera resource:

 Unloaded += (sender, e) => { cameraBarcodeReaderView.Handler.DisconnectHandler(); };

Add don't forget to add the x:Name of the control to the XAML page like below:

<zxing:CameraBarcodeReaderView x:Name="cameraBarcodeReaderView" x:FieldModifier="public" />