diff options
author | Sol Boucher <solb@google.com> | 2014-08-12 12:09:04 -0700 |
---|---|---|
committer | Solomon Boucher <solb@google.com> | 2014-08-15 20:21:16 +0000 |
commit | f9feab9a826e5b33d811e757bdfdbfa0738fcfa5 (patch) | |
tree | f86eb591626f6e50b5ade96d6587744869637e14 /camera2/portability/src/com/android/ex/camera2/portability/CameraDeviceInfo.java | |
parent | 47497bee8a3550af79f9a32c9362d7ee78364a71 (diff) | |
download | android_frameworks_ex-f9feab9a826e5b33d811e757bdfdbfa0738fcfa5.tar.gz android_frameworks_ex-f9feab9a826e5b33d811e757bdfdbfa0738fcfa5.tar.bz2 android_frameworks_ex-f9feab9a826e5b33d811e757bdfdbfa0738fcfa5.zip |
camera2-portability: Provide preview transformation matrix
Applying this transform to the Surface on which the stream is displayed results
in a correctly rotated image. For API 1, the rotation is actually performed by
the Camera class and the returned matrix is an identity matrix; however, for API
2, the transformation is responsible for proper orientation.
Bug: 16875535
Change-Id: I044ffbd1095bd1a9792c899b792129cc94c7c916
Diffstat (limited to 'camera2/portability/src/com/android/ex/camera2/portability/CameraDeviceInfo.java')
-rw-r--r-- | camera2/portability/src/com/android/ex/camera2/portability/CameraDeviceInfo.java | 91 |
1 files changed, 76 insertions, 15 deletions
diff --git a/camera2/portability/src/com/android/ex/camera2/portability/CameraDeviceInfo.java b/camera2/portability/src/com/android/ex/camera2/portability/CameraDeviceInfo.java index a657170..72a641e 100644 --- a/camera2/portability/src/com/android/ex/camera2/portability/CameraDeviceInfo.java +++ b/camera2/portability/src/com/android/ex/camera2/portability/CameraDeviceInfo.java @@ -16,7 +16,8 @@ package com.android.ex.camera2.portability; -import android.hardware.Camera; +import android.graphics.Matrix; +import android.graphics.RectF; import com.android.ex.camera2.portability.debug.Log; @@ -67,17 +68,18 @@ public interface CameraDeviceInfo { public abstract boolean isFacingFront(); /** - * @return The camera image orientation, or the clockwise rotation angle - * that must be applied to display it in its natural orientation - * (in degrees, always a multiple of 90, and between [90,270]). + * @return The camera sensor orientation, or the counterclockwise angle + * from its natural position that the device must be held at + * for the sensor to be right side up (in degrees, always a + * multiple of 90, and between 0 and 270, inclusive). */ public abstract int getSensorOrientation(); /** * @param currentDisplayOrientation - * The current display orientation, as measured clockwise from - * the device's natural orientation (in degrees, always a - * multiple of 90, and between 0 and 270, inclusive). + * The current display orientation, measured counterclockwise + * from to the device's natural orientation (in degrees, always + * a multiple of 90, and between 0 and 270, inclusive). * @return * The relative preview image orientation, or the clockwise * rotation angle that must be applied to display preview @@ -92,9 +94,9 @@ public interface CameraDeviceInfo { /** * @param currentDisplayOrientation - * The current display orientation, as measured clockwise from - * the device's natural orientation (in degrees, always a - * multiple of 90, and between 0 and 270, inclusive). + * The current display orientation, measured counterclockwise + * from to the device's natural orientation (in degrees, always + * a multiple of 90, and between 0 and 270, inclusive). * @return * The relative capture image orientation, or the clockwise * rotation angle that must be applied to display these frames @@ -120,11 +122,8 @@ public interface CameraDeviceInfo { */ protected int getRelativeImageOrientation(int currentDisplayOrientation, boolean compensateForMirroring) { - if (currentDisplayOrientation % 90 != 0) { - Log.e(TAG, "Provided display orientation is not divisible by 90"); - } - if (currentDisplayOrientation < 0 || currentDisplayOrientation > 270) { - Log.e(TAG, "Provided display orientation is outside expected range"); + if (!orientationIsValid(currentDisplayOrientation)) { + return 0; } int result = 0; @@ -142,8 +141,70 @@ public interface CameraDeviceInfo { } /** + * @param currentDisplayOrientation + * The current display orientation, measured counterclockwise + * from to the device's natural orientation (in degrees, always + * a multiple of 90, and between 0 and 270, inclusive). + * @param surfaceDimensions + * The dimensions of the {@link android.view.Surface} on which + * the preview image is being rendered. It usually only makes + * sense for the upper-left corner to be at the origin. + * @return + * The transform matrix that should be applied to the + * {@link android.view.Surface} in order for the image to + * display properly in the device's current orientation. + */ + public Matrix getPreviewTransform(int currentDisplayOrientation, RectF surfaceDimensions) { + return getPreviewTransform(currentDisplayOrientation, surfaceDimensions, + new RectF(surfaceDimensions)); + } + + /** + * @param currentDisplayOrientation + * The current display orientation, measured counterclockwise + * from to the device's natural orientation (in degrees, always + * a multiple of 90, and between 0 and 270, inclusive). + * @param surfaceDimensions + * The dimensions of the {@link android.view.Surface} on which + * the preview image is being rendered. It usually only makes + * sense for the upper-left corner to be at the origin. + * @param desiredBounds + * The boundaries within the {@link android.view.Surface} where + * the final image should appear. These can be used to + * translate and scale the output, but note that the image will + * be stretched to fit, possibly changing its aspect ratio. + * @return + * The transform matrix that should be applied to the + * {@link android.view.Surface} in order for the image to + * display properly in the device's current orientation. + */ + public Matrix getPreviewTransform(int currentDisplayOrientation, RectF surfaceDimensions, + RectF desiredBounds) { + if (!orientationIsValid(currentDisplayOrientation) || + surfaceDimensions.equals(desiredBounds)) { + return new Matrix(); + } + + Matrix transform = new Matrix(); + transform.setRectToRect(surfaceDimensions, desiredBounds, Matrix.ScaleToFit.FILL); + return transform; + } + + /** * @return Whether the shutter sound can be disabled. */ public abstract boolean canDisableShutterSound(); + + protected static boolean orientationIsValid(int angle) { + if (angle % 90 != 0) { + Log.e(TAG, "Provided display orientation is not divisible by 90"); + return false; + } + if (angle < 0 || angle > 270) { + Log.e(TAG, "Provided display orientation is outside expected range"); + return false; + } + return true; + } } } |