12

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 fullscreen camera but with a limited scanning window. For example, the surface view for the camera needs to be fullscreen, it has 2 transparent overlays set to 35% of the screen height top and bottom leaving a 30% viewport in the center.

I have changed the graphic overlay so it will only display in the middle viewport but havent been able to work out how to limit the barcode tracker to the same area.

Any ideas?

devchimp
  • 754
  • 6
  • 18
  • could you please share your code on how you use your transparent overlays, please? Do you display a rectangle square? if so, how you do that... I am banging my head against the wall to figure it out... I greatly appreciate if you could help me with this. – Vincy Apr 16 '17 at 22:43

2 Answers2

11

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:

public class CentralBarcodeFocusingProcessor extends FocusingProcessor<Barcode> {

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

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

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

   BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
   barcodeDetector.setProcessor(
                new CentralBarcodeFocusingProcessor(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:

class MyDetector extends Detector<Barcode> {
  private Detector<Barcode> mDelegate;

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

  public SparseArray<Barcode> detect(Frame frame) {
    // *** crop the frame here
    return 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:

BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context)
        .build();
MyDetector myDetector = new MyDetector(barcodeDetector);

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

mCameraSource = new CameraSource.Builder(context, myDetector)
        .build();
pm0733464
  • 2,862
  • 14
  • 16
  • In the end I came to the same conclusion but tried to crop the image before inserting it into the output frame. The issue I had then was converting the YUV to the bitmap. In the end I ran out of time and found the dm77 barcode scanner more agreeable to customization. Thanks for the input ^^ – devchimp Apr 07 '16 at 10:47
  • I added an alternative approach above, where you can just filter out detection results that are not in the central area of the image. This is easier than cropping the image, although it still will scan the entire image. – pm0733464 Apr 12 '16 at 14:16
  • 3
    How do you determine where the barcode is from inside the CentralFocusingProcessor? – JPM Jul 25 '16 at 19:50
  • 2
    @pm0733464 Do you have any sample code ? we are missing this code. if (/* barcode in central region */) { return id; } – Android Developer World Jun 13 '17 at 15:09
  • 2
    Any update on how can the barcode in Central region be identified? – Snake Mar 27 '18 at 21:04
  • 1
    @pm0733464 Can you please provide a sample app with above code. It will be very helpful. Thanks – Nishant May 07 '20 at 07:01
  • What about the second parameter in the ```CentralBarcodeFocusingProcessor``` constructor? – Andrei Manolache May 10 '21 at 22:36
7

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<Barcode> {

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

    @Override
    public int selectFocus(Detector.Detections<Barcode> detections) {

        SparseArray<Barcode> 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;
    }
}