1. 功能概述
默认的ZXing Demo提供的是横屏扫描,不符合现在市场的流行趋势和用户的使用习惯,然而在修改界面为竖屏的过程中,我们发现zxing无法读取条形码数据(二维码可以正常读取)。修改布局的过程不再赘述。
2. 修改说明
1) 在AndroidManifest.xml中,把Activity的属性的screenOrientation设置为"portrait"。在开发android的应用中,有时候需要限制横竖屏切换。只需要在AndroidManifest.xml文件中加入android:screenOrientation属性限制。其中android:screenOrientation="portrait"是限制此页面竖屏显示。
2) 在CameraManager.java文件中,修改预览缩放。主要根据长宽比对预览效果进行了分类。如果不需要横屏了,可以直接取竖屏的设置。主要代码如下:
rect.left = framingRect.left * cameraResolution.x / screenResolution.x;
rect.right = framingRect.right * cameraResolution.x / screenResolution.x;
rect.top = framingRect.top * cameraResolution.y / screenResolution.y;
rect.bottom = framingRect.bottom * cameraResolution.y / screenResolution.y;
改为:
// 下面为竖屏模式
if (screenResolution.x < screenResolution.y) {
rect.left = framingRect.left * cameraResolution.y / screenResolution.x;
rect.right = framingRect.right * cameraResolution.y / screenResolution.x;
rect.top = framingRect.top * cameraResolution.x / screenResolution.y;
rect.bottom = framingRect.bottom * cameraResolution.x / screenResolution.y;
} else {
// 下面为横屏模式
rect.left = framingRect.left * cameraResolution.x / screenResolution.x;
rect.right = framingRect.right * cameraResolution.x / screenResolution.x;
rect.top = framingRect.top * cameraResolution.y / screenResolution.y;
rect.bottom = framingRect.bottom * cameraResolution.y / screenResolution.y;
}
3) 在CameraManager.java文件中,修改解析函数 buildLuminanceSource(),这是本次修改的核心,因为默认的条形码解析是横屏的,在本次修改中,我们根据竖屏条件做了一些针对性的处理,主要功能是将获得数据的宽和高置换,使其适应竖屏环境。核心代码如下:
public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
Rect rect = getFramingRectInPreview();
if (rect == null) {
return null;
}
PlanarYUVLuminanceSource source;
Point point = configManager.getScreenResolution();
if (point.x < point.y) {
byte[] rotatedData = new byte[data.length];
int newWidth = height;
int newHeight = width;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * newWidth + newWidth - 1 - y] = data[x + y * width];
}
source = new PlanarYUVLuminanceSource(rotatedData, newWidth, newHeight,
rect.left, rect.top, rect.width(), rect.height(), false);
} else {
source = new PlanarYUVLuminanceSource(data, width, height,
rect.left, rect.top, rect.width(), rect.height(), false);
}
return source;
}
4) 在CameraConfigurationUtils.java文件中,需要在findBestPreviewSizeValue方法中将screenAspectRatio分情况设置,因为screenAspectRatio的要求总是大值比小值。代码如下:
将
double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;
修改为:
double screenAspectRatio;
if (screenResolution.x < screenResolution.y) { // 竖屏
screenAspectRatio = (double) screenResolution.y / (double) screenResolution.x;
} else {
screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;
}
5) 在CameraConfigurationUtils.java文件中,删除如下代码,原因是对于镜头分辨率高,而屏幕分辨率低的手机,这段代码直接导致扫码插件采用较低的分辨率去生成用于解析的位图,所以直接去掉。然后你就会发现低分辨率的手机,对二维码、条码的识别率显著提高。
if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
Point exactPoint = new Point(realWidth, realHeight);
Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
return exactPoint;
}
网友评论