当我 return 到 activity 时,如何重新启动 BarcodeDetector 以获得新结果?
How can I restart BarcodeDetector for new result when I return to the activity?
我有一个 QR 扫描器 activity,它会在成功扫描代码后移动到下一个 activity。但是,我希望有一个后退按钮 return 到 activity (如果这个结果可能不正确)。然而,当我 return 到 activity 时,结果仍然存储并且扫描器没有扫描代码。
如何重启条码检测器?我应该覆盖 onPause/onResume 吗?下面是我到目前为止的代码。
private void setupBarcodeDetector() {
barcodeDetector =
new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
cameraSource =
new CameraSource.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
surfaceView
.getHolder()
.addCallback(
new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(
getApplicationContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
try {
cameraSource.start(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(
SurfaceHolder holder, int format, int width, int height) {
// LEAVE EMPTY
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(
new Detector.Processor<Barcode>() {
@Override
public void release() {
// LEAVE EMPTY
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> qrCodes = detections.getDetectedItems();
if (qrCodes.size() != 0) {
barcodeDetector.release();
resultQRtv.post(
new Runnable() {
@Override
public void run() {
Vibrator vibrator =
(Vibrator)
getApplicationContext()
.getSystemService(
Context
.VIBRATOR_SERVICE);
vibrator.vibrate(500);
successPrompt.setVisibility(View.VISIBLE);
allGoodTv.setVisibility(View.VISIBLE);
// Loading animation - to be changed with specific
// animation
if (dialog == null) {
dialog =
new ProgressDialog(
QRCodeScannerActivity.this);
dialog.setCancelable(false);
dialog.setMessage("Registering...");
}
dialog.show();
Handler handler = new Handler();
handler.postDelayed(
new Runnable() {
@Override
public void run() {
// On successful scan, go to the required
// activity
Intent
goToAdminAccountConfirmationActivity =
new Intent(
QRCodeScannerActivity
.this,
AdminAccountConfirmSettings
.class);
startActivity(
goToAdminAccountConfirmationActivity);
// Remove the prompts in case the user
// returns to this activity
dialog.dismiss();
successPrompt.setVisibility(View.GONE);
allGoodTv.setVisibility(View.GONE);
}
},
1500);
}
});
}
}
});
} // setupBarcodeDetector
在您的情况下,在您的活动的 onResume() 方法中调用 setupBarcodeDetector() 方法。
为了连击优化,在 onCreate() 方法中进行初始化,并在 onPause() 和 onResume() 方法中开始和停止扫描。
我有一个 QR 扫描器 activity,它会在成功扫描代码后移动到下一个 activity。但是,我希望有一个后退按钮 return 到 activity (如果这个结果可能不正确)。然而,当我 return 到 activity 时,结果仍然存储并且扫描器没有扫描代码。
如何重启条码检测器?我应该覆盖 onPause/onResume 吗?下面是我到目前为止的代码。
private void setupBarcodeDetector() {
barcodeDetector =
new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
cameraSource =
new CameraSource.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
surfaceView
.getHolder()
.addCallback(
new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(
getApplicationContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
try {
cameraSource.start(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(
SurfaceHolder holder, int format, int width, int height) {
// LEAVE EMPTY
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(
new Detector.Processor<Barcode>() {
@Override
public void release() {
// LEAVE EMPTY
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> qrCodes = detections.getDetectedItems();
if (qrCodes.size() != 0) {
barcodeDetector.release();
resultQRtv.post(
new Runnable() {
@Override
public void run() {
Vibrator vibrator =
(Vibrator)
getApplicationContext()
.getSystemService(
Context
.VIBRATOR_SERVICE);
vibrator.vibrate(500);
successPrompt.setVisibility(View.VISIBLE);
allGoodTv.setVisibility(View.VISIBLE);
// Loading animation - to be changed with specific
// animation
if (dialog == null) {
dialog =
new ProgressDialog(
QRCodeScannerActivity.this);
dialog.setCancelable(false);
dialog.setMessage("Registering...");
}
dialog.show();
Handler handler = new Handler();
handler.postDelayed(
new Runnable() {
@Override
public void run() {
// On successful scan, go to the required
// activity
Intent
goToAdminAccountConfirmationActivity =
new Intent(
QRCodeScannerActivity
.this,
AdminAccountConfirmSettings
.class);
startActivity(
goToAdminAccountConfirmationActivity);
// Remove the prompts in case the user
// returns to this activity
dialog.dismiss();
successPrompt.setVisibility(View.GONE);
allGoodTv.setVisibility(View.GONE);
}
},
1500);
}
});
}
}
});
} // setupBarcodeDetector
在您的情况下,在您的活动的 onResume() 方法中调用 setupBarcodeDetector() 方法。 为了连击优化,在 onCreate() 方法中进行初始化,并在 onPause() 和 onResume() 方法中开始和停止扫描。