如何禁用多重条码检测 android Vision API

How to Disable multiple barcode detection android Vision API

我试图同时禁用多个 Barcode 检测。 如何使用 Google Vision API 禁用 MultiProcessor,我无法从官方网站 here

找到任何解决方案

我已经从 here

下载示例

代码

BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
    BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay);
    barcodeDetector.setProcessor(
            new MultiProcessor.Builder<>(barcodeFactory).build());

即使删除下面的线,我也根本检测不到。

 barcodeDetector.setProcessor(
        new MultiProcessor.Builder<>(barcodeFactory).build());

更新您的 BarcodeGraphicTracker 并实现 onNewItem 函数如下:

public class BarcodeGraphicTracker extends Tracker<Barcode> {

        private static final String TAG = "Barcode-reader";
        @Override
        public void onNewItem(int id, final Barcode item) {
            Log.d(TAG, "Detected!!"+ item.toString());
                Runnable r=new Runnable()
                {
                    public void run()
                    {
                        //write your code here
                        }
                    }
                };
                getActivity().runOnUiThread(r);
        }

        @Override
        public void onUpdate(Detector.Detections<Barcode> detectionResults, Barcode item) {

        }
    }

和 BarcodeTrackerFactory 为:

public class BarcodeTrackerFactory implements MultiProcessor.Factory<Barcode> {

        @Override
        public Tracker<Barcode> create(Barcode barcode) {
            return new BarcodeGraphicTracker();
        }

    }

我已经通过探索示例代码和一些逻辑实现同时解决了这个多重 Barcode 检测问题。

解决方法是:停止mPreview,1秒后重新开始delay

创建回调监听器

public interface BarCodeDetectListener {
void onBarCodeDetect(Barcode barcode);
}

BarcodeGraphicTracker

当设备在 onNewItem() 中检测到 Barcode 回调到 Barcodefragment

class BarcodeGraphicTracker extends Tracker<Barcode> {
...
...
private BarcodeGraphic mGraphic;
private BarCodeDetectListener barCodeDetectListener;
...

 /**
  * Start tracking the detected item instance within the item overlay.
  */
 @Override
 public void onNewItem(int id, Barcode item) {
    mGraphic.setId(id);
    barCodeDetectListener.onBarCodeDetect(item);
 }
}

BarCodeFragment

@SuppressLint("NewApi")
public class SurveyBarCodeFragment extends Fragment implements BarCodeDetectListener {
  ...
  private CameraSourcePreview mPreview;
  private CameraSource mCameraSource;
  ...



 @Override
 public void onBarCodeDetect(final Barcode barcode) {
  new Handler().post(new Runnable() {
        @Override
        public void run() {
            mPreview.stop(); //Stop preview
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        //Start preview after 1s delay
                        mPreview.start(mCameraSource);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }, 1000);
        }
    });

 }
}