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

时间:2022-11-17 11:06:52

项目中用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;
}