My app opens the camera using surface holder. Camera opens with a default preview size. On certain devices the default preview size captures more image than is actually visible on the screen. Inorder to avoid this, I started setting the preview size myself. Criterion is to select the preview size from the available list of sizes, which is closest to the width of the screen.
This works well most of the time. But often I stumble upon few devices which doesn't like the preview size I selected.
1) Nexus 4 didn't capture the image for one of the preview sizes I selected. 2) Samsung S 4 gives band of lines(noise) along the captured images.
So I wanted to understand if I am doing anything against the standard. Here are my questions :-
1) I am using surface holder to open the camera. Is there any other way I can use the camera hardware in my app.
2) I am selecting the preview size from the list of available sizes. Are there any specific which I should look for before selecting any size ?. Logic for selecting the size is mentioned below :-
private Camera.Size getBestPreviewSize(int width, int height)
{
List<Size> sizes = camera.getParameters().getSupportedPreviewSizes();
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = height;
int targetWidth = width;
int minWidthDiff = 0;
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.width - targetWidth) < minDiff) {
if(size.width > width) {
if (minWidthDiff == 0) {
minWidthDiff = size.width - width;
optimalSize = size;
}
else if (Math.abs(size.width - targetWidth) < minWidthDiff) {
minWidthDiff = size.width - width;
optimalSize = size;
}
minDiff = Math.abs(size.width - targetWidth);
}
}
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
Any directions on this will really help.
Thanks !! Gagan