Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:03:17+00:00 2026-06-13T16:03:17+00:00

The documentation page for Camera.setDisplayOrientation contains the following code sample stating that using it

  • 0

The documentation page for Camera.setDisplayOrientation contains the following code sample stating that using it will “make the camera image show in the same orientation as the display”:

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

However, I had problems using it, sometimes the image would appear upside down. After some trial-and-error, I found that the correct code would be (replacing the last 8 lines of the method):

    int result = (360 + info.orientation - degrees) % 360;
    camera.setDisplayOrientation(result);

(Which means the calculation for back-facing cameras is correct for front cameras too.) The “compensate the mirror” comment is a bit weird since mirroring cannot be undone by rotating, that operation only swaps 90° and 270° rotations which doesn’t really make sense to me.

So the question is: is the sample wrong indeed or am I missing something? I tried it on multiple devices, both back and front cameras and all supported orientations, so I know that my code IS working. One little detail that might be worth mentioning: all my devices returned 90° as info.orientation.

EDIT: Here is my camera-related code, I have tested it on a Nexus One and a Samsung Galaxy S Plus. It is used in my head-tracking 3D app, the preview is shown in the lower left corner and should always have the correct orientation.

SOLUTION (sort of): It looks like the code is correct, but my testing phone (Samsung Galaxy S Plus) returns an incorrect value for the front camera’s CameraInfo.orientation. There are many related discussions about the preview being shown upside down on this model (examples here and here). A way to fix is to include an option to manually rotate the image.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:03:18+00:00Added an answer on June 13, 2026 at 4:03 pm

    The snippet you’ve quoted, which I used and applied in my project, is no problem in my circumstances.

    For all the devices (Galaxy Note, Galaxy S III, Galaxy Nexus, Galaxy Ace II, Nexus S) I used for test, info.Orientation all return 270 on front camera, and 90 on back camera.

    After a few discuss with the question raiser, I found I misunderstood the questions, so I divide the answer in two parts.

    For the wrong orientation in camera preview, please refer to this solution:

    Solution for wrong orientation in camera preview:

    First please ensure info.Orientation will return 270 on front camera, 90 on back camera.
    Then please try set your camera preview activity (or similar class that handles the preview) orientation to landscape.

    Thus, when you go through the code, you’ll find:

    degree = 90 for screen orientation, info.Orientation = 270 for the front camera. Then you’ll get result = (270 - 90 + 360) % 360, result = 180, which means it will rotate clock wise 180 for your front camera view that will correct the front camera upside-down issue.

    Solution for wrong orientation in camera photo results:

    If this info.Orientation applies to you, then the problem may be:

    1. For some Samsung (or other) devices (Like Galaxy Note, Galaxy SIII), the camera will only write the Orientation Tag instead of rotate the real pixels.
    2. If you’re using the front camera and using the code above, it will display the preview with correct orientation, but will show u the “upside down” picture if you take the shot.

    Solution:

    /**
     *
     * Get the orientation from EXIF
     * @param filepath
     * @return orientation
     */
    public int getExifOrientation(String filepath) {
        int degree = 0;
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException ex) {
            Log.e("EXIF info", "cannot read exif", ex);
        }
        if (exif != null) {
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
            if (orientation != -1) {
                // We only recognize a subset of orientation tag values.
                switch(orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
                }
            }
        } else {
            degree = 1;
        }
        Log.i("EXIF info", "Orientation degrees: " + degree);
        return degree;
    }
    

    Then

    if (isFromCamera) {
    
        if (fromFrontCam) {
            // try change here if the orientation still wrong, -90 means rotate counter-clockwise 90 degrees.
            matrix.preRotate(-90);
         } else {
            matrix.preRotate(90);
         }
    } else {
        // read EXIF data
        getExifOrientation(path)
        // don't forget to handle the situation that some devices won't write exif data
        matrix.preRotate(degree);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the search_config documentation page, I see that there's something that looks like it
The Google Storage documentation page states that You can use a CNAME redirect only
on twill documentation page it is written: By default, twill will run pages through
On the documentation page for Equal we read that Approximate numbers with machine precision
The documentation page for INSERT ... SELECT states that for INSERT ... SELECT to
PHP's documentation page for flock() indicates that it's not safe to use under IIS.
Thare is a tutorial at javaFX documentation page. This example describes how to make
I am using the render_field macro presented on the Flask-WFT documentation page to render
I am trying to make Doctrine 2 work. But on the documentation page about
Reading the documentation page of BugTracker.NET BugTracker.NET API Documentation I realized that I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.