参考:http://www.360doc.com/content/14/0825/15/16319846_404515413.shtml
每部手机中设置了好几组预览图片的尺寸、以及实际图片的尺寸。只有当预览图的尺寸比例和实际图片的比例相同的时候就不会出现变形的情况。
对于ZXing扫描来说,实际图片的大小就是屏幕的大小,所以我们要从好几组预览图片的尺寸中找到一组比例(主要是宽、高比例)最合适的,这样预览的图片就不会变形了。
打开文件:
*.*.*.zxing.camera.CameraConfigurationManag
private static Point findBestPreviewSizeValue(CharSequence previewSizeValueString, Point screenResolution) {
int bestX = 0;
int bestY = 0;
float diff = Float.MAX_VALUE;
Log.d(TAG, "screen point: "+screenResolution);
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;
}
Log.w(TAG, "preview-size: " + previewSize +", newDiff: "+newDiff+", diff: "+diff);
}
if (bestX > 0 && bestY > 0) {
return new Point(bestX, bestY);
}
return null;
}
以下是调试的输出信息:
02-25 16:20:22.599 5725-5725/**** D/CameraConfigurationManager﹕ Screen resolution: Point(720, 1280)
02-25 16:20:22.599 5725-5725**** D/CameraConfigurationManager﹕ preview-size-values parameter: 1920x1080,1280x960,1280x720,960x720,960x540,864x480,800x480,768x432,720x540,720x480,640x480,576x432,480x320,384x288,352x288,320x240,240x160,176x144,144x176
02-25 16:20:22.599 5725-5725/**** D/CameraConfigurationManager﹕ Camera resolution: Point(720, 1280)
02-25 16:20:22.609 5725-5725/**** D/CameraConfigurationManager﹕ Setting preview size: Point(1920, 1080)
可以看到支持的预览参数有很多个,这时屏幕的宽、高分别是:720、1280,那么最后寻找的最佳预览是1920,1080,感觉不太对了,哈哈,因为这时屏幕是竖屏的,所以我当时的屏幕是1280、720,那适配的就对了,1920、1080分别就是1280和720的1.5倍,比例适配正好。