Share via


Camera.SetDisplayOrientation(Int32) Method

Definition

Caution

deprecated

Set the clockwise rotation of preview display in degrees.

[Android.Runtime.Register("setDisplayOrientation", "(I)V", "")]
[System.Obsolete("deprecated")]
public void SetDisplayOrientation (int degrees);
[<Android.Runtime.Register("setDisplayOrientation", "(I)V", "")>]
[<System.Obsolete("deprecated")>]
member this.SetDisplayOrientation : int -> unit

Parameters

degrees
Int32

the angle that the picture will be rotated clockwise. Valid values are 0, 90, 180, and 270.

Attributes

Remarks

Set the clockwise rotation of preview display in degrees. This affects the preview frames and the picture displayed after snapshot. This method is useful for portrait mode applications. Note that preview display of front-facing cameras is flipped horizontally before the rotation, that is, the image is reflected along the central vertical axis of the camera sensor. So the users can see themselves as looking into a mirror.

This does not affect the order of byte array passed in PreviewCallback#onPreviewFrame, JPEG pictures, or recorded videos. This method is not allowed to be called during preview.

If you want to make the camera image show in the same orientation as the display, you can use the following code.

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 rotation = activity.getWindowManager().getDefaultDisplay()
                        .getRotation();
                int degrees = 0;
                switch (rotation) {
                    case Surface.ROTATION_0: degrees = 0; break;
                    case Surface.ROTATION_90: degrees = 90; break;
                    case Surface.ROTATION_180: degrees = 180; break;
                    case Surface.ROTATION_270: degrees = 270; break;
                }

                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);
            }

Starting from API level 14, this method can be called when preview is active.

<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.

Java documentation for android.hardware.Camera.setDisplayOrientation(int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

See also