Search code examples
javaandroidbarcodebarcode-scanner

barcode scanner result twice


hello i try to develop an app which i should be able to scan in main activity and with intent i get the result in a textbox. My only problem is that when i scan the barcode SOMETIMES it opens the result activity 2-3 times.. Maybe someone could help me i dont know what to do

i dont know how i should help you

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

FINISH ACTIVITY

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

Solution

  • You probably don't want to setup in onCreate and teardown in onDestroy. If you do, the camera will still be active and scanning even after your activity has moved to the background. Use onResume and onPause instead.

    The other issue you may be experiencing is that receiveDetections is called every time it detects a barcode (even if it's the same barcode). If the barcode detector can detect barcodes faster than the system can start an activity, the callback will be called multiple times before you release the camera and barcode detector. To prevent this, you can use a flag to prevent startActivity from being called more than once or you can release the barcode detector (and camera) from inside receiveDetections