Fix wrong usage of setRotation and setOrientation.

bug:3105659
Change-Id: Ie2c1559cc03ece66705c1d766bea0e736f2684bc
diff --git a/src/com/android/camera/Camera.java b/src/com/android/camera/Camera.java
index c9c7394..99951c3 100644
--- a/src/com/android/camera/Camera.java
+++ b/src/com/android/camera/Camera.java
@@ -776,16 +776,16 @@
         private void capture() {
             mCaptureOnlyData = null;
 
-            // Set JPEG rotation to match the orientation of what users see. The
-            // rotation is relative to the orientation of the camera. The value
-            // from OrientationEventListener is relative to the natural
-            // orientation of the device. CameraInfo.mOrientation is the angle
-            // between camera orientation and natural device orientation. The
-            // sum of the two is the value for setRotation.
+            // See android.hardware.Camera.Parameters.setRotation for
+            // documentation.
             int rotation = 0;
             if (mOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
                 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
-                rotation = (mOrientation + info.orientation) % 360;
+                if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
+                    rotation = (info.orientation - mOrientation + 360) % 360;
+                } else {  // back-facing camera
+                    rotation = (info.orientation + mOrientation) % 360;
+                }
             }
             mParameters.setRotation(rotation);
 
diff --git a/src/com/android/camera/Util.java b/src/com/android/camera/Util.java
index adf9152..d5cd768 100644
--- a/src/com/android/camera/Util.java
+++ b/src/com/android/camera/Util.java
@@ -22,6 +22,7 @@
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Matrix;
+import android.hardware.Camera;
 import android.util.Log;
 import android.view.Surface;
 import android.view.View;
@@ -293,12 +294,19 @@
     }
 
     public static void setCameraDisplayOrientation(Activity activity,
-            int cameraId, android.hardware.Camera camera) {
-        android.hardware.Camera.CameraInfo info =
-                new android.hardware.Camera.CameraInfo();
-        android.hardware.Camera.getCameraInfo(cameraId, info);
+            int cameraId, Camera camera) {
+        // See android.hardware.Camera.setCameraDisplayOrientation for
+        // documentation.
+        Camera.CameraInfo info = new Camera.CameraInfo();
+        Camera.getCameraInfo(cameraId, info);
         int degrees = getDisplayRotation(activity);
-        int result = (info.orientation - degrees + 360) % 360;
+        int result;
+        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
+            result = (info.orientation + degrees) % 360;
+            result = (360 - result) % 360;  // compensate the mirror
+        } else {  // back-facing
+            result = (info.orientation - degrees + 360) % 360;
+        }
         camera.setDisplayOrientation(result);
     }
 }