diff options
author | Yuichi Araki <yaraki@google.com> | 2016-01-12 17:03:26 +0900 |
---|---|---|
committer | Yuichi Araki <yaraki@google.com> | 2016-01-13 13:47:36 +0900 |
commit | 33a323efaf50c783e81990c2c51696a33669ed8c (patch) | |
tree | fbb524d07fc3ed88508cb430945987db12ce0fd7 | |
parent | fd2af1f1e8c12b1d17f542093356bdc92edf68be (diff) | |
download | android-33a323efaf50c783e81990c2c51696a33669ed8c.tar.gz |
Camera2Basic: Fix JPEG orientation on some devices
https://github.com/googlesamples/android-Camera2Basic/issues/24
Change-Id: I1a2ac55a0ebdba922ac07e2ee6ccf5ed42c11956
-rw-r--r-- | media/Camera2Basic/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/media/Camera2Basic/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java b/media/Camera2Basic/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java index 747d8d83..a29fa144 100644 --- a/media/Camera2Basic/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java +++ b/media/Camera2Basic/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java @@ -276,6 +276,11 @@ public class Camera2BasicFragment extends Fragment private boolean mFlashSupported; /** + * Orientation of the camera sensor + */ + private int mSensorOrientation; + + /** * A {@link CameraCaptureSession.CaptureCallback} that handles events related to JPEG capture. */ private CameraCaptureSession.CaptureCallback mCaptureCallback @@ -515,19 +520,19 @@ public class Camera2BasicFragment extends Fragment // Find out if we need to swap dimension to get the preview size relative to sensor // coordinate. int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); - int sensorOrientation = - characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); + //noinspection ConstantConditions + mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION); boolean swappedDimensions = false; switch (displayRotation) { case Surface.ROTATION_0: case Surface.ROTATION_180: - if (sensorOrientation == 90 || sensorOrientation == 270) { + if (mSensorOrientation == 90 || mSensorOrientation == 270) { swappedDimensions = true; } break; case Surface.ROTATION_90: case Surface.ROTATION_270: - if (sensorOrientation == 0 || sensorOrientation == 180) { + if (mSensorOrientation == 0 || mSensorOrientation == 180) { swappedDimensions = true; } break; @@ -821,7 +826,7 @@ public class Camera2BasicFragment extends Fragment // Orientation int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); - captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation)); + captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation)); CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() { @@ -844,6 +849,20 @@ public class Camera2BasicFragment extends Fragment } /** + * Retrieves the JPEG orientation from the specified screen rotation. + * + * @param rotation The screen rotation. + * @return The JPEG orientation (one of 0, 90, 270, and 360) + */ + private int getOrientation(int rotation) { + // Sensor orientation is 90 for most devices, or 270 for some devices (eg. Nexus 5X) + // We have to take that into account and rotate JPEG properly. + // For devices with orientation of 90, we simply return our mapping from ORIENTATIONS. + // For devices with orientation of 270, we need to rotate the JPEG 180 degrees. + return (ORIENTATIONS.get(rotation) + mSensorOrientation + 270) % 360; + } + + /** * Unlock the focus. This method should be called when still image capture sequence is * finished. */ |