I know this topic has appeared many times on SO but i cant get any of the solutions to work. I am building a photo booth type app and every thing is working fine except the live view from the camera is distorted (the image is streached from top to bottom ) this is effecting the end result as the overlay is not distorted just the camera preview. the image is also saving fine in the correct aspect ratio. Bellow is the code and i can post more if required.
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();
Calcuate Preview Size
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;
}
Changing the values of params.setPreviewSize()
seems to have no effect
To answer my own question i was missing one line before i start the camera preview. camera.setParameters(params);
so my first block should look like this.
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();