我在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
}
}
}
答案 0 :(得分:2)
您需要创建一个摄像头实例并将其链接到您的探测器(已连接到您的处理器)
mCameraSource = new CameraSource.Builder(context, barcodeDetector)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedFps(15.0f)
.build();
要将相机链接到SurfaceView并启动它,请使用这样的代码(当SurfaceView可用时)
mCameraSource.start(mSurfaceView.getHolder());
您有一个由Google提供的here工作示例。
对于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);
}