I'm creating a custom camera so that my app can take videos of a specified size (640 x 480), or at least close to a specified size. To do this, I try to get the supported Video size in onCreate like so:
useMe = null;
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> list = parameters.getSupportedVideoSizes();
for (int i = 0; i < list.size(); i++) {
Camera.Size size = list.get(i);
Log.i("SIZE: ", size.width + " " + size.height);
if (size.width == 640) {
useMe = size;
break;
}
if (size.width < 640) {
if ((i - 1) > 0) {
useMe = list.get(i-1);
} else {
useMe = size;
}
}
}
if (useMe == null) {
useMe = list.get(list.size()/2);
}
Then, I use the size while setting up the camera:
private boolean prepareVideoRecorder(){
mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(useMe.width, useMe.height);
mCamera.setParameters(p);
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mMediaRecorder.setVideoSize(useMe.width, useMe.height);
mMediaRecorder.setMaxDuration(10000);
mMediaRecorder.setOnInfoListener(this);
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile().toString());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
This records the video correctly, BUT the preview is stretched. I also tried setting
parameters.setPreviewSize(size.width, size.height);
in the surfaceChanged() method.