summaryrefslogtreecommitdiffstats
path: root/camera2/portability/src/com/android/ex/camera2/portability/AndroidCamera2AgentImpl.java
diff options
context:
space:
mode:
authorSol Boucher <solb@google.com>2014-08-12 12:09:04 -0700
committerSolomon Boucher <solb@google.com>2014-08-15 20:21:16 +0000
commitf9feab9a826e5b33d811e757bdfdbfa0738fcfa5 (patch)
treef86eb591626f6e50b5ade96d6587744869637e14 /camera2/portability/src/com/android/ex/camera2/portability/AndroidCamera2AgentImpl.java
parent47497bee8a3550af79f9a32c9362d7ee78364a71 (diff)
downloadandroid_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/AndroidCamera2AgentImpl.java')
-rw-r--r--camera2/portability/src/com/android/ex/camera2/portability/AndroidCamera2AgentImpl.java43
1 files changed, 42 insertions, 1 deletions
diff --git a/camera2/portability/src/com/android/ex/camera2/portability/AndroidCamera2AgentImpl.java b/camera2/portability/src/com/android/ex/camera2/portability/AndroidCamera2AgentImpl.java
index 65047f1..af53bb6 100644
--- a/camera2/portability/src/com/android/ex/camera2/portability/AndroidCamera2AgentImpl.java
+++ b/camera2/portability/src/com/android/ex/camera2/portability/AndroidCamera2AgentImpl.java
@@ -19,7 +19,9 @@ package com.android.ex.camera2.portability;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.ImageFormat;
+import android.graphics.Matrix;
import android.graphics.Rect;
+import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
@@ -458,7 +460,6 @@ class AndroidCamera2AgentImpl extends CameraAgent {
}*/
case CameraActions.SET_DISPLAY_ORIENTATION: {
- // TODO: Need to handle preview in addition to capture
// Only set the JPEG capture orientation if requested to do so; otherwise,
// capture in the sensor's physical orientation
mPersistentSettings.set(CaptureRequest.JPEG_ORIENTATION, msg.arg2 > 0 ?
@@ -1139,11 +1140,51 @@ class AndroidCamera2AgentImpl extends CameraAgent {
}
@Override
+ public Matrix getPreviewTransform(int currentDisplayOrientation,
+ RectF surfaceDimensions,
+ RectF desiredBounds) {
+ if (!orientationIsValid(currentDisplayOrientation)) {
+ return new Matrix();
+ }
+
+ // The system transparently transforms the image to fill the surface
+ // when the device is in its natural orientation. We rotate the
+ // coordinates of the rectangle's corners to be relative to the
+ // original image, instead of to the current screen orientation.
+ float[] surfacePolygon = rotate(convertRectToPoly(surfaceDimensions),
+ 2 * currentDisplayOrientation / 90);
+ float[] desiredPolygon = convertRectToPoly(desiredBounds);
+
+ Matrix transform = new Matrix();
+ // Use polygons instead of rectangles so that rotation will be
+ // calculated, since that is not done by the new camera API.
+ transform.setPolyToPoly(surfacePolygon, 0, desiredPolygon, 0, 4);
+ return transform;
+ }
+
+ @Override
public boolean canDisableShutterSound() {
// The new API doesn't support this operation, so don't encourage people to try it.
// TODO: What kind of assumptions have callers made about this result's meaning?
return false;
}
+
+ private static float[] convertRectToPoly(RectF rf) {
+ return new float[] {rf.left, rf.top, rf.right, rf.top,
+ rf.right, rf.bottom, rf.left, rf.bottom};
+ }
+
+ private static float[] rotate(float[] arr, int times) {
+ if (times < 0) {
+ times = times % arr.length + arr.length;
+ }
+
+ float[] res = new float[arr.length];
+ for (int offset = 0; offset < arr.length; ++offset) {
+ res[offset] = arr[(times + offset) % arr.length];
+ }
+ return res;
+ }
}
}