【问题标题】:Using ZXing in Xamarin for Android, how do I stop continuous scanning right after I get my result?在 Xamarin for Android 中使用 ZXing,如何在获得结果后立即停止连续扫描?
【发布时间】:2017-03-03 19:30:54
【问题描述】:

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

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

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

代码如下:

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

                }
            }

谢谢!

【问题讨论】:

  • 你试过scanner.Stop()吗?
  • 如果我在 HandleScanResult 中放入类似的内容,则会收到如下错误消息:错误 CS0103 The name 'scanner' does not exist in the current context
  • 让它成为类的成员变量?
  • 没用,显然是scanner.Cancel();由于错误而无法正常工作...?

标签: android xamarin zxing scanning continuous


【解决方案1】:

老问题,但无论如何我都会将其发布给仍在寻找此信息的任何人。 你需要你的扫描仪是一个类变量。这是我的代码:

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

【讨论】:

    最近更新 更多