I am making a Camera app in Android and have used the following function to get the preview size:
private Size getOptimalPreviewSize(List sizes,
That algorithm is not the greatest.
The default algorithm in my CWAC-Camera library is now:
public static Camera.Size getBestAspectPreviewSize(int displayOrientation,
int width,
int height,
Camera.Parameters parameters,
double closeEnough) {
double targetRatio=(double)width / height;
Camera.Size optimalSize=null;
double minDiff=Double.MAX_VALUE;
if (displayOrientation == 90 || displayOrientation == 270) {
targetRatio=(double)height / width;
}
List<Size> sizes=parameters.getSupportedPreviewSizes();
Collections.sort(sizes,
Collections.reverseOrder(new SizeComparator()));
for (Size size : sizes) {
double ratio=(double)size.width / size.height;
if (Math.abs(ratio - targetRatio) < minDiff) {
optimalSize=size;
minDiff=Math.abs(ratio - targetRatio);
}
if (minDiff < closeEnough) {
break;
}
}
return(optimalSize);
}
This:
Takes into account portrait versus landscape
Starts with the highest resolution previews and works its way down
Can be tailored via closeEnough
to opt for higher resolution as opposed to best matching the aspect ratio of the preview area