I'm trying to capture photos directly using the camera api, but this is the preview I got:
& this is the image taken after calling takePicture()
which is bigger than the preview itself:
(note: I cropped the height of the previous 2 photos to enhance question readability, & kept the width as is)
I'm using this utility method to choose best optimal preview size before starting the camera preview:
public static Camera.Size getBestAspectPreviewSize(int displayOrientation,
int width,
int height,
Camera.Parameters parameters) {
double targetRatio = (double) width / height;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
if (displayOrientation == 90 || displayOrientation == 270) {
targetRatio = (double) height / width;
}
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Collections.sort(sizes,
Collections.reverseOrder(new SizeComparator()));
for (Camera.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 < 0.0d) {
break;
}
}
return (optimalSize);
}
& this method to choose a suitable picture size:
public static Camera.Size getBiggestSafePictureSize(Camera.Parameters parameters) {
Camera.Size result = null;
long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
long availableMemory = Runtime.getRuntime().maxMemory() - used;
for (Camera.Size size : parameters.getSupportedPictureSizes()) {
int newArea = size.width * size.height;
long neededMemory = newArea * 4 * 4; // newArea * 4 Bytes/pixel * 4 needed copies of the bitmap (for safety :) )
if (neededMemory > availableMemory)
continue;
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
if (newArea > resultArea) {
result = size;
}
}
}
return (result);
}
& this is the camera preview element in the layout:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/cameraPreview"></FrameLayout>
& I'm following the official documentation for creating the camera preview itself
So, how to force the camera preview to show the exact photo that will be taken?