我正在使用移动视觉API扫描条形码。我正在使用TextureView
将摄像头渲染到上面。我阅读了这个github线程https://github.com/googlesamples/android-vision/issues/15,看起来移动视觉不兼容TextureView
我发现CameraSource
是为SurfaceView
设计的,不兼容TextureView
,因为没有方法在TextureView
上预览帧。https://developers.google.com/android/reference/com/google/android/gms/vision/CameraSource.html#start(android.view.SurfaceHolder)
我尝试使用以下方法,并检查到receiveDetections
没有被调用。有人知道如何将移动视觉API与TextureView
和Camera
集成吗?
MainActivity.java
@RuntimePermissions
public class ScanBarcodeActivity extends BaseActivity {
private TextureView textureView;
private BarcodeDetector barcodeDetector;
private Camera camera;
private String TAG = LogUtils.makeLogTag(ScanBarcodeActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityScanBarcodeBinding activityScanBarcodeBinding = setContentView(this, R.layout.activity_scan_barcode);
textureView = activityScanBarcodeBinding.textureView;
barcodeDetector = new BarcodeDetector.Builder(this).build();
ScanBarcodeActivityPermissionsDispatcher.requestCameraPermissionWithCheck(this);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// NOTE: delegate the permission handling to generated method
ScanBarcodeActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
if(permissions[0].equals(Manifest.permission.CAMERA)) {
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initializeCamera(); }
}
}
@NeedsPermission(Manifest.permission.CAMERA)
void requestCameraPermission() {
initializeCamera();
}
void initializeCamera() {
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
camera = Camera.open();
/* Set Auto focus */
Camera.Parameters parameters = camera.getParameters();
List<String> focusModes = parameters.getSupportedFocusModes();
if(focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)){
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
} else
if(focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)){
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
camera.setParameters(parameters);
try {
camera.setPreviewTexture(surface);
} catch (IOException io) {
LogUtils.LOGD(TAG, io.getMessage());
}
camera.startPreview();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
try {
camera.stopPreview();
camera.release();
} catch (Exception e) {
LogUtils.LOGD(TAG, e.getMessage());
}
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
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) {
LogUtils.LOGD(TAG, barcodes.valueAt(0).displayValue);
}
}
});
}
@OnShowRationale(Manifest.permission.CAMERA)
void showRationaleForCamera(final PermissionRequest request) {
new AlertDialog.Builder(this)
.setMessage(R.string.permission_camera_rationale)
.setPositiveButton(R.string.button_allow, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.proceed();
}
})
.setNegativeButton(R.string.button_deny, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.cancel();
}
})
.show();
}
@OnPermissionDenied(Manifest.permission.CAMERA)
void showPermissionDeniedForCamera() {
Toast.makeText(this, R.string.permission_camera_denied, Toast.LENGTH_SHORT).show();
}
@OnNeverAskAgain(Manifest.permission.CAMERA)
void showNeverAskAgainForCamera() {
Toast.makeText(this, R.string.permission_camera_never_ask_again, Toast.LENGTH_SHORT).show();
}
}