2

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

4

1 回答 1

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);
  }
}

用你的类包装文本识别器,并将你的类传递给相机源。它看起来像这样:

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();

您的 MyTextRecognizer 将首先使用原始帧数据调用。

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

请注意:一旦检测方法返回,您不应该访问帧像素数据。由于相机源回收图像缓冲区,一旦方法返回,帧对象的内容最终将被覆盖。

于 2016-09-06T15:15:27.337 回答