rotate the video properly too

This commit is contained in:
tibbi 2016-06-16 18:13:09 +02:00
parent d5d98fcf31
commit b97ca61f6c
2 changed files with 45 additions and 23 deletions

View File

@ -170,7 +170,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
private void handleShutter() { private void handleShutter() {
if (isInPhotoMode) { if (isInPhotoMode) {
preview.takePicture(orientation); preview.takePicture();
} else { } else {
final Resources res = getResources(); final Resources res = getResources();
final boolean isRecording = preview.toggleRecording(); final boolean isRecording = preview.toggleRecording();
@ -342,4 +342,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
public void activateShutter() { public void activateShutter() {
handleShutter(); handleShutter();
} }
@Override
public int getCurrentOrientation() {
return orientation;
}
} }

View File

@ -46,6 +46,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
private static String curVideoPath; private static String curVideoPath;
private static int lastClickX; private static int lastClickX;
private static int lastClickY; private static int lastClickY;
private static int initVideoRotation;
public Preview(Context context) { public Preview(Context context) {
super(context); super(context);
@ -134,7 +135,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
return result % 360; return result % 360;
} }
private static int getPictureRotation(int cameraId) { private static int getMediaRotation(int cameraId) {
int degrees = getRotationDegrees(); int degrees = getRotationDegrees();
final Camera.CameraInfo info = Utils.getCameraInfo(cameraId); final Camera.CameraInfo info = Utils.getCameraInfo(cameraId);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
@ -160,7 +161,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
} }
} }
public void takePicture(int orientation) { public void takePicture() {
if (canTakePicture) { if (canTakePicture) {
if (isFlashEnabled) { if (isFlashEnabled) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
@ -168,12 +169,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
} }
int rotation = getPictureRotation(currCameraId); int rotation = getMediaRotation(currCameraId);
if (orientation == Constants.ORIENT_LANDSCAPE_LEFT) { rotation += compensateDeviceRotation();
rotation += 270;
} else if (orientation == Constants.ORIENT_LANDSCAPE_RIGHT) {
rotation += 90;
}
final Camera.Size maxSize = getOptimalPictureSize(); final Camera.Size maxSize = getOptimalPictureSize();
parameters.setPictureSize(maxSize.width, maxSize.height); parameters.setPictureSize(maxSize.width, maxSize.height);
@ -249,6 +246,18 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
return maxSize; return maxSize;
} }
private int compensateDeviceRotation() {
int degrees = 0;
boolean isFrontCamera = (currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT);
int deviceOrientation = callback.getCurrentOrientation();
if (deviceOrientation == Constants.ORIENT_LANDSCAPE_LEFT) {
degrees += isFrontCamera ? 90 : 270;
} else if (deviceOrientation == Constants.ORIENT_LANDSCAPE_RIGHT) {
degrees += isFrontCamera ? 270 : 90;
}
return degrees;
}
private void focusArea() { private void focusArea() {
if (camera == null) if (camera == null)
return; return;
@ -298,6 +307,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
camera.release(); camera.release();
camera = null; camera = null;
} }
cleanupRecorder();
} }
@Override @Override
@ -462,11 +473,11 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
final Camera.Size videoSize = getOptimalVideoSize(); final Camera.Size videoSize = getOptimalVideoSize();
recorder.setVideoSize(videoSize.width, videoSize.height); recorder.setVideoSize(videoSize.width, videoSize.height);
if (currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) { int rotation = getMediaRotation(currCameraId);
recorder.setOrientationHint(270); rotation += compensateDeviceRotation();
} else { rotation %= 360;
recorder.setOrientationHint(getPreviewRotation(currCameraId)); initVideoRotation = rotation;
} recorder.setOrientationHint(rotation);
try { try {
recorder.prepare(); recorder.prepare();
@ -481,19 +492,23 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
stopRecording(); stopRecording();
initRecorder(); initRecorder();
} else { } else {
camera.lock(); startRecording();
camera.unlock();
try {
recorder.start();
isRecording = true;
} catch (Exception e) {
Utils.showToast(getContext(), R.string.video_setup_error);
Log.e(TAG, "toggleRecording " + e.getMessage());
}
} }
return isRecording; return isRecording;
} }
private void startRecording() {
camera.lock();
camera.unlock();
try {
recorder.start();
isRecording = true;
} catch (Exception e) {
Utils.showToast(getContext(), R.string.video_setup_error);
Log.e(TAG, "toggleRecording " + e.getMessage());
}
}
private void stopRecording() { private void stopRecording() {
if (recorder != null && isRecording) { if (recorder != null && isRecording) {
try { try {
@ -532,5 +547,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
void setIsCameraAvailable(boolean available); void setIsCameraAvailable(boolean available);
void activateShutter(); void activateShutter();
int getCurrentOrientation();
} }
} }