summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/callcomposer/camera
diff options
context:
space:
mode:
Diffstat (limited to 'java/com/android/dialer/callcomposer/camera')
-rw-r--r--java/com/android/dialer/callcomposer/camera/CameraManager.java113
-rw-r--r--java/com/android/dialer/callcomposer/camera/ImagePersistTask.java6
-rw-r--r--java/com/android/dialer/callcomposer/camera/exif/ExifInterface.java2
3 files changed, 75 insertions, 46 deletions
diff --git a/java/com/android/dialer/callcomposer/camera/CameraManager.java b/java/com/android/dialer/callcomposer/camera/CameraManager.java
index 977f063df..5915ce331 100644
--- a/java/com/android/dialer/callcomposer/camera/CameraManager.java
+++ b/java/com/android/dialer/callcomposer/camera/CameraManager.java
@@ -509,54 +509,61 @@ public class CameraManager implements FocusOverlayManager.Listener {
}.execute();
}
- /** Updates the orientation of the camera to match the orientation of the device */
- private void updateCameraOrientation() {
- if (mCamera == null || mCameraPreview == null || mTakingPicture) {
- return;
+ /**
+ * Updates the orientation of the {@link Camera} w.r.t. the orientation of the device and the
+ * orientation that the physical camera is mounted on the device.
+ *
+ * @param camera that needs to be reorientated
+ * @param screenRotation rotation of the physical device
+ * @param cameraOrientation {@link CameraInfo#orientation}
+ * @param cameraIsFrontFacing {@link CameraInfo#CAMERA_FACING_FRONT}
+ * @return rotation that images returned from {@link
+ * android.hardware.Camera.PictureCallback#onPictureTaken(byte[], Camera)} will be rotated.
+ */
+ @VisibleForTesting
+ static int updateCameraRotation(
+ @NonNull Camera camera,
+ int screenRotation,
+ int cameraOrientation,
+ boolean cameraIsFrontFacing) {
+ Assert.isNotNull(camera);
+ Assert.checkArgument(cameraOrientation % 90 == 0);
+
+ int rotation = screenRotationToDegress(screenRotation);
+ boolean portrait = rotation == 0 || rotation == 180;
+
+ if (!portrait && !cameraIsFrontFacing) {
+ rotation += 180;
}
+ rotation += cameraOrientation;
+ rotation %= 360;
- final WindowManager windowManager =
- (WindowManager) mCameraPreview.getContext().getSystemService(Context.WINDOW_SERVICE);
+ // Rotate the camera
+ if (portrait && cameraIsFrontFacing) {
+ camera.setDisplayOrientation((rotation + 180) % 360);
+ } else {
+ camera.setDisplayOrientation(rotation);
+ }
+
+ // Rotate the images returned when a picture is taken
+ Camera.Parameters params = camera.getParameters();
+ params.setRotation(rotation);
+ camera.setParameters(params);
+ return rotation;
+ }
- int degrees;
- switch (windowManager.getDefaultDisplay().getRotation()) {
+ private static int screenRotationToDegress(int screenRotation) {
+ switch (screenRotation) {
case Surface.ROTATION_0:
- degrees = 0;
- mCamera.setDisplayOrientation(90);
- break;
+ return 0;
case Surface.ROTATION_90:
- degrees = 90;
- break;
+ return 90;
case Surface.ROTATION_180:
- degrees = 180;
- break;
+ return 180;
case Surface.ROTATION_270:
- degrees = 270;
- mCamera.setDisplayOrientation(180);
- break;
+ return 270;
default:
- throw Assert.createAssertionFailException("");
- }
-
- // The clockwise rotation angle relative to the orientation of the camera. This affects
- // pictures returned by the camera in Camera.PictureCallback.
- if (mCameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
- mRotation = (mCameraInfo.orientation + degrees) % 360;
- } else { // back-facing
- mRotation = (mCameraInfo.orientation - degrees + 360) % 360;
- }
- try {
- final Camera.Parameters params = mCamera.getParameters();
- params.setRotation(mRotation);
- mCamera.setParameters(params);
- } catch (final RuntimeException e) {
- LogUtil.e(
- "CameraManager.updateCameraOrientation",
- "RuntimeException in CameraManager.updateCameraOrientation",
- e);
- if (mListener != null) {
- mListener.onCameraError(ERROR_OPENING_CAMERA, e);
- }
+ throw Assert.createIllegalStateFailException("Invalid surface rotation.");
}
}
@@ -586,7 +593,14 @@ public class CameraManager implements FocusOverlayManager.Listener {
}
try {
mCamera.stopPreview();
- updateCameraOrientation();
+ if (!mTakingPicture) {
+ mRotation =
+ updateCameraRotation(
+ mCamera,
+ getScreenRotation(),
+ mCameraInfo.orientation,
+ mCameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT);
+ }
final Camera.Parameters params = mCamera.getParameters();
final Camera.Size pictureSize = chooseBestPictureSize();
@@ -635,6 +649,14 @@ public class CameraManager implements FocusOverlayManager.Listener {
}
}
+ private int getScreenRotation() {
+ return mCameraPreview
+ .getContext()
+ .getSystemService(WindowManager.class)
+ .getDefaultDisplay()
+ .getRotation();
+ }
+
public boolean isCameraAvailable() {
return mCamera != null && !mTakingPicture && mIsHardwareAccelerationSupported;
}
@@ -672,7 +694,14 @@ public class CameraManager implements FocusOverlayManager.Listener {
@Override
public void onOrientationChanged(final int orientation) {
- updateCameraOrientation();
+ if (!mTakingPicture) {
+ mRotation =
+ updateCameraRotation(
+ mCamera,
+ getScreenRotation(),
+ mCameraInfo.orientation,
+ mCameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT);
+ }
}
}
diff --git a/java/com/android/dialer/callcomposer/camera/ImagePersistTask.java b/java/com/android/dialer/callcomposer/camera/ImagePersistTask.java
index b5542ab6c..7cb6921e9 100644
--- a/java/com/android/dialer/callcomposer/camera/ImagePersistTask.java
+++ b/java/com/android/dialer/callcomposer/camera/ImagePersistTask.java
@@ -98,11 +98,11 @@ public class ImagePersistTask extends FallibleAsyncTask<Void, Void, Uri> {
// Couldn't get exif tags, not the end of the world
}
+ ExifInterface.OrientationParams params = ExifInterface.getOrientationParams(orientation);
Bitmap bitmap = BitmapFactory.decodeByteArray(mBytes, 0, mBytes.length);
final int clippedWidth;
final int clippedHeight;
- boolean invert = ExifInterface.getOrientationParams(orientation).invertDimensions;
- if (invert) {
+ if (params.invertDimensions) {
Assert.checkState(mWidth == bitmap.getHeight());
Assert.checkState(mHeight == bitmap.getWidth());
clippedWidth = (int) (mHeight * mHeightPercent);
@@ -120,7 +120,7 @@ public class ImagePersistTask extends FallibleAsyncTask<Void, Void, Uri> {
mHeight = clippedHeight;
Matrix matrix = new Matrix();
- matrix.postRotate(invert ? 90 : 0);
+ matrix.postRotate(params.rotation);
Bitmap clippedBitmap =
Bitmap.createBitmap(
diff --git a/java/com/android/dialer/callcomposer/camera/exif/ExifInterface.java b/java/com/android/dialer/callcomposer/camera/exif/ExifInterface.java
index 92dee1c94..1bf9519ad 100644
--- a/java/com/android/dialer/callcomposer/camera/exif/ExifInterface.java
+++ b/java/com/android/dialer/callcomposer/camera/exif/ExifInterface.java
@@ -286,7 +286,7 @@ public class ExifInterface {
/** Wrapper class to define some orientation parameters. */
public static class OrientationParams {
- int rotation = 0;
+ public int rotation = 0;
int scaleX = 1;
int scaleY = 1;
public boolean invertDimensions = false;