在Android Mobile Vision中使用管道检测文本时获取帧

ama*_*uel 2 android android-vision

我正在使用管道检测文本,就像Google CodeLabs示例代码一样.如何获取CameraSource发送到TextRecognizer的预览帧?

pm0*_*464 5

预览帧不会发送到文本识别器之外.但是,您可以创建一个包装文本识别器的类,在检测之前接收预览帧.在这里看到类似的讨论.

首先,实现一个检测器类来包装文本识别器:

class MyTextRecognizer extends Detector<TextBlock> {
  private Detector<TextBlock> mDelegate;

  MyTextRecognizer(Detector<TextBlock> delegate) {
    mDelegate = delegate;
  }

  public SparseArray<TextBlock> detect(Frame frame) {
    // *** add your code to access the preview frame here
    return mDelegate.detect(frame);
  }

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

  public boolean setFocus(int id) {
    return mDelegate.setFocus(id);
  }
}
Run Code Online (Sandbox Code Playgroud)

将文本识别器包裹在您的班级中,并将您的班级传递到相机源.它看起来像这样:

TextRecognizer textRecognizer = new TextRecognizer.Builder(context)
        .build();
TextRecognizer myTextRecognizer = new MyTextRecognizer(textRecognizer);

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

mCameraSource = new CameraSource.Builder(context, myTextRecognizer)
        .build();
Run Code Online (Sandbox Code Playgroud)

首先使用原始帧数据调用MyTextRecognizer.

请注意,如果旋转设备,图像可能不是直立的.您可以通过框架的metadata.getRotation方法获取方向.

需要注意的一点是:一旦检测方法返回,就不应该访问帧像素数据.由于相机源会回收图像缓冲区,因此一旦方法返回,帧对象的内容将最终被覆盖.