问题描述:
预览图像被拉伸变形
问题原因:
由于预览图像大小跟SurfaceView 大小不一致引起
解决方法:
获取系统支持的所有预览尺寸[getSupportedPictureSizes],然后再取一个比较接近的尺寸进行设置[setPreviewSize]
版本0(在小米8会模糊不清且变形)
private static Point getCameraResolution(Camera.Parameters parameters, Point screenResolution) {
System.out.println("pingmu " + screenResolution.x + " " + screenResolution.y);
List<Size> list = parameters.getSupportedPreviewSizes();
Collections.sort(list, new Comparator<Size>() {
@Override
public int compare(Size lhs, Size rhs) {
if (lhs.width - rhs.width == 0) {
return lhs.height - rhs.height;
}
return lhs.width - rhs.width;
}
});
for (Size size : list) {
System.out.println(size.width + " " + size.height);
if (Math.abs(size.width * 1.0 / size.height - 1.0 * screenResolution.y / screenResolution.x) < 0.1) {
return new Point(size.width, size.height);
}
}
return new Point(list.get(0).width, list.get(0).height);
// String previewSizeValueString = parameters.get("preview-size-values");
// // saw this on Xperia
// if (previewSizeValueString == null) {
// previewSizeValueString = parameters.get("preview-size-value");
// }
//
// Point cameraResolution = null;
//
// if (previewSizeValueString != null) {
// Log.d(TAG, "preview-size-values parameter: " + previewSizeValueString);
// cameraResolution = findBestPreviewSizeValue(previewSizeValueString, screenResolution);
// }
//
// if (cameraResolution == null) {
// // Ensure that the camera resolution is a multiple of 8, as the screen may not be.
// cameraResolution = new Point(
// (screenResolution.x >> 3) << 3,
// (screenResolution.y >> 3) << 3);
// }
//
// return cameraResolution;
}
版本1(有点变形,清晰明了,但扫描时不灵敏)
private static Point getCameraResolution(Camera.Parameters parameters, Point screenResolution) {
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
int w = screenResolution.x;
int h = screenResolution.y;
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) h / w;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
if(null == optimalSize){
return new Point(sizes.get(0).width, sizes.get(0).height);
}
return new Point(optimalSize.width, optimalSize.height);
}
版本2(暂时测试完美)
private Point getCameraResolution(Camera.Parameters parameters, Point screenResolution){
float tmp = 0f;
float mindiff = 100f;
float x_d_y = (float)screenResolution.x / (float)screenResolution.y;
Size best = null;
List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
for(Size s : supportedPreviewSizes){
tmp = Math.abs(((float)s.height/(float)s.width)-x_d_y);
if(tmp<mindiff){
mindiff = tmp;
best = s;
}
}
return new Point(best.width, best.height);
}
网友评论