我Barcode
通过探索示例代码和一些逻辑实现同时解决了这种多重检测。
解决方案是:停止mPreview
并在 1 秒后重新启动delay
创建回调监听器
public interface BarCodeDetectListener {
void onBarCodeDetect(Barcode barcode);
}
条码图形跟踪器
当设备检测到Barcode
内部onNewItem()
回调时Barcodefragment
class BarcodeGraphicTracker extends Tracker<Barcode> {
...
...
private BarcodeGraphic mGraphic;
private BarCodeDetectListener barCodeDetectListener;
...
/**
* Start tracking the detected item instance within the item overlay.
*/
@Override
public void onNewItem(int id, Barcode item) {
mGraphic.setId(id);
barCodeDetectListener.onBarCodeDetect(item);
}
}
条码片段
@SuppressLint("NewApi")
public class SurveyBarCodeFragment extends Fragment implements BarCodeDetectListener {
...
private CameraSourcePreview mPreview;
private CameraSource mCameraSource;
...
@Override
public void onBarCodeDetect(final Barcode barcode) {
new Handler().post(new Runnable() {
@Override
public void run() {
mPreview.stop(); //Stop preview
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
//Start preview after 1s delay
mPreview.start(mCameraSource);
} catch (IOException e) {
e.printStackTrace();
}
}
}, 1000);
}
});
}
}