gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:01:31 27 4
gpt4 key购买 nike

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

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

 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com