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

拥有回忆 提交于 2020-02-02 12:52:59

问题


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()
                        .setBitmap(bitmap)
                        .setRotation(frame.getMetadata().getRotation())
                        .build();

        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);
        barcodeDetector.setProcessor(
                new MultiProcessor.Builder<>(barcodeFactory).build());
 @SuppressWarnings("SuspiciousNameCombination")
        CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(metrics.heightPixels, metrics.widthPixels)
                .setRequestedFps(30.0f);

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.


回答1:


    BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context)
            .setBarcodeFormats(Barcode.ALL_FORMATS)
            .build();

    //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);
    bx.setProcessor(
            new MultiProcessor.Builder<>(barcodeFactory).build());


来源:https://stackoverflow.com/questions/48923619/vision-api-barcode-restrict-the-detection-area-to-center-rect-of-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!