How to disable the detection of multiple Android Vision API barcodes

I am trying to disable Barcode multiple detection. How to disable MultiProcessor using the Google Vision API , I could not find a solution from the official site here

I downloaded a sample from here

The code

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

Even if I delete the line below, I can’t detect at all.

  barcodeDetector.setProcessor( new MultiProcessor.Builder<>(barcodeFactory).build()); 
+6
source share
2 answers

I resolved this multiple detection of Barcode at the same time, exploring the sample code and some logical implementation.

Solution : stop mPreview and start again after 1 second delay

Create callback listener

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

BarcodeGraphicTracker

When a Barcode device is detected inside the onNewItem() callback on 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); } }); } } 
0
source

Update your BarcodeGraphicTracker and implement the onNewItem function as shown 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:

 public class BarcodeTrackerFactory implements MultiProcessor.Factory<Barcode> { @Override public Tracker<Barcode> create(Barcode barcode) { return new BarcodeGraphicTracker(); } } 
+1
source

Source: https://habr.com/ru/post/1014797/


All Articles