从SurfaceView使用ByteBuffer时如何暂停android ML-kit中的条形码扫描

时间:2018-07-04 17:10:24

标签: android firebase surfaceview barcode-scanner firebase-mlkit

上下文

我正在使用Android firebase-ml-vision使用带有连续ByteBuffer图片框架的SurfaceView扫描条形码。我以ML kit quickstart project为起点,效果很好。

我的项目的目的是识别与条形码关联的产品,并将其添加到扫描项目列表中。

问题

相机对焦后,条形码处理器会多次检测到相同的条形码,因此您每秒可以扫描20条而不是1条条形码。

这是来自CamereSource.FrameProcessingRunnable.run的javadoc

 * As long as the processing thread is active, this executes detection on frames continuously.
 * The next pending frame is either immediately available or hasn't been received yet. Once it
 * is available, we transfer the frame info to local variables and run detection on that frame.
 * It immediately loops back for the next frame without pausing.

我试图在FrameProcessingRunnable中添加一个“已暂停”的检查,但是由于至少下一个帧已经被送入检测,因此我仍然至少两次被识别相同的条形码:

private class FrameProcessingRunnable implements Runnable {
private volatile boolean paused = false;
.
.
.
public void pause() {
  synchronized (lock) {
    this.paused = true;
    lock.notifyAll();
  }
}

public void resume() {
  synchronized (lock) {
    this.paused = false;
    lock.notifyAll();
  }
}

public void run() {
.
.
.
synchronized (processorLock) {
        if (!paused) {
          Log.d(TAG, "Process an image");
          frameProcessor.process(...

使用停止和启动的解决方案

由于无法暂停,我选择了从缓冲区中检测到条形码时停止并开始:

private CameraSourcePreview preview;
public void pauseImageProcessing() {
  preview.stop();
  try {
      preview.start(cameraSource, graphicOverlay);
  } catch (IOException e) {
  }
}

这可以工作,但是大约需要1秒钟的延迟才能再次启动,相机开始对焦并且可以检测到下一个条形码。无疑,这种方法还会消耗不必要的资源。您可能会说很好,但是在this video you'll see the difference between the camera scanner with Stop&Start and a Bluetooth scanner中:

更好的方法

我正在寻找一种解决方案,该解决方案将在成功检测到帧之后立即丢弃所有帧并重新开始,但是到目前为止,我失败了。我每秒使用20帧。

VissionProcessorBase确实有用于节流的代码

// Whether we should ignore process(). This is usually caused by feeding input data faster than
// the model can handle.
private final AtomicBoolean shouldThrottle = new AtomicBoolean(false);

但是距离我的需求还不够远:(

1 个答案:

答案 0 :(得分:1)

  

相机对焦后,条形码处理器会多次检测到同一条条形码,因此您将扫描20条而不是1条条形码   一秒钟。

VisionProcessorBase.java

post
相关问题