gpt4 book ai didi

android - 必须先用 setProcessor 设置检测器处理器才能接收检测结果

转载 作者:行者123 更新时间:2023-12-04 11:40:17 29 4
gpt4 key购买 nike

我正在使用 Mobile Vision API 来扫描二维码。
我在activity中有这个方法使用摄像头捕捉代码:

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();
}

}
});
}
}
当检测到并读取代码时,我需要将该代码传递给其他 Activity 。这就是为什么我有这个代码:
                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();
}
当我运行应用程序时,这个异常显示在 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)
该应用程序工作正常,因为新 Activity 已打开,我可以在那里接收读取的代码,但是,如果我在该 Activity 中设置断点并开始调试,调试器 session 将在几秒钟后结束,不再出现错误。
那个代码有什么问题?

最佳答案

调试有时会改变代码的行为。尤其是当您的框架与许多线程(如 android)一起工作时。
来自堆栈跟踪 at java.lang.Thread.run(Thread.java:929)我想说,在调用 setProcessor 之前,线程中的某些代码(当前未处于调试状态)想要使用解码器。
在 Java 中,只有正在调试的线程会在断点处停止,其他线程不会被触及,并且在到达断点时也不会停止。

关于android - 必须先用 setProcessor 设置检测器处理器才能接收检测结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63625096/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com