Vision API Barcode – Restrict the detection area to center rect of screen

I am implementing barcode scanner in my application. I would like to limit my detection area. followed the below logic but i doesn’t work properly in some of the devices.

//Trying to crop the center portion of the frame:

 public class BoxDetector extends Detector {
    private Detector mDelegate;
    private int mBoxWidth, mBoxHeight;

    public BoxDetector(Detector delegate, int boxWidth, int boxHeight) {
        mDelegate = delegate;
        mBoxWidth = boxWidth;
        mBoxHeight = boxHeight;

    public SparseArray detect(Frame frame) {
        int width = frame.getMetadata().getWidth();
        int height = frame.getMetadata().getHeight();
        int right = (width / 2) + (mBoxHeight / 2);
        int left = (width / 2) - (mBoxHeight / 2);
        int bottom = (height / 2) + (mBoxWidth / 2);
        int top = (height / 2) - (mBoxWidth / 2);

        YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21, width, height, null);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(new Rect(left, top, right, bottom), 100, byteArrayOutputStream);
        byte[] jpegArray = byteArrayOutputStream.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);

        Frame croppedFrame =
                new Frame.Builder()

        return mDelegate.detect(croppedFrame);

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

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


//This my barcode detector building class:

     Detector<Barcode> barcodeDetector = new BoxDetector(new BarcodeDetector.Builder(context).setBarcodeFormats(Barcode.ALL_FORMATS).build(), metrics.widthPixels, metrics.heightPixels);
        //BoxDetector myDetector = new BoxDetector(barcodeDetector, metrics.widthPixels, metrics.heightPixels);

        BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay, this);
                new MultiProcessor.Builder<>(barcodeFactory).build());
        CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
                .setRequestedPreviewSize(metrics.heightPixels, metrics.widthPixels)

Where and what i am missing. Referred all github thread related to this issue. But i couldn’t find the solution. Please suggest some link or solution for this issue.

One thought on “Vision API Barcode – Restrict the detection area to center rect of screen”

  1. BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context)

    //qrBorder 280dp
    DisplayMetrics dm = getResources().getDisplayMetrics();
    int height = dm.heightPixels;
    int wight = dm.widthPixels;
    BoxDetector bx = new BoxDetector(barcodeDetector,pxFromDp(280), height, wight);
    BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay, this);
    new MultiProcessor.Builder<>(barcodeFactory).build());

Leave a Reply

Your email address will not be published.