setup the proper video preview size

This commit is contained in:
tibbi 2016-06-16 15:56:49 +02:00
parent a021b1fe80
commit 145271bd7c
2 changed files with 33 additions and 8 deletions

View File

@ -170,7 +170,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
rotation += 90; rotation += 90;
} }
final Camera.Size maxSize = getPictureSize(); final Camera.Size maxSize = getOptimalPictureSize();
parameters.setPictureSize(maxSize.width, maxSize.height); parameters.setPictureSize(maxSize.width, maxSize.height);
parameters.setRotation(rotation % 360); parameters.setRotation(rotation % 360);
@ -206,8 +206,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
} }
}; };
private Camera.Size getPictureSize() { private Camera.Size getOptimalPictureSize() {
List<Camera.Size> sizes = parameters.getSupportedPictureSizes(); final List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
Camera.Size maxSize = sizes.get(0); Camera.Size maxSize = sizes.get(0);
for (Camera.Size size : sizes) { for (Camera.Size size : sizes) {
final boolean isEightMegapixelsMax = isEightMegapixelsMax(size); final boolean isEightMegapixelsMax = isEightMegapixelsMax(size);
@ -231,6 +231,19 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
return diff < RATIO_TOLERANCE; return diff < RATIO_TOLERANCE;
} }
private Camera.Size getOptimalVideoSize() {
final List<Camera.Size> sizes = parameters.getSupportedVideoSizes();
Camera.Size maxSize = sizes.get(0);
for (Camera.Size size : sizes) {
final boolean isSixteenToNine = isSixteenToNine(size);
if (isSixteenToNine) {
maxSize = size;
break;
}
}
return maxSize;
}
private void focusArea() { private void focusArea() {
if (camera == null) if (camera == null)
return; return;
@ -413,9 +426,13 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
// VIDEO RECORDING // VIDEO RECORDING
public void initRecorder() { public void initRecorder() {
if (camera == null) if (camera == null || recorder != null)
return; return;
final Camera.Size preferred = parameters.getPreferredPreviewSizeForVideo();
parameters.setPreviewSize(preferred.width, preferred.height);
camera.setParameters(parameters);
isRecording = false; isRecording = false;
isVideoMode = true; isVideoMode = true;
recorder = new MediaRecorder(); recorder = new MediaRecorder();
@ -434,6 +451,9 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
recorder.setOutputFile(curVideoPath); recorder.setOutputFile(curVideoPath);
recorder.setPreviewDisplay(surfaceHolder.getSurface()); recorder.setPreviewDisplay(surfaceHolder.getSurface());
final Camera.Size videoSize = getOptimalVideoSize();
recorder.setVideoSize(videoSize.width, videoSize.height);
if (currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) { if (currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) {
recorder.setOrientationHint(270); recorder.setOrientationHint(270);
} else { } else {
@ -442,9 +462,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
try { try {
recorder.prepare(); recorder.prepare();
} catch (IllegalStateException e) { } catch (Exception e) {
Log.e(TAG, "initRecorder " + e.getMessage()); Utils.showToast(getContext(), R.string.video_setup_error);
} catch (IOException e) {
Log.e(TAG, "initRecorder " + e.getMessage()); Log.e(TAG, "initRecorder " + e.getMessage());
} }
} }
@ -456,7 +475,12 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
} else { } else {
camera.lock(); camera.lock();
camera.unlock(); camera.unlock();
try {
recorder.start(); recorder.start();
} catch (Exception e) {
Utils.showToast(getContext(), R.string.video_setup_error);
Log.e(TAG, "toggleRecording " + e.getMessage());
}
isRecording = true; isRecording = true;
} }
return isRecording; return isRecording;

View File

@ -4,6 +4,7 @@
<string name="camera_open_error">An error occurred at accessing the camera</string> <string name="camera_open_error">An error occurred at accessing the camera</string>
<string name="video_creating_error">An error occurred at creating the video file</string> <string name="video_creating_error">An error occurred at creating the video file</string>
<string name="video_saving_error">An error occurred at saving the video file</string> <string name="video_saving_error">An error occurred at saving the video file</string>
<string name="video_setup_error">An error occurred at setting up the recorder</string>
<string name="video_directory">Simple Videos</string> <string name="video_directory">Simple Videos</string>
<string name="photo_directory">Simple Photos</string> <string name="photo_directory">Simple Photos</string>
<string name="no_permissions">Not much to do without accessing your camera and storage</string> <string name="no_permissions">Not much to do without accessing your camera and storage</string>