Hi I checked the barcode reader sample from google on github , I am trying to just make the barcodedetector detect the first barcode (only one) and when it does it send the decoded barcode to another activity. Mabye I am wrong but I need to put this 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 is null");
}
}
else {
Log.d(TAG,"no barcode detected");
}
return barcode != null;
}
somewhere in this one so that the barcode is captured automatically without that I need to tap when barcode graphic appears.I also figured that I dont need the MultiProcessor.Builder<>
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay);
barcodeDetector.setProcessor(
new MultiProcessor.Builder<>(barcodeFactory).build());
I was after the same outcome. Here is how i accomplished it.
Add a listener to 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 is fired whenever the create method is called when a new barcode is detected.
Next, from the BarcodeCaptureActivity
, under the createCameraSource
method attach a new listener and send the barcode wherever you'd like it.
private void createCameraSource(boolean autoFocus, boolean useFlash) {
Context context = getApplicationContext();
// A barcode detector is created to track barcodes. An associated multi-processor instance
// is set to receive the barcode detection results, track the barcodes, and maintain
// graphics for each barcode on screen. The factory is used by the multi-processor to
// create a separate tracker instance for 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 that helps!