条形码扫描仪结果两次

时间:2017-11-05 11:36:40

标签: java android barcode barcode-scanner

你好,我试着开发一个应用程序,我应该能够在主要活动中扫描,并有意图我得到一个文本框中的结果。我唯一的问题是,当我扫描条形码有时它打开结果活动2-3次..也许有人可以帮助我,我不知道该怎么做

我不知道我应该如何帮助你

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

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new  String[]{Manifest.permission.INTERNET}, 200);
    }


    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, 200);
    }


    cameraPreview = (SurfaceView) findViewById(R.id.cameraPreview);
    cameraPreview.setZOrderMediaOverlay(true);

    barcodeDetector = new BarcodeDetector.Builder(this)
            .setBarcodeFormats(Barcode.ALL_FORMATS)
            .build();
    cameraSource = new CameraSource
            .Builder(this, barcodeDetector)
            .setFacing(CameraSource.CAMERA_FACING_BACK)
            .setRequestedFps(24)
            .setRequestedPreviewSize(1600, 1024)
            .setAutoFocusEnabled(true)
            .build();
    cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try{
                if(ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
                    cameraSource.start(cameraPreview.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> barcodes = detections.getDetectedItems();
             if (barcodes.size() != 0)
             {
                 Intent intent = new Intent(MainActivity.this, finish.class);
                 intent.putExtra("barcode", barcodes.valueAt(0));
                 startActivityForResult(intent, 100);
                 finish();

             }

             }

    });

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

完成活动

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.finish);
    result = (TextView) findViewById(R.id.editText1);
    final Barcode barcode = getIntent().getParcelableExtra("barcode");
    result.setText(barcode.displayValue);

2 个答案:

答案 0 :(得分:0)

您可能不希望在onDesre中设置onCreate和teardown。如果这样,即使您的活动已移至后台,相机仍将处于活动状态并进行扫描。请改用onResume和onPause。

您可能遇到的另一个问题是,每次检测到条形码时都会调用receiveDetections(即使它是相同的条形码)。如果条形码检测器可以比系统启动活动更快地检测到条形码,则在释放相机和条形码检测器之前将多次调用回调。为防止这种情况发生,您可以使用标记来防止startActivity被多次调用,或者您可以从receiveDetections内部释放条形码检测器(和相机)

答案 1 :(得分:0)

您所能做的就是在收到条形码数据后立即释放条形码检测器。

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

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcode = detections.getDetectedItems();
            if (barcode.size() != 0) {
                barcodeDetector.release();
                //......put your code here.....//
            }
        }
    });