首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android视觉-减少条形码跟踪窗口

Android视觉-减少条形码跟踪窗口
EN

Stack Overflow用户
提问于 2016-04-04 14:39:33
回答 2查看 8.5K关注 0票数 12

我正在尝试将谷歌视觉扫描仪(GoogleVisions扫描器)实现到一个应用程序中。默认情况下,它是一个全屏活动,条形码在整个屏幕上被跟踪。

然而,我需要一个全屏相机,但有一个有限的扫描窗口。例如,相机的表面视图需要全屏幕,它有2个透明覆盖设置为35%的屏幕高度,顶部和底部留下30%的视口在中心。

我已经改变了图形覆盖,所以它将只显示在中间的视口,但无法计算出如何将条形码跟踪器限制在同一区域。

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-05 14:10:19

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

滤波结果接近

使用这种方法,条形码检测器仍将扫描整个图像区域,但在目标区域之外检测到的条形码将被忽略。这样做的一种方法是实现一个“聚焦处理器”,它接收来自检测器的结果,并且最多只将一个条形码传递给您相关的跟踪器。例如:

代码语言:javascript
复制
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;
  }
}

然后,将此处理器与检测器关联如下:

代码语言:javascript
复制
   BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
   barcodeDetector.setProcessor(
                new CentralBarcodeFocusingProcessor(myTracker));

裁剪图像的方法

在调用检测器之前,您需要自己裁剪图像。这可以通过实现检测器子类来实现,该子类封装条形码检测器,对接收到的图像进行裁剪,并调用条形码扫描器和裁剪的图像。

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

代码语言:javascript
复制
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);
  }
} 

您可以将条形码检测器与此封装在一起,将其放在相机源和条形码检测器之间:

代码语言:javascript
复制
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();
票数 11
EN

Stack Overflow用户

发布于 2017-09-21 01:33:24

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

代码语言:javascript
复制
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;
    }
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36405717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档