I am using mobile vision API for scanning barcode. I am using TextureView
to render camera on it. I read this github thread https://github.com/googlesamples/android-vision/issues/15 it seems mobile vision is not compatible with TextureView
I see that CameraSource
is for SurfaceView
which is not compatible with TextureView
, as there is no method to preview frames on TextureView
. https://developers.google.com/android/reference/com/google/android/gms/vision/CameraSource.html#start(android.view.SurfaceHolder)
I tried to do using below approach and checked that receiveDetections
is not being called. Does anyone have any idea how can I integrate mobile vision API with TextureView
and 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();
}
}