summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Eino-Ville Talvala <etalvala@google.com> 2016-03-25 11:54:39 -0700
committer Eino-Ville Talvala <etalvala@google.com> 2016-03-25 12:29:44 -0700
commit6c91e2c31b514bcfd838c75b4c74412e7288415d (patch)
tree2f19498e8b2aa00cbc27ab1f682a46a1ddccebf4
parenta70cbf7f9cf7dbb12f7f6067c039c85bcac99361 (diff)
Camera: Add bandaid for preview orientation on reverse-landscape cameras
Camera sensors on Android may be either landscape or reverse-landscape oriented, but the vast majority of shipping devices only use landscape. This means that many camera-using apps (which are generally forcing themselves to landscape orientation) never call setDisplayOrientation, since its default value of 0 is correct for the majority of devices. However, there are some reverse-landscape devices, and for those, such apps get upside-down preview. This bandaid changes the default value of displayOrientation to be 180 on such devices, so that apps that never call setDisplayOrientation get correct preview. This bandaid has no effect on apps that do call setDisplayOrientation, so hopefully such apps are doing the math correctly. Also update documentation to indicate that setDisplayOrientation should be called, and to note the change in default orientation behavior in Android N. Change-Id: I1b2c957642fda8edead61bd07eda9d18c38d1fe6 Fixes: 27840948
-rw-r--r--core/java/android/hardware/Camera.java12
-rw-r--r--core/jni/android_hardware_Camera.cpp39
2 files changed, 48 insertions, 3 deletions
diff --git a/core/java/android/hardware/Camera.java b/core/java/android/hardware/Camera.java
index 3dbe437a6c43..acf0677559fd 100644
--- a/core/java/android/hardware/Camera.java
+++ b/core/java/android/hardware/Camera.java
@@ -76,7 +76,7 @@ import static android.system.OsConstants.*;
* <li>If necessary, modify the returned {@link Camera.Parameters} object and call
* {@link #setParameters(Camera.Parameters)}.
*
- * <li>If desired, call {@link #setDisplayOrientation(int)}.
+ * <li>Call {@link #setDisplayOrientation(int)} to ensure correct orientation of preview.
*
* <li><b>Important</b>: Pass a fully initialized {@link SurfaceHolder} to
* {@link #setPreviewDisplay(SurfaceHolder)}. Without a surface, the camera
@@ -1511,9 +1511,15 @@ public class Camera {
* <p>Starting from API level 14, this method can be called when preview is
* active.
*
+ * <p><b>Note: </b>Before API level 24, the default value for orientation is 0. Starting in
+ * API level 24, the default orientation will be such that applications in forced-landscape mode
+ * will have correct preview orientation, which may be either a default of 0 or
+ * 180. Applications that operate in portrait mode or allow for changing orientation must still
+ * call this method after each orientation change to ensure correct preview display in all
+ * cases.</p>
+ *
* @param degrees the angle that the picture will be rotated clockwise.
- * Valid values are 0, 90, 180, and 270. The starting
- * position is 0 (landscape).
+ * Valid values are 0, 90, 180, and 270.
* @see #setPreviewDisplay(SurfaceHolder)
*/
public native final void setDisplayOrientation(int degrees);
diff --git a/core/jni/android_hardware_Camera.cpp b/core/jni/android_hardware_Camera.cpp
index 806fcc3619a1..91f003d59c46 100644
--- a/core/jni/android_hardware_Camera.cpp
+++ b/core/jni/android_hardware_Camera.cpp
@@ -567,6 +567,45 @@ static jint android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
// save context in opaque field
env->SetLongField(thiz, fields.context, (jlong)context.get());
+
+ // Update default display orientation in case the sensor is reverse-landscape
+ CameraInfo cameraInfo;
+ status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
+ if (rc != NO_ERROR) {
+ return rc;
+ }
+ int defaultOrientation = 0;
+ switch (cameraInfo.orientation) {
+ case 0:
+ break;
+ case 90:
+ if (cameraInfo.facing == CAMERA_FACING_FRONT) {
+ defaultOrientation = 180;
+ }
+ break;
+ case 180:
+ defaultOrientation = 180;
+ break;
+ case 270:
+ if (cameraInfo.facing != CAMERA_FACING_FRONT) {
+ defaultOrientation = 180;
+ }
+ break;
+ default:
+ ALOGE("Unexpected camera orientation %d!", cameraInfo.orientation);
+ break;
+ }
+ if (defaultOrientation != 0) {
+ ALOGV("Setting default display orientation to %d", defaultOrientation);
+ rc = camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION,
+ defaultOrientation, 0);
+ if (rc != NO_ERROR) {
+ ALOGE("Unable to update default orientation: %s (%d)",
+ strerror(-rc), rc);
+ return rc;
+ }
+ }
+
return NO_ERROR;
}