如何通过Android中嵌入的zxing-android停止连续扫描 [英] How to stop continuous scanning by zxing-android-embedded in Android

查看:87
本文介绍了如何通过Android中嵌入的zxing-android停止连续扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Zxing-android-embedded (( https://github.com/journeyapps/zxing-android-embedded )来扫描QR码.我已经从github导入了库.应用启动后,只要将相机放在条形码上,相机就会重复扫描代码.我要在检测到条形码并显示后立即停止扫描(而不是相机预览)一个带有确认"按钮,取消"按钮和一个输入框的对话框.当用户按下确认"或取消"按钮时,它将再次开始扫描.

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.

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

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.

这是我的课程-

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

推荐答案

我已经找到了解决方案.我发布此信息是为了对其他有相同疑问的人有所帮助.

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

onCreate 方法内使用 barcodeView.decodeSingle(callback); ,而不是使用 barcodeView.decodeContinuous(callback); .一旦找到QR码,它将停止扫描.调用确认取消按钮中的 barcodeView.decodeSingle(callback); 再次激活扫描.

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.

这篇关于如何通过Android中嵌入的zxing-android停止连续扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆