条形码检测器多次读取QR码

时间:2017-11-29 07:25:25

标签: android qr-code barcode

我正在尝试为我的应用创建条形码阅读器,它工作正常,但问题是当我一直专注于QR码时,它会继续阅读并永不停止! 我怎么能只让这一次发生? 在这段代码中,它继续" Toast"直到我关闭应用程序,当我使用Intent关闭当前活动并打开另一个活动时,活动会打开几次! 这是代码:

public class BarcodeCaptureActivity extends AppCompatActivity {

private SurfaceView camera_preview;
TextView text;
BarcodeDetector barcodeDetector;
CameraSource cameraSource;
final int requestCameraPermissionID = 1001;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_barcode_capture);

    camera_preview = (SurfaceView) findViewById(R.id.camera_preview);
    text = (TextView) findViewById(R.id.text);

    barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
    cameraSource = new CameraSource.Builder(this, barcodeDetector).setRequestedPreviewSize(640, 480).build();

    camera_preview.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(BarcodeCaptureActivity.this, new String[]{Manifest.permission.CAMERA},requestCameraPermissionID);
                return;
            }
            try {
                cameraSource.start(camera_preview.getHolder());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            cameraSource.stop();
        }
    });

    barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {

        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> qrcode = detections.getDetectedItems();
            if (qrcode.size() != 0){
                text.post(new Runnable() {
                    @Override
                    public void run() {
                        Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(1000);
                        text.setText(qrcode.valueAt(0).displayValue);
                        String title = text.getText().toString();
                        Toast.makeText(BarcodeCaptureActivity.this, "" + title, Toast.LENGTH_SHORT).show();
                        finish();
                    }
                });

            }
        }
    });

}

感谢您的帮助...

更新

添加onDestroy和onPause方法解决了问题...

@Override
protected void onPause() {
    super.onPause();
    cameraSource.stop();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    cameraSource.stop();
}

0 个答案:

没有答案