如何通过zxing-android-embedded in Android停止连续扫描

How to stop continuous scanning by zxing-android-embedded in Android

我正在使用 Zxing-android-embedded(https://github.com/journeyapps/zxing-android-embedded) 扫描二维码。我已经从 github 导入了库。当应用程序启动时,只要将相机放在条形码上,相机就会重复扫描代码。我想停止扫描(但不是相机预览) 一旦条形码检测到并显示一个带有 "Confirm" 按钮、"Cancel" 按钮和一个输入框的对话框。当用户按下 "Confirm" 或 "Cancel" 按钮时,它应该会再次开始扫描。

我在暂停相机预览的 decode() 方法的开头调用了 barcodeView.pause();。此外,在 "dialogConfirmClick" 和 "dialogCancelClick" 的 onClick 方法中添加了 barcodeView.resume();。但是 barcodeView.pause(); 方法会暂停扫描以及相机预览。

这是我的 class-

public class MyScanActivity extends Activity {
    private static final String TAG = MyScanActivity.class.getSimpleName();

    private CompoundBarcodeView barcodeView;
    private BeepManager beepManager;
    private DialogInterface.OnClickListener dialogCanselClick;
    private AlertDialog dialog;

    private BarcodeCallback callback = new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {
            if (result.getText() != null) {
                handleDecode(result);
            }
        }

        @Override
        public void possibleResultPoints(List<ResultPoint> resultPoints) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.continuous_scan);
        barcodeView = (CompoundBarcodeView) findViewById(R.id.barcode_scanner);
        barcodeView.decodeContinuous(callback);
        beepManager = new BeepManager(this);
        dialogCancelClick = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                barcodeView.resume();//Resume scanning 
                dialog.dismiss();
            }
        };
    }

    public void handleDecode(BarcodeResult rawResult) {
        barcodeView.pause();//Pause preview
        String result = rawResult.getText();

        beepManager.playBeepSoundAndVibrate();

        DialogInterface.OnClickListener dialogOkClick = new DialogInterface.OnClickListener() { // OK
            // button
            @Override
            public void onClick(DialogInterface dialog, int which) {

                if (writeNote) {
                    EditText txtNote = (EditText) promptsView.findViewById(R.id.txt_dialog_note);
                    //code to merge value of txtNote with result
                }

                dialog.dismiss();
                barcodeView.resume();//Resume scanning after pressing confirm button
                Toast.makeText(MyScanActivity.this, R.string.dialog_save_qr_alert, Toast.LENGTH_SHORT).show();
            }
        };

        AlertDialog dialog = DialogHelper.CreateDialog(this, DialogHelper.SAVE_QR_CODE, result, dialogOkClick, dialogCancelClick, promptsView);
        dialog.show();
    } 

    @Override
    protected void onResume() {
        super.onResume();

        barcodeView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();

        barcodeView.pause();
    }

    public void pause(View view) {
        barcodeView.pause();
    }

    public void resume(View view) {
        barcodeView.resume();
    }

    public void triggerScan(View view) {
        barcodeView.decodeSingle(callback);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }
}

我找到了解决办法。我发布这个是为了帮助其他可能有同样疑问的人。

不要在 onCreate 方法中使用 barcodeView.decodeContinuous(callback);,而是使用 barcodeView.decodeSingle(callback);。一旦找到二维码,它将停止扫描。在 ConfirmCancel 按钮中调用 barcodeView.decodeSingle(callback); 以再次激活扫描。