Search code examples
androidbarcode-scannerandroid-vision

How to Disable multiple barcode detection android Vision API


I'm trying to disable multiple Barcode detection at time. How to disable MultiProcessor using Google Vision API, I couldn't find any solution from official site here

I have downloaded sample from here

Code

BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
    BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay);
    barcodeDetector.setProcessor(
            new MultiProcessor.Builder<>(barcodeFactory).build());

Even if remove below line, I cannot detect at all.

 barcodeDetector.setProcessor(
        new MultiProcessor.Builder<>(barcodeFactory).build());

Solution

  • I have resolved this multiple Barcode detection at same time by exploring sample code and some logic implementation.

    Solution is : stop mPreview and start again after 1 second delay

    Create callback listener

    public interface BarCodeDetectListener {
    void onBarCodeDetect(Barcode barcode);
    }
    

    BarcodeGraphicTracker

    When device detected Barcode inside onNewItem() callback to Barcodefragment

    class BarcodeGraphicTracker extends Tracker<Barcode> {
    ...
    ...
    private BarcodeGraphic mGraphic;
    private BarCodeDetectListener barCodeDetectListener;
    ...
    
     /**
      * Start tracking the detected item instance within the item overlay.
      */
     @Override
     public void onNewItem(int id, Barcode item) {
        mGraphic.setId(id);
        barCodeDetectListener.onBarCodeDetect(item);
     }
    }
    

    BarCodeFragment

    @SuppressLint("NewApi")
    public class SurveyBarCodeFragment extends Fragment implements BarCodeDetectListener {
      ...
      private CameraSourcePreview mPreview;
      private CameraSource mCameraSource;
      ...
    
    
    
     @Override
     public void onBarCodeDetect(final Barcode barcode) {
      new Handler().post(new Runnable() {
            @Override
            public void run() {
                mPreview.stop(); //Stop preview
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (ActivityCompat.checkSelfPermission(getApplicationContext(), 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;
                            }
                            //Start preview after 1s delay
                            mPreview.start(mCameraSource);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }, 1000);
            }
        });
    
     }
    }