I have been trying to figure this out for a while, but for some reason, when I start recording a video with the camera, the preview zooms in. I have the following code from some examples:
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters myParameters = mCamera.getParameters();
List<Camera.Size> sizes = myParameters.getSupportedPreviewSizes();
Camera.Size myBestSize = getBestPreviewSize(sizes, width, height);
if (myBestSize != null) {
myParameters.setPreviewSize(myBestSize.width, myBestSize.height);
myParameters.setVideoStabilization(false);
mCamera.setParameters(myParameters);
mCamera.startPreview();
mCamera.unlock();
}
}
private Camera.Size getBestPreviewSize(List<Camera.Size> sizes, int width, int height) {
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) width / height;
if (sizes == null) return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = height;
// Find size
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;
}
But the preview still zooms in when recording a video. I tried manually setting the preview size to 1280x720, however, it doesn't work on all devices that I tested, with some showing a black SurfaceView.
Is there any way to prevent the preview from zooming in when recording a video? None of the solutions here at StackOverflow seem to work.
EDIT: I tried created a custom SurfaceView with its own onMesaure method, but now I get a -19 error when I start my MediaRecorder. (disregard the -16 in the bounty)