I am using Mobile Vision API to scan QR code.
I have this method in the activity to use the camera to capture the code:
private void createCameraSource() {
final BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).build();
if(!barcodeDetector.isOperational()){
Toast.makeText(this,
"No se puede utilizar el detector de códigos de barra", Toast.LENGTH_LONG).show();
this.finish();
}
else {
final CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
.setAutoFocusEnabled(true)
.setRequestedPreviewSize(1600, 1024)
.build();
cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
if (ActivityCompat.checkSelfPermission(RegisterActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
try {
cameraSource.start(cameraPreview.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
cameraSource.stop();
}
});
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 (!RegisterActivity.this.isProcessing() && barcodes.size() > 0) {
barcodeDetector.release();
RegisterActivity.this.setProcessing(true);
Intent intent = new Intent(RegisterActivity.this, ResultActivity.class);
intent.putExtra("code", codigo);
intent.putExtra("data", barcodes.valueAt(0));
setResult(CommonStatusCodes.SUCCESS, intent);
startActivity(intent);
RegisterActivity.this.setProcessing(false);
finish();
}
}
});
}
}
When a code is detected and read, I need to pass that code to other activity. That's why I have this code:
if (!RegisterActivity.this.isProcessing() && barcodes.size() > 0) {
barcodeDetector.release();
RegisterActivity.this.setProcessing(true);
Intent intent = new Intent(RegisterActivity.this, ResultActivity.class);
intent.putExtra("code", codigo);
intent.putExtra("data", barcodes.valueAt(0));
setResult(CommonStatusCodes.SUCCESS, intent);
startActivity(intent);
RegisterActivity.this.setProcessing(false);
finish();
}
When I run the application, this exception is shown in the logcat:
2020-08-27 18:19:53.894 11076-11746/com.company.myapp E/CameraSource: Exception thrown from receiver.
java.lang.IllegalStateException: Detector processor must first be set with setProcessor in order to receive detection results.
at com.google.android.gms.vision.Detector.receiveFrame(com.google.android.gms:play-services-vision-common@@19.1.2:17)
at com.google.android.gms.vision.CameraSource$zza.run(com.google.android.gms:play-services-vision-common@@19.1.2:47)
at java.lang.Thread.run(Thread.java:929)
2020-08-27 18:19:53.956 11076-11746/com.company.myapp E/CameraSource: Exception thrown from receiver.
java.lang.IllegalStateException: Detector processor must first be set with setProcessor in order to receive detection results.
at com.google.android.gms.vision.Detector.receiveFrame(com.google.android.gms:play-services-vision-common@@19.1.2:17)
at com.google.android.gms.vision.CameraSource$zza.run(com.google.android.gms:play-services-vision-common@@19.1.2:47)
at java.lang.Thread.run(Thread.java:929)
The application works because the new activity is opened and I can receive the read code there, but, if I set a breakpoint in that activity and start debugging, debugger session ends with no more errors after a few seconds.
What's wrong with that code?
Debugging sometimes changes the behavior of your code. Especially when your framework works with a lot of threads like android.
From the stacktrace at java.lang.Thread.run(Thread.java:929)
I would say, that some code, in a thread, which is currently not under debugging wants to use the Decoder, before your call of setProcessor.
In Java only the thread which is under debugging will stop at a breakpoint other threads are not touched and won't be stopped when a breakpoint is reached.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With