androidxamarinzxingscanningcontinuous

Using ZXing in Xamarin for Android, how do I stop continuous scanning right after I get my result?


I'm using ZXing in an Android app being developed in Xamarin to scan a QR code and start playing the corresponding audio file automatically.

My problem is that when I get a result from scanning, it takes some time for the audio player activity to load so it gets called twice or more due to subsequent successful scannings.

Is there a way to stop continuous scanning as soon as I get a correct result?

Here's the code:

            //Start scanning
        scanner.ScanContinuously(opt, HandleScanResult);

    }

    private void HandleScanResult(ZXing.Result result)
    {
        string msg = "";

        if (result != null && !string.IsNullOrEmpty(result.Text))
        {

            msg = result.Text;

            var playerActivity = new Intent(myContext, typeof(AudioActivity));

            //-------------------------------------------------------------                   
            // Prerequisite: load all tracks onto "Assets/tracks" folder
            // You can put here qr code - track assignments here below
            // msg: decoded qr code
            // playerActivity.Putextra second parameter is a relative path
            // under "Assets" directory
            //--------------------------------------------------------------

            //Iterate through tracks stored in assets and load their titles into an array
            System.String[] trackArray = Application.Context.Assets.List("tracks");

            bool trackFound = false;
            foreach (string track in trackArray)
            {
                if (track.Equals(msg + ".mp3"))
                {
                    playerActivity.PutExtra("Track", "tracks/" + msg + ".mp3");

                    for (int i = 0; i < PostList.postList.Count; i++)
                    {
                        if (PostList.postList.ElementAt(i).code.Equals(msg))
                            playerActivity.PutExtra("TrackTitle", PostList.postList.ElementAt(i).title);
                    }
                    myContext.StartActivity(playerActivity);

                    trackFound = true;

                }
            }

Thank you!


Solution

  • Old question but i'll post it anyway for anyone still looking for this information. You need your scanner to be a class variable. This is my code:

    public MobileBarcodeScanner scanner = new MobileBarcodeScanner();
    
    private void ArrivalsClick(object sender, EventArgs e)
        {
            try
            {
                if (Arrivals.IsEnabled)
                {
                    MobileBarcodeScanningOptions optionsCustom = new MobileBarcodeScanningOptions();
    
                    scanner.TopText = "Scan Barcode";
    
                    optionsCustom.DelayBetweenContinuousScans = 3000;
                    scanner.ScanContinuously(optionsCustom, ArrivalResult);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    
        private async void ArrivalResult(ZXing.Result result)
        {
            if (result != null && result.Text != "")
            {
                // Making a call to a REST API
    
                if (resp.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    int? res = JsonConvert.DeserializeObject<int>(resp.Content);
                    if (res == 0)
                    {
                        scanner.Cancel(); // <----- Stops scanner (Something went wrong)
                        Device.BeginInvokeOnMainThread(async () =>
                        {
                            await DisplayAlert("..", "..", "ΟΚ");
                        });
                    }
                    else
                    {
                        Plugin.SimpleAudioPlayer.ISimpleAudioPlayer player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
                        player.Load("beep.wav");
                        player.Play(); // Scan successful
                    }
                }
                else
                {
                    scanner.Cancel();
                    Device.BeginInvokeOnMainThread(async () =>
                    {
                        await DisplayAlert("..", "..", "ΟΚ");
                    });
                }
            }
        }