图像在相机预览中失真
image distorted on camera preview
我知道这个主题在 SO 上出现过很多次,但我无法找到任何解决方案。我正在构建一个照相亭类型的应用程序,除了相机的实时取景失真(图像从上到下拉伸)之外,一切正常,这会影响最终结果,因为叠加层不会扭曲,只是相机预览。图像也以正确的纵横比保存得很好。下面是代码,如果需要,我可以 post 更多。
Parameters params = camera.getParameters();
List<Camera.Size> supportedPreviewSizes = params.getSupportedPreviewSizes();
Camera.Size camPreviewSize = getOptimalPreviewSize(supportedPreviewSizes, width , height);
Log.d(TAG+"--",height +" : " + width);//display height and width
Log.d(TAG+"--",camPreviewSize.height +" : " + camPreviewSize.width);
params.setPreviewSize(camPreviewSize.width ,camPreviewSize.height);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
计算预览大小
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio=(double)h / w;
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;
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);
}
}
}
return optimalSize;
}
更改 params.setPreviewSize()
的值似乎没有效果
为了回答我自己的问题,我在开始相机预览之前漏掉了一行。 camera.setParameters(params);
所以我的第一个区块应该是这样的。
Parameters params = camera.getParameters();
List<Camera.Size> supportedPreviewSizes = params.getSupportedPreviewSizes();
Camera.Size camPreviewSize = getOptimalPreviewSize(supportedPreviewSizes, width , height);
Log.d(TAG+"--",height +" : " + width);//display height and width
Log.d(TAG+"--",camPreviewSize.height +" : " + camPreviewSize.width);
params.setPreviewSize(camPreviewSize.width ,camPreviewSize.height);
camera.setParameters(params); //This line was missing
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
我知道这个主题在 SO 上出现过很多次,但我无法找到任何解决方案。我正在构建一个照相亭类型的应用程序,除了相机的实时取景失真(图像从上到下拉伸)之外,一切正常,这会影响最终结果,因为叠加层不会扭曲,只是相机预览。图像也以正确的纵横比保存得很好。下面是代码,如果需要,我可以 post 更多。
Parameters params = camera.getParameters();
List<Camera.Size> supportedPreviewSizes = params.getSupportedPreviewSizes();
Camera.Size camPreviewSize = getOptimalPreviewSize(supportedPreviewSizes, width , height);
Log.d(TAG+"--",height +" : " + width);//display height and width
Log.d(TAG+"--",camPreviewSize.height +" : " + camPreviewSize.width);
params.setPreviewSize(camPreviewSize.width ,camPreviewSize.height);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
计算预览大小
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio=(double)h / w;
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;
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);
}
}
}
return optimalSize;
}
更改 params.setPreviewSize()
的值似乎没有效果
为了回答我自己的问题,我在开始相机预览之前漏掉了一行。 camera.setParameters(params);
所以我的第一个区块应该是这样的。
Parameters params = camera.getParameters();
List<Camera.Size> supportedPreviewSizes = params.getSupportedPreviewSizes();
Camera.Size camPreviewSize = getOptimalPreviewSize(supportedPreviewSizes, width , height);
Log.d(TAG+"--",height +" : " + width);//display height and width
Log.d(TAG+"--",camPreviewSize.height +" : " + camPreviewSize.width);
params.setPreviewSize(camPreviewSize.width ,camPreviewSize.height);
camera.setParameters(params); //This line was missing
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();