Android 愿景 - 减少条形码跟踪 window

Android Vision - Reduce bar code tracking window

我正在尝试将 Google Visions 扫描仪应用到我正在处理的应用程序中。默认情况下它是全屏 activity 并且条形码在整个屏幕上被跟踪。

但是,我需要一个全屏相机,但扫描范围有限 window。例如,相机的表面视图需要全屏,它有 2 个透明叠加层设置为屏幕高度顶部和底部的 35%,在中心留下 30% 的视口。

我已经更改了图形叠加层,因此它只会显示在中间视口中,但无法弄清楚如何将条形码跟踪器限制在同一区域。

有什么想法吗?

当前API不提供限制扫描区域的方法。但是,您可以过滤来自检测器的结果或裁剪传递到检测器的图像。

过滤结果方法

使用这种方法,条形码检测器仍会扫描整个图像区域,但检测到的目标区域之外的条形码将被忽略。这样做的一种方法是实施 "focusing processor" 从检测器接收结果,并且最多只将一个条形码传递给关联的跟踪器。例如:

public class CentralBarcodeFocusingProcessor extends FocusingProcessor<Barcode> {

  public CentralBarcodeFocusingProcessor(Detector<Barcode> detector, Tracker<Barcode> tracker) {
    super(detector, tracker);
  }

  @Override
  public int selectFocus(Detections<Barcode> detections) {
    SparseArray<Barcode> barcodes = detections.getDetectedItems();
    for (int i = 0; i < barcodes.size(); ++i) {
      int id = barcodes.keyAt(i);
      if (/* barcode in central region */) {
        return id;
      }
    }
    return -1;
  }
}

然后您可以像这样将此处理器与检测器相关联:

   BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
   barcodeDetector.setProcessor(
                new CentralBarcodeFocusingProcessor(myTracker));

裁剪图像方法

在调用检测器之前,您需要先自己裁剪图像。这可以通过实现一个 Detector 子类来完成,该子类包装条形码检测器,裁剪接收到的图像,并使用裁剪后的图像调用条形码扫描仪。

例如,您可以制作一个检测器来拦截和裁剪图像,如下所示:

class MyDetector extends Detector<Barcode> {
  private Detector<Barcode> mDelegate;

  MyDetector(Detector<Barcode> delegate) {
    mDelegate = delegate;
  }

  public SparseArray<Barcode> detect(Frame frame) {
    // *** crop the frame here
    return mDelegate.detect(croppedFrame);
  }

  public boolean isOperational() {
    return mDelegate.isOperational();
  }

  public boolean setFocus(int id) {
    return mDelegate.setFocus(id);
  }
} 

您可以用这个包裹条码检测器,将其放在相机源和条码检测器之间:

BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context)
        .build();
MyDetector myDetector = new MyDetector(barcodeDetector);

myDetector.setProcessor(/* include your processor here */);

mCameraSource = new CameraSource.Builder(context, myDetector)
        .build();

基于@pm0733464 的回答,并举例说明如何获取最靠近预览中心的条形码。

public class CentralBarcodeFocusingProcessor extends FocusingProcessor<Barcode> {

    public CentralBarcodeFocusingProcessor(Detector<Barcode> detector, Tracker<Barcode> tracker) {
        super(detector, tracker);
    }

    @Override
    public int selectFocus(Detector.Detections<Barcode> detections) {

        SparseArray<Barcode> barcodes = detections.getDetectedItems();
        Frame.Metadata meta = detections.getFrameMetadata();
        double nearestDistance = Double.MAX_VALUE;
        int id = -1;

        for (int i = 0; i < barcodes.size(); ++i) {
            int tempId = barcodes.keyAt(i);
            Barcode barcode = barcodes.get(tempId);
            float dx = Math.abs((meta.getWidth() / 2) - barcode.getBoundingBox().centerX());
            float dy = Math.abs((meta.getHeight() / 2) - barcode.getBoundingBox().centerY());

            double distanceFromCenter =  Math.sqrt((dx * dx) + (dy * dy));

            if (distanceFromCenter < nearestDistance) {
                id = tempId;
                nearestDistance = distanceFromCenter;
            }
        }
        return id;
    }
}