我正在使用移动视觉 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
是否没有被调用。有谁知道如何将移动视觉 APITextureView
与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();
}
}