本文主要是介绍Android开源库之使用ZXing开源库二维码-实现竖屏且高识别率,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ZXing开源库默认是横屏显示,在改为竖屏显示后,手动设定扫描匡的宽高,会发现近距离扫描二维码时,无法扫描成功,需要稍微远一点距离扫描,才能顺利扫描成功,分析应该是设置扫描匡的宽高后,其实际的扫描区域计算有问题,第1~4条借鉴博客,基本解决该问题,也感谢原博主,但是还稍微有点问题,就是在扫描复杂二维码时,识别率太低,这个问题在第5条中已经给出解决方法;
步骤:
1) 在AndroidManifest.xml中把 <Activity />标签 CaptureActivity 的screenOrientation修改为
android:screenOrientation="portrait"
2) 在CameraManager.java类中的getFramingRectInPreview()替换掉原先的 left right top bottom
//竖屏
rect.left = rect.left * cameraResolution.y / screenResolution.x;
rect.right = rect.right * cameraResolution.y / screenResolution.x;
rect.top = rect.top * cameraResolution.x / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
3) 在CameraConfigurationManager.java中void setDesiredCameraParameters(Camera camera)方法
在setParameters之前增加
camera.setDisplayOrientation(90);
co
4) 在DecodeHandler.java中的 decode(byte[] data, int width, int height)方法在
PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
之前添加:
byte[] rotatedData = new byte[data.length]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) rotatedData[x * height + height - y - 1] = data[x + y * width]; } int tmp = width; // Here we are swapping, that's the difference to #11 width = height; height = tmp; data = rotatedData;
修改CameraConfigurationUtils类下的方法findBestPreviewSizeValue下的double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;
这段代码为:
这样无论是横屏还是竖屏,都能正确找到最匹配的最大清晰度,识别率大大提高;double screenAspectRatio; if(screenResolution.x > screenResolution.y){screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y; }else{screenAspectRatio = (double) screenResolution.y / (double) screenResolution.x; }
这篇关于Android开源库之使用ZXing开源库二维码-实现竖屏且高识别率的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!