How to Disable multiple barcode detection android Vision API
Asked Answered
C

2

9

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());
Conflict answered 8/2, 2017 at 11:58 Comment(2)
I think You might want this, #33000313Irrespective
No. please read what I'm asking.Conflict
C
1

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

 }
}
Conflict answered 11/2, 2017 at 13:45 Comment(0)
O
1

Update your BarcodeGraphicTracker and implement onNewItem function as below:

public class BarcodeGraphicTracker extends Tracker<Barcode> {

        private static final String TAG = "Barcode-reader";
        @Override
        public void onNewItem(int id, final Barcode item) {
            Log.d(TAG, "Detected!!"+ item.toString());
                Runnable r=new Runnable()
                {
                    public void run()
                    {
                        //write your code here
                        }
                    }
                };
                getActivity().runOnUiThread(r);
        }

        @Override
        public void onUpdate(Detector.Detections<Barcode> detectionResults, Barcode item) {

        }
    }

and BarcodeTrackerFactory as:

public class BarcodeTrackerFactory implements MultiProcessor.Factory<Barcode> {

        @Override
        public Tracker<Barcode> create(Barcode barcode) {
            return new BarcodeGraphicTracker();
        }

    }
Ozzy answered 8/2, 2017 at 12:7 Comment(3)
What about GraphicOverlay?. I think it will not work without GraphicOverlayConflict
Keep the entire code as it was in the sample code. Just cut the code from onUpdate and paste it in onNewItem and then try.Ozzy
Doesn't detect anything!!Conflict
C
1

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

 }
}
Conflict answered 11/2, 2017 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.