diff options
Diffstat (limited to 'java/com/android/dialer/callcomposer/camera/CameraManager.java')
-rw-r--r-- | java/com/android/dialer/callcomposer/camera/CameraManager.java | 113 |
1 files changed, 71 insertions, 42 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); + } } } |