前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android Zxing 转换竖屏扫描且提高识别率的方法

Android Zxing 转换竖屏扫描且提高识别率的方法

作者头像
砸漏
发布2020-11-05 14:51:25
9710
发布2020-11-05 14:51:25
举报
文章被收录于专栏:恩蓝脚本

最近的一个Android需要用到扫码功能,用的是Zxing开源库。Zxing的集成就不说了,但是Zxing默认的是横屏扫码,在实际生产中并不适用,需要改为竖屏扫描。

转竖屏步骤:

1 . AndroidManifest.xml中把activity标签CaptureActivity部分的screenOrientation改为portrait。

代码语言:javascript
复制
android:screenOrientation="portrait"

2 . CameraManager类中的getFramingRectInPreview()方法,将left, right, top, bottom改变。

代码语言:javascript
复制
//竖屏
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类中的setDesiredCameraParameters(OpenCamera camera, boolean safeMode)方法,在setParameters之前添加

代码语言:javascript
复制
theCamera.setDisplayOrientation(90);

4 . DecodeHandler类中的decode(byte[] data, int width, int height)方法,在PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height)之前添加

代码语言:javascript
复制
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;

此时,竖屏扫描已经可以实现了,但是扫描复杂的图码时,分辨率低的已经分不清纹理了,很难识别出来,所以需要优化识别率。

识别率优化:

1 . CameraConfigurationUtils类中的findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution)方法,将double screenAspectRatio = screenResolution.x / (double) screenResolution.y改为

代码语言:javascript
复制
double screenAspectRatio;
if (screenResolution.x   screenResolution.y) {
  screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;
} else {
  screenAspectRatio = (double) screenResolution.y / (double) screenResolution.x;
}

2 . 至此,识别率已经很大程度上的提高了,若在要提高识别率,可通过修改CameraManager类中的MAX_FRAME_WIDTH和MAX_FRAME_HEIGHT来提高精度。

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档