在 Xamarin 中使用 ZXing for Android,如何在获得结果后立即停止连续扫描?

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

我在 Xamarin 开发的 Android 应用程序中使用 ZXing 来扫描二维码并自动开始播放相应的音频文件。

我的问题是,当我获得扫描结果时,音频播放器 activity 需要一些时间才能加载,因此由于随后的成功扫描,它会被调用两次或更多次。

有没有办法在我得到正确结果后立即停止连续扫描?

代码如下:

            //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;

                }
            }

谢谢!

老问题了,但我还是会 post 为仍在寻找此信息的任何人提供答案。 你需要你的扫描仪是一个 class 变量。这是我的代码:

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("..", "..", "ΟΚ");
                });
            }
        }
    }