I've been playing with the sample code from the new Google Barcode API. It overlays a box and the barcode value over the live camera feed of a barcode. (Also faces)
I can't tell how to return a barcode value to my app. A) How to tell when a detection event has occurred and B) how to access the ravValue for use in other parts of my app. Can anyone help with this?
https://developers.google.com/vision/multi-tracker-tutorial
https://github.com/googlesamples/android-vision
UPDATE: Building on @pm0733464's answer, I added a callback interface (called onFound) to the Tracker class that I could access in the Activity. Adapting the Google multi-tracker sample:
GraphicTracker:
class GraphicTracker<T> extends Tracker<T> {
private GraphicOverlay mOverlay;
private TrackedGraphic<T> mGraphic;
private Callback mCallback;
GraphicTracker(GraphicOverlay overlay, TrackedGraphic<T> graphic, Callback callback) {
mOverlay = overlay;
mGraphic = graphic;
mCallback = callback;
}
public interface Callback {
void onFound(String barcodeValue);
}
@Override
public void onUpdate(Detector.Detections<T> detectionResults, T item) {
mCallback.onFound(((Barcode) item).rawValue);
mOverlay.add(mGraphic);
mGraphic.updateItem(item);
}
BarcodeTrackerFactory :
class BarcodeTrackerFactory implements MultiProcessor.Factory<Barcode> {
private GraphicOverlay mGraphicOverlay;
private GraphicTracker.Callback mCallback;
BarcodeTrackerFactory(GraphicOverlay graphicOverlay, GraphicTracker.Callback callback) {
mGraphicOverlay = graphicOverlay;
mCallback = callback;
}
@Override
public Tracker<Barcode> create(Barcode barcode) {
BarcodeGraphic graphic = new BarcodeGraphic(mGraphicOverlay);
return new GraphicTracker<>(mGraphicOverlay, graphic, mCallback);
}
}
Main Activity:
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay, new GraphicTracker.Callback() {
@Override
public void onFound(String barcodeValue) {
Log.d(TAG, "Barcode in Multitracker = " + barcodeValue);
}
});
MultiProcessor<Barcode> barcodeMultiProcessor = new MultiProcessor.Builder<>(barcodeFactory).build();
barcodeDetector.setProcessor(barcodeMultiProcessor);