在我的应用程序中,我有扫描仪视图,必须使用zxing库扫描两个条形码。顶级条形码为PDF417格式,底部 - 在DATAMATRIX中。我使用https://github.com/dm77/barcodescanner作为基础,但主要区别:我必须使用图像坐标作为扫描区域。主要算法:
public static Rect getScanRectangle(View view) {
int[] l = new int[2];
view.measure(0, 0);
view.getLocationOnScreen(l);
return new Rect(l[0], l[1], l[0] + view.getMeasuredWidth(), l[1] + view.getMeasuredHeight());
}
2.在扫描仪视图中,在onPreviewFrame方法中,从相机参数接收相机预览尺寸。当我将字母数据从相机转换为内存中的位图图像时,我看到它旋转了90度,并且相机分辨率不等于屏幕分辨率。所以,我必须将屏幕坐标映射到相机(或表面视图)坐标:
private Rect normalizeScreenCoordinates(Rect input, int cameraWidth, int cameraHeight) {
if(screenSize == null) {
screenSize = new Point();
Display display = activity.getWindowManager().getDefaultDisplay();
display.getSize(screenSize);
}
int height = screenSize.x;
int width = screenSize.y;
float widthCoef = (float)cameraWidth/width;
float heightCoef = (float)cameraHeight/height;
Rect result = new Rect((int)(input.top * widthCoef), (int)(input.left * heightCoef), (int)(input.bottom * widthCoef), (int)(input.right * heightCoef));
return result;
}