【问题标题】:BarcodeDetector should only scan when a Button is clickedBarcodeDetector 仅应在单击按钮时进行扫描
【发布时间】:2019-03-28 12:59:54
【问题描述】:

我正在尝试使用集成的条形码扫描仪编写应用程序。 我遵循了本教程: https://www.c-sharpcorner.com/article/xamarin-android-qr-code-reader-by-mobile-camera/

扫描工作正常且速度非常快(在我使用 ZXing.Net.Mobile 之前,它非常慢)。 现在我需要一些帮助来集成应用程序仅在用户按下按钮而不是整个时间时检测到一个条形码。也许延迟也能解决问题。

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource  
            SetContentView(Resource.Layout.ScannerTest);
            surfaceView = FindViewById<SurfaceView>(Resource.Id.cameraView);
            txtResult = FindViewById<TextView>(Resource.Id.txtResult);
            barcodeDetector = new BarcodeDetector.Builder(this)
                .SetBarcodeFormats(BarcodeFormat.Code128 | BarcodeFormat.Ean13 | BarcodeFormat.QrCode)
                .Build();
            cameraSource = new CameraSource
                .Builder(this, barcodeDetector)
                .SetRequestedPreviewSize(320, 480)
                .SetAutoFocusEnabled(true)
                .Build();

            surfaceView.Click += StartScanning;
            surfaceView.Holder.AddCallback(this);
            //barcodeDetector.SetProcessor(this);
        }
private void StartScanning(object sender, EventArgs e)
{
        barcodeDetector.SetProcessor(this);
}
public void ReceiveDetections(Detections detections)
        {
            SparseArray qrcodes = detections.DetectedItems;
            if (qrcodes.Size() != 0)
            {
                txtResult.Post(() => {
                    //Vibrator vibrator = (Vibrator)GetSystemService(Context.VibratorService);
                    //vibrator.Vibrate(1000);
                    txtResult.Text = ((Barcode)qrcodes.ValueAt(0)).RawValue;
                });
            }
        }

在用户按下 SurfaceView 的那一刻,扫描仪启动并且永不停止。

有没有可能,它只是在按下“按钮”后扫描一个?

r3d007

【问题讨论】:

  • 看看这个post可能会有所帮助。

标签: c# xamarin.android


【解决方案1】:

1 在 OnCreate 方法中取消注释这一行

barcodeDetector.SetProcessor(this);

2 从 SurfaceCreated 和 OnRequestPermissionsResult 方法中删除或注释此行

cameraSource.Start(surfaceView.Holder);

3 您的 StartScanning 方法应该调用 Start

private void StartScanning(object sender, EventArgs e)
{
    cameraSource.Start(surfaceView.Holder);
}

4 读取并验证代码后,停止扫描仪

public void ReceiveDetections(Detections detections)
{
    SparseArray qrcodes = detections.DetectedItems;
    if (qrcodes.Size() != 0)
    {
        txtResult.Post(() => {
            //Vibrator vibrator = (Vibrator)GetSystemService(Context.VibratorService);
            //vibrator.Vibrate(1000);
            txtResult.Text = ((Barcode)qrcodes.ValueAt(0)).RawValue;
        });

        using (var h = new Handler (Looper.MainLooper))
        h.Post (() => {
            cameraSource.Stop();
        });

    }
}

为防止崩溃,请考虑隐藏或禁用该按钮,直到您获得相机权限并且扫描仪已经启动。

【讨论】:

  • 尝试过这种方式,第一次单击按钮时,它会扫描并在找到结果后停止扫描。但是当我再次单击该按钮时,它不会开始扫描,并且没有其他按钮正在工作。那时甚至 Android 的后退按钮都不起作用。
  • @r3d007 检查更新,崩溃是停止时引起的,而不是当您尝试重新启动时,receiveDetections 方法中的代码在不同的线程上运行,因此您需要发布 cameraSource .Stop() 来自处理程序
  • 我的情况是,我在 txtResult.Post(()={}) 方法中调用了多个 api。我遇到的问题是 ReceiveDetections 方法调用了多次,而我的 API 也调用了多个次。我怎样才能防止这种情况发生。任何形式的帮助都是高度合适的。
  • @DanielBrughera 请看一看。这是我的问题stackoverflow.com/questions/63535237/…
【解决方案2】:

您需要在触发扫描过程后添加此项。必须添加“-”运算符以防止不停工作。您在此行插入事件

//adds the handler
surfaceView.Click += StartScanning;

之后你需要这个。

 // removes the handler
surfaceView.Click -= StartScanning;

也看here

【讨论】:

  • 这会从 SurfaceView 中移除 EventHandler 并且不会停止扫描过程。
  • 你的扫描过程在哪里可以分享一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
  • 1970-01-01
  • 1970-01-01
  • 2018-04-14
相关资源
最近更新 更多