android - 视觉 API 条码 - 将检测区域限制在屏幕的中心矩形

标签 android barcode-scanner vision

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

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

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

最佳答案

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

关于android - 视觉 API 条码 - 将检测区域限制在屏幕的中心矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48923619/

相关文章:

java.lang.NoSuchFieldError : com. google.android.gms.R$string.location_client_powered_by_google 问题

android - Jitpack:无法通过示例库解决(Android Studio)

Android/Mobile Webkit CSS 背景附件 :Fixed Not Working?

ios - 在 iOS 中将条码扫描器作为键盘的方法?

java - 如何在 Java 中生成有效的 EAN13 条形码?

ios - 使用Vision&AVFoundation Framework从实时摄像头(不是从静态图像)进行实时面部检测

ios - Swift iOS - 视觉框架文本识别和矩形

android - Flutter: InternalLinkedHashMap<String, dynamic >' has no instance method ' cast' with matching arguments

android - 如何传递 Intent 要求用户选择任何应用程序扫描二维码?

deep-learning - Pytorch Global Pruning 并没有减小模型的大小