How to stop continuous scanning by zxing-android-e

2019-04-29 17:35发布

I am using Zxing-android-embedded(https://github.com/journeyapps/zxing-android-embedded) to scan QR codes. I have imported the library from github . When the app starts , the camera is scanning the code repeatedly as long as the camera is placed on a barcode.I want to stop the scanning(but not the camera preview) once the barcode is detected and show a dialog box with "Confirm" button, "Cancel" button, and a input box. When user press the "Confirm" or "Cancel" button it should start scanning again.

I have called barcodeView.pause(); at the beginning of the decode() method which pauses the camera preview. Also, added barcodeView.resume(); inside onClick method of "dialogConfirmClick" and "dialogCancelClick". But barcodeView.pause(); method pauses scanning as well as the camera preview.

Here is my 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);
    }
}

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-29 18:25

I have got the solution. I am posting this so that this may help others who may have same doubts.

Instead of using barcodeView.decodeContinuous(callback); inside onCreate method use barcodeView.decodeSingle(callback);. Once a QR code is found it will stop scanning. Call barcodeView.decodeSingle(callback); inside Confirm and Cancel button to activate scan again.

查看更多
登录 后发表回答