Skip to content Skip to sidebar Skip to footer

Android Vision - Reduce Bar Code Tracking Window

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. However, I need a f

Solution 1:

The current API doesn't provide a way to limit the scan area. However, you could either filter the results coming out of the detector or crop the image that is passed into the detector.

Filter Results Approach

With this approach, the barcode detector would still scan the full image area, but detected barcodes outside of the target region would be ignored. One way of doing this is to implement a "focusing processor" that receives the results from the detector and only passes at most one barcode to your associated tracker. For example:

publicclassCentralBarcodeFocusingProcessorextendsFocusingProcessor<Barcode> {

  publicCentralBarcodeFocusingProcessor(Detector<Barcode> detector, Tracker<Barcode> tracker) {
    super(detector, tracker);
  }

  @OverridepublicintselectFocus(Detections<Barcode> detections) {
    SparseArray<Barcode> barcodes = detections.getDetectedItems();
    for (inti=0; i < barcodes.size(); ++i) {
      intid= barcodes.keyAt(i);
      if (/* barcode in central region */) {
        return id;
      }
    }
    return -1;
  }
}

You'd then associate this processor with the detector like this:

BarcodeDetectorbarcodeDetector=newBarcodeDetector.Builder(context).build();
   barcodeDetector.setProcessor(
                newCentralBarcodeFocusingProcessor(myTracker));

Cropping Images Approach

You'd need to crop the image yourself first, before the detector is called. This could be done by implementing a Detector subclass which wraps the barcode detector, crops the images received, and calls the barcode scanner with the cropped images.

For example, you'd make a detector to intercept and crop the image like this:

classMyDetectorextendsDetector<Barcode> {
  private Detector<Barcode> mDelegate;

  MyDetector(Detector<Barcode> delegate) {
    mDelegate = delegate;
  }

  public SparseArray<Barcode> detect(Frame frame) {
    // *** crop the frame herereturn mDelegate.detect(croppedFrame);
  }

  public boolean isOperational() {
    return mDelegate.isOperational();
  }

  public boolean setFocus(int id) {
    return mDelegate.setFocus(id);
  }
} 

You'd wrap the barcode detector with this one, putting it in between the camera source and the barcode detector:

BarcodeDetectorbarcodeDetector=newBarcodeDetector.Builder(context)
        .build();
MyDetectormyDetector=newMyDetector(barcodeDetector);

myDetector.setProcessor(/* include your processor here */);

mCameraSource = newCameraSource.Builder(context, myDetector)
        .build();

Solution 2:

Based on @pm0733464's answer with an example of how to get the barcode that's nearest to the center of the preview.

publicclassCentralBarcodeFocusingProcessorextendsFocusingProcessor<Barcode> {

    publicCentralBarcodeFocusingProcessor(Detector<Barcode> detector, Tracker<Barcode> tracker) {
        super(detector, tracker);
    }

    @OverridepublicintselectFocus(Detector.Detections<Barcode> detections) {

        SparseArray<Barcode> barcodes = detections.getDetectedItems();
        Frame.Metadatameta= detections.getFrameMetadata();
        doublenearestDistance= Double.MAX_VALUE;
        intid= -1;

        for (inti=0; i < barcodes.size(); ++i) {
            inttempId= barcodes.keyAt(i);
            Barcodebarcode= barcodes.get(tempId);
            floatdx= Math.abs((meta.getWidth() / 2) - barcode.getBoundingBox().centerX());
            floatdy= Math.abs((meta.getHeight() / 2) - barcode.getBoundingBox().centerY());

            doubledistanceFromCenter=  Math.sqrt((dx * dx) + (dy * dy));

            if (distanceFromCenter < nearestDistance) {
                id = tempId;
                nearestDistance = distanceFromCenter;
            }
        }
        return id;
    }
}

Post a Comment for "Android Vision - Reduce Bar Code Tracking Window"