发送帧到检测器

Send frame to detector

我在 TextureView 中有相机预览。如何将相机帧发送到我的条形码检测器?

barcodeDetector = new BarcodeDetector.Builder(this)
    .setBarcodeFormats(Barcode.ALL_FORMATS)
    .build();

barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {

    @Override
    public void release() {
    }

    @Override
    public void receiveDetections(Detector.Detections<Barcode> detections) {
        final SparseArray<Barcode> barcodes = detections.getDetectedItems();
        if (barcodes.size() != 0) {
           // do the operation
        }  
    }
}

您需要创建一个相机实例并将其 link 发送到您的检测器(它已经 link 发送到您的处理器)

mCameraSource = new CameraSource.Builder(context, barcodeDetector)
                   .setFacing(CameraSource.CAMERA_FACING_BACK)
                   .setRequestedFps(15.0f)
                   .build();

为了 link 将相机连接到您的 SurfaceView 并使用这样的代码启动它(当 SurfaceView 可用时)

mCameraSource.start(mSurfaceView.getHolder());

here Google 提供了一个工作示例。

对于TextureView使用手动检测

public void onSurfaceTextureUpdated(SurfaceTexture surface) {
          // Invoked every time there's a new Camera preview frame
           mTextureView.getBitmap(bitmap);
           Frame frame = new Frame.Builder().setBitmap(bitmap).build();
           SparseArray<Barcode> barcodes = barcodeDetector.detect(frame);
      }