Fixed distortion of videos recorded in landscape orientation

pull/1/head
Eduardo Omine 8 years ago
parent cc6116a951
commit 4701cdff94
  1. 104
      camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java

@ -5,6 +5,7 @@ import android.graphics.YuvImage;
import android.hardware.Camera; import android.hardware.Camera;
import android.media.CamcorderProfile; import android.media.CamcorderProfile;
import android.media.MediaRecorder; import android.media.MediaRecorder;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
@ -248,59 +249,79 @@ public class Camera1 extends CameraImpl {
mCameraListener.onVideoTaken(mVideoFile); mCameraListener.onVideoTaken(mVideoFile);
} }
@Override // Code from SandriosCamera library
Size getCaptureResolution() { // https://github.com/sandrios/sandriosCamera/blob/master/sandriosCamera/src/main/java/com/sandrios/sandriosCamera/internal/utils/CameraHelper.java#L218
if (mCaptureSize == null && mCameraParameters != null) { public static Size getSizeWithClosestRatio(List<Size> sizes, int width, int height)
TreeSet<Size> sizes = new TreeSet<>(); {
for (Camera.Size size : mCameraParameters.getSupportedPictureSizes()) { if (sizes == null) return null;
sizes.add(new Size(size.width, size.height));
double MIN_TOLERANCE = 100;
double targetRatio = (double) height / width;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = height;
for (Size size : sizes) {
if (size.getWidth() == width && size.getHeight() == height)
return size;
double ratio = (double) size.getHeight() / size.getWidth();
if (Math.abs(ratio - targetRatio) < MIN_TOLERANCE) MIN_TOLERANCE = ratio;
else continue;
if (Math.abs(size.getHeight() - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.getHeight() - targetHeight);
}
} }
TreeSet<AspectRatio> aspectRatios = findCommonAspectRatios( if (optimalSize == null) {
mCameraParameters.getSupportedPreviewSizes(), minDiff = Double.MAX_VALUE;
mCameraParameters.getSupportedPictureSizes() for (Size size : sizes) {
); if (Math.abs(size.getHeight() - targetHeight) < minDiff) {
AspectRatio targetRatio = aspectRatios.size() > 0 ? aspectRatios.last() : null; optimalSize = size;
minDiff = Math.abs(size.getHeight() - targetHeight);
Iterator<Size> descendingSizes = sizes.descendingIterator();
Size size;
while (descendingSizes.hasNext() && mCaptureSize == null) {
size = descendingSizes.next();
if (targetRatio == null || targetRatio.matches(size)) {
mCaptureSize = size;
break;
} }
} }
} }
return optimalSize;
return mCaptureSize;
} }
@Override List<Size> sizesFromList(List<Camera.Size> sizes) {
Size getPreviewResolution() { if (sizes == null) return null;
if (mPreviewSize == null && mCameraParameters != null) { List<Size> result = new ArrayList<>(sizes.size());
TreeSet<Size> sizes = new TreeSet<>();
for (Camera.Size size : mCameraParameters.getSupportedPreviewSizes()) { for (Camera.Size size : sizes) {
sizes.add(new Size(size.width, size.height)); result.add(new Size(size.width, size.height));
} }
TreeSet<AspectRatio> aspectRatios = findCommonAspectRatios( return result;
mCameraParameters.getSupportedPreviewSizes(),
mCameraParameters.getSupportedPictureSizes()
);
AspectRatio targetRatio = aspectRatios.size() > 0 ? aspectRatios.last() : null;
Iterator<Size> descendingSizes = sizes.descendingIterator();
Size size;
while (descendingSizes.hasNext() && mPreviewSize == null) {
size = descendingSizes.next();
if (targetRatio == null || targetRatio.matches(size)) {
mPreviewSize = size;
break;
} }
// Code from SandriosCamera library
// https://github.com/sandrios/sandriosCamera/blob/master/sandriosCamera/src/main/java/com/sandrios/sandriosCamera/internal/manager/impl/Camera1Manager.java#L212
void initResolutions() {
List<Size> previewSizes = sizesFromList(mCameraParameters.getSupportedPreviewSizes());
List<Size> videoSizes = (Build.VERSION.SDK_INT > 10) ? sizesFromList(mCameraParameters.getSupportedVideoSizes()) : previewSizes;
CamcorderProfile camcorderProfile = getCamcorderProfile(mVideoQuality);
mCaptureSize = getSizeWithClosestRatio(
(videoSizes == null || videoSizes.isEmpty()) ? previewSizes : videoSizes,
camcorderProfile.videoFrameWidth, camcorderProfile.videoFrameHeight);
mPreviewSize = getSizeWithClosestRatio(previewSizes, mCaptureSize.getWidth(), mCaptureSize.getHeight());
} }
@Override
Size getCaptureResolution() {
return mCaptureSize;
} }
@Override
Size getPreviewResolution() {
return mPreviewSize; return mPreviewSize;
} }
@ -367,6 +388,8 @@ public class Camera1 extends CameraImpl {
} }
private void adjustCameraParameters() { private void adjustCameraParameters() {
initResolutions();
boolean invertPreviewSizes = mDisplayOrientation%180 != 0; boolean invertPreviewSizes = mDisplayOrientation%180 != 0;
mPreview.setTruePreviewSize( mPreview.setTruePreviewSize(
invertPreviewSizes? getPreviewResolution().getHeight() : getPreviewResolution().getWidth(), invertPreviewSizes? getPreviewResolution().getHeight() : getPreviewResolution().getWidth(),
@ -428,6 +451,7 @@ public class Camera1 extends CameraImpl {
mVideoFile = new File(mPreview.getView().getContext().getExternalFilesDir(null), "video.mp4"); mVideoFile = new File(mPreview.getView().getContext().getExternalFilesDir(null), "video.mp4");
mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath()); mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath());
mMediaRecorder.setOrientationHint(calculatePreviewRotation()); mMediaRecorder.setOrientationHint(calculatePreviewRotation());
mMediaRecorder.setVideoSize(mCaptureSize.getWidth(), mCaptureSize.getHeight());
} }
private void prepareMediaRecorder() { private void prepareMediaRecorder() {

Loading…
Cancel
Save