1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.camera.one.v2.photo; 18 19 import com.google.common.base.Supplier; 20 21 import android.annotation.TargetApi; 22 import android.os.Build; 23 24 import com.android.camera.app.OrientationManager; 25 import com.android.camera.debug.Log; 26 import com.android.camera.one.OneCamera; 27 import com.android.camera.one.OneCameraCharacteristics; 28 import com.android.camera.util.CameraUtil; 29 30 /** 31 * Default implementation of ImageRotationCalculator which takes the camera's 32 * sensor rotation and front/back-facing property to calculate image rotations. 33 */ 34 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 35 public class ImageRotationCalculatorImpl implements ImageRotationCalculator { 36 37 private final int mSensorOrientationDegrees; 38 private final boolean mFrontFacing; 39 private final OrientationManager mOrientationManager; 40 41 /** 42 * Create a calculator with the given hardware properties of the camera. 43 * 44 * @param sensorOrientationDegrees the orientation of the sensor, in 45 * degrees. 46 * @param frontFacing whether the camera is front-facing. 47 */ 48 public ImageRotationCalculatorImpl(OrientationManager orientationManager, 49 int sensorOrientationDegrees, boolean frontFacing) { 50 mSensorOrientationDegrees = sensorOrientationDegrees; 51 mFrontFacing = frontFacing; 52 mOrientationManager = orientationManager; 53 } 54 55 /** 56 * Create a calculator based on Camera characteristics. 57 */ 58 public static ImageRotationCalculator from(OrientationManager orientationManager, 59 OneCameraCharacteristics characteristics) { 60 int sensorOrientation = characteristics.getSensorOrientation(); 61 OneCamera.Facing lensDirection = characteristics.getCameraDirection(); 62 return new ImageRotationCalculatorImpl(orientationManager, sensorOrientation, 63 lensDirection == OneCamera.Facing.FRONT); 64 } 65 66 @Override 67 public OrientationManager.DeviceOrientation toImageRotation() { 68 int imageRotation = CameraUtil.getImageRotation(mSensorOrientationDegrees, 69 mOrientationManager.getDeviceOrientation().getDegrees(), mFrontFacing); 70 return OrientationManager.DeviceOrientation.from(imageRotation); 71 } 72 73 @Override 74 public Supplier<Integer> getSupplier() { 75 return new Supplier<Integer>() { 76 @Override 77 public Integer get() { 78 return Integer.valueOf(toImageRotation().getDegrees()); 79 } 80 }; 81 } 82 } 83