android - use google mobile vision api to detect the first barcode and send the data to another activity -


hi checked barcode reader sample google on github , trying make barcodedetector detect first barcode (only one) , when send decoded barcode activity. mabye wrong need put code

barcodegraphic graphic = mgraphicoverlay.getfirstgraphic();     barcode barcode = null;     if (graphic != null) {         barcode = graphic.getbarcode();         if (barcode != null) {             intent data = new intent();             data.putextra(barcodeobject, barcode);             setresult(commonstatuscodes.success, data);             finish();         }         else {             log.d(tag, "barcode data null");         }     }     else {         log.d(tag,"no barcode detected");     }     return barcode != null; } 

somewhere in 1 barcode captured automatically without need tap when barcode graphic appears.i figured dont need multiprocessor.builder<>

barcodedetector barcodedetector = new barcodedetector.builder(context).build();     barcodetrackerfactory barcodefactory = new barcodetrackerfactory(mgraphicoverlay);     barcodedetector.setprocessor(             new multiprocessor.builder<>(barcodefactory).build()); 

i after same outcome. here how accomplished it.

add listener barcodetracker:

class barcodetrackerfactory implements multiprocessor.factory<barcode> { private graphicoverlay<barcodegraphic> mgraphicoverlay;  private onnewbarcodelistener newbarcodelistener;  barcodetrackerfactory(graphicoverlay<barcodegraphic> barcodegraphicoverlay) {     mgraphicoverlay = barcodegraphicoverlay; }  @override public tracker<barcode> create(barcode barcode) {     barcodegraphic graphic = new barcodegraphic(mgraphicoverlay);     newbarcodelistener.onnewitem(barcode);     return new barcodegraphictracker(mgraphicoverlay, graphic); }  public interface onnewbarcodelistener {     void onnewitem(barcode item); }  public void setonnewbarcodelistener(onnewbarcodelistener newbarcodelistener) {     this.newbarcodelistener = newbarcodelistener; }  } 

this listener fired whenever create method called when new barcode detected.

next, barcodecaptureactivity, under createcamerasource method attach new listener , send barcode wherever you'd it.

private void createcamerasource(boolean autofocus, boolean useflash) {     context context = getapplicationcontext();      // barcode detector created track barcodes.  associated multi-processor instance     // set receive barcode detection results, track barcodes, , maintain     // graphics each barcode on screen.  factory used multi-processor     // create separate tracker instance each barcode.     barcodedetector barcodedetector = new barcodedetector.builder(context).build();     barcodetrackerfactory barcodefactory = new barcodetrackerfactory(mgraphicoverlay);      barcodefactory.setonnewbarcodelistener(new barcodetrackerfactory.onnewbarcodelistener() {         @override         public void onnewitem(barcode item) {             log.d("barcodefound", "found new barcode! " + item.rawvalue);             intent intent = new intent(this, dosomethingwithbarcodeactivity.class);             intent.putextra("barcode", item.rawvalue);             startactivity(intent);         }     }); ... 

hope helps!


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -