Android使用ZXing扫描在部分手机上扫描变形解决办法

释放双眼,带上耳机,听听看~!

项目中用ZXing做的二维码扫描,在部分6.0的手机上扫描变形了,网上找了老半天终于是修改成功了,记录下解决办法。

修改CameraConfigurationManager.java下的findBestPreviewSizeValue()方法中的一句代码:

float newDiff = Math.abs(screenResolution.x * 1.0f / newY - screenResolution.y * 1.0f / newX);
private static Point findBestPreviewSizeValue(
CharSequence previewSizeValueString, Point screenResolution) {
int bestX = 0;
int bestY = 0;
float diff = Integer.MAX_VALUE;
for (String previewSize : COMMA_PATTERN.split(previewSizeValueString)) {
previewSize = previewSize.trim();
int dimPosition = previewSize.indexOf('x');
if (dimPosition < 0) {
Log.w(TAG, "Bad preview-size: " + previewSize);
continue;
}
int newX;
int newY;
try {
newX = Integer.parseInt(previewSize.substring(0, dimPosition));
newY = Integer.parseInt(previewSize.substring(dimPosition + 1));
} catch (NumberFormatException nfe) {
Log.w(TAG, "Bad preview-size: " + previewSize);
continue;
}
//          int newDiff = Math.abs(newX - screenResolution.x) + Math.abs(newY - screenResolution.y);
float newDiff = Math.abs(screenResolution.x * 1.0f / newY - screenResolution.y * 1.0f / newX);
if (newDiff == 0) {
bestX = newX;
bestY = newY;
break;
} else if (newDiff < diff) {
bestX = newX;
bestY = newY;
diff = newDiff;
}
}
if (bestX > 0 && bestY > 0) {
return new Point(bestX, bestY);
}
return null;
}

人已赞赏
Android文章

Android Studio获取sha1值和MD5的正确姿势

2021-1-7 10:59:07

Android文章

Android RecyclerView使用notifyItemRemoved()移除数据时数据错乱

2021-1-7 13:05:08

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索