dev*_*imp 10 android barcode-scanner google-vision
我正在尝试将Google Visions扫描程序应用到我正在开发的应用程序中.默认情况下,它会在整个屏幕上跟踪全屏活动和条形码.
但是,我需要全屏摄像头,但扫描窗口有限.例如,相机的表面视图需要全屏,它有2个透明覆盖设置为屏幕高度顶部和底部的35%,在中心留下30%的视口.
我已经更改了图形叠加层,因此它只会显示在中间视口中,但还无法确定如何将条形码跟踪器限制在同一区域.
有任何想法吗?
当前的API不提供限制扫描区域的方法.但是,您可以过滤检测器发出的结果,也可以裁剪传入检测器的图像.
过滤结果方法
使用这种方法,条形码检测器仍将扫描整个图像区域,但是将忽略在目标区域之外检测到的条形码.这样做的一种方法是实现一个"聚焦处理器",它接收来自探测器的结果,并且只将最多一个条形码传递给相关的跟踪器.例如:
public class CentralBarcodeFocusingProcessor extends FocusingProcessor<Barcode> {
public CentralBarcodeFocusingProcessor(Detector<Barcode> detector, Tracker<Barcode> tracker) {
super(detector, tracker);
}
@Override
public int selectFocus(Detections<Barcode> detections) {
SparseArray<Barcode> barcodes = detections.getDetectedItems();
for (int i = 0; i < barcodes.size(); ++i) {
int id = barcodes.keyAt(i);
if (/* barcode in central region */) {
return id;
}
}
return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您将此处理器与检测器关联,如下所示:
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context).build();
barcodeDetector.setProcessor(
new CentralBarcodeFocusingProcessor(myTracker));
Run Code Online (Sandbox Code Playgroud)
裁剪图像方法
在调用探测器之前,您需要先自己裁剪图像.这可以通过实现检测器子类来完成,该子类包装条形码检测器,裁剪接收的图像,并使用裁剪的图像调用条形码扫描器.
例如,你要做一个探测器拦截和裁剪图像,如下所示:
class MyDetector extends Detector<Barcode> {
private Detector<Barcode> mDelegate;
MyDetector(Detector<Barcode> delegate) {
mDelegate = delegate;
}
public SparseArray<Barcode> detect(Frame frame) {
// *** crop the frame here
return mDelegate.detect(croppedFrame);
}
public boolean isOperational() {
return mDelegate.isOperational();
}
public boolean setFocus(int id) {
return mDelegate.setFocus(id);
}
}
Run Code Online (Sandbox Code Playgroud)
您将条形码检测器与此包装在一起,将其放在相机源和条形码检测器之间:
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context)
.build();
MyDetector myDetector = new MyDetector(barcodeDetector);
myDetector.setProcessor(/* include your processor here */);
mCameraSource = new CameraSource.Builder(context, myDetector)
.build();
Run Code Online (Sandbox Code Playgroud)
小智 6
基于@ pm0733464的答案,并提供了有关如何获取最接近预览中心的条形码的示例。
public class CentralBarcodeFocusingProcessor extends FocusingProcessor<Barcode> {
public CentralBarcodeFocusingProcessor(Detector<Barcode> detector, Tracker<Barcode> tracker) {
super(detector, tracker);
}
@Override
public int selectFocus(Detector.Detections<Barcode> detections) {
SparseArray<Barcode> barcodes = detections.getDetectedItems();
Frame.Metadata meta = detections.getFrameMetadata();
double nearestDistance = Double.MAX_VALUE;
int id = -1;
for (int i = 0; i < barcodes.size(); ++i) {
int tempId = barcodes.keyAt(i);
Barcode barcode = barcodes.get(tempId);
float dx = Math.abs((meta.getWidth() / 2) - barcode.getBoundingBox().centerX());
float dy = Math.abs((meta.getHeight() / 2) - barcode.getBoundingBox().centerY());
double distanceFromCenter = Math.sqrt((dx * dx) + (dy * dy));
if (distanceFromCenter < nearestDistance) {
id = tempId;
nearestDistance = distanceFromCenter;
}
}
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5576 次 |
最近记录: |