视觉API条形码-将检测区域限制在屏幕的中央区域 [英] Vision API Barcode - Restrict the detection area to center rect of screen

查看:134
本文介绍了视觉API条形码-将检测区域限制在屏幕的中央区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中实现条形码扫描仪.我想限制我的检测范围.遵循以下逻辑,但在某些设备中我无法正常工作.

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);

我在哪里和我想念的地方.引用了与此问题相关的所有github线程.但是我找不到解决方案.请为该问题提供一些链接或解决方案.

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.

推荐答案

    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());

这篇关于视觉API条形码-将检测区域限制在屏幕的中央区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆