如何防止条码扫描器重复相同的结果?

时间:2018-03-02 20:26:57

标签: barcode google-vision android-vision

我正在开发一个广泛使用条形码扫描仪的项目。我使用谷歌视觉条码扫描仪并成功实施。但是,每当我尝试扫描条形码时,apk会重复相同的结果一次以上。提前谢谢。

createCameraSource()

    private void createCameraSource() {
    final BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.EAN_13 | Barcode.EAN_8)
            .build();
    final CameraSource cameraSource = new CameraSource
            .Builder(this, barcodeDetector)
            .setAutoFocusEnabled(true)
            .setRequestedPreviewSize(1600, 1024)
            .build();

    this.cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if (ActivityCompat.checkSelfPermission(Entrance.this, 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;
            }
            try {
                cameraSource.start(cameraView.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 BarcodeDetector.Processor<Barcode>() {
        @Override
        public void release() {
        }

        @Override
        public void receiveDetections(BarcodeDetector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcodes = detections.getDetectedItems();
            if (barcodes.size() > 0) {

                edx.post(new Runnable() {    // Use the post method of the TextView
                    public void run() {
                        edx.setText(barcodes.valueAt(0).displayValue);

                   }

          }
    });
}

0 个答案:

没有答案