Vision API条形码 - 将检测区域限制在屏幕的中心位置

问题描述 投票:1回答:1

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

//尝试裁剪框架的中心部分:

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

}

//这是我的条形码检测器构建类:

     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线程。但我找不到解决方案。请为此问题提供一些链接或解决方案。

android barcode-scanner vision
1个回答
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());
© www.soinside.com 2019 - 2024. All rights reserved.