我正在开发一个自定义相机应用程序,我面临着设置相机预览尺寸的问题。问题是相机预览不会保持纵横比,因此预览看起来会拉伸到两侧。
我用来计算最佳尺寸的代码:
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 1;
double targetRatio=(double)w / h;
if (sizes == null){
return null;
}
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
Log.d("Test", "Ration: " + Math.abs(ratio - targetRatio));
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) {
Log.d("Test", "MATCFH!");
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
我一直在努力争取这一天,但找不到最佳解决方案。任何建议将不胜感激!
答案 0 :(得分:0)
您说得对,更改相机预览尺寸可能无法保留纵横比。要使预览显示看起来不错,您应该相应地调整预览surfaceView的大小。