I\'m trying to implement Google Visions scanner into an app im working on. By default its a full screen activity and barcodes are tracked over the entire screen.
How
Based on @pm0733464's answer with an example of how to get the barcode that's nearest to the center of the preview.
public class CentralBarcodeFocusingProcessor extends FocusingProcessor {
public CentralBarcodeFocusingProcessor(Detector detector, Tracker tracker) {
super(detector, tracker);
}
@Override
public int selectFocus(Detector.Detections detections) {
SparseArray barcodes = detections.getDetectedItems();
Frame.Metadata meta = detections.getFrameMetadata();
double nearestDistance = Double.MAX_VALUE;
int id = -1;
for (int i = 0; i < barcodes.size(); ++i) {
int tempId = barcodes.keyAt(i);
Barcode barcode = barcodes.get(tempId);
float dx = Math.abs((meta.getWidth() / 2) - barcode.getBoundingBox().centerX());
float dy = Math.abs((meta.getHeight() / 2) - barcode.getBoundingBox().centerY());
double distanceFromCenter = Math.sqrt((dx * dx) + (dy * dy));
if (distanceFromCenter < nearestDistance) {
id = tempId;
nearestDistance = distanceFromCenter;
}
}
return id;
}
}