1

I don't want to support landscape UI at all across my app, but I want to be able to automatically rotate the photos users take in landscape mode. Currently if a user takes a photo in landscape mode, it remains on screen as if it was taken in portrait (the horizon in the photo is vertical).

I've tried to get the orientation from the system like this:

val display = (getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
val screenOrientation = display.rotation
Log.d("orientation", screenOrientation.toString())

But it doesn't work and just gives back 0 every time.

I've tried a few other solutions but couldn't get them to work.

I am wondering, am I wasting my time trying to figure this out? Is it even possible to know the orientation in which the photo was captured if my activity only operates in portrait mode? There are a few articles out there talking about camera orientation but they don't talk about whether the orientation in their activity is locked or not.

Thank you.

Tsabary
  • 3,119
  • 2
  • 24
  • 66
  • 2
    Possible duplicate of [Android get Orientation of a camera Bitmap? And rotate back -90 degrees](https://stackoverflow.com/questions/7286714/android-get-orientation-of-a-camera-bitmap-and-rotate-back-90-degrees) – ADM May 07 '19 at 07:37
  • Do you take the photo with camera intent (that is, handling the orientation in **onActivityResult()** callback), or you use Camera API, or some helper library? – Alex Cohn May 07 '19 at 14:26
  • 1
    @Alex Cohn, I am uisng CamerKit's API. I've attached my current code in a second question asking where to integrate the rotation, you can see it here https://stackoverflow.com/questions/56020041/what-is-the-right-point-in-my-code-to-rotate-an-landscape-image-captured-in-my-a/56020207#56020207 – Tsabary May 07 '19 at 22:37
  • I believe [this solution](https://stackoverflow.com/a/27617038/192373) can be applied in your case with minimal adaptations. – Alex Cohn May 08 '19 at 07:07

1 Answers1

0
    //Yes, find the Orientation Using ExifInterface
       try{
            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                                ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_NORMAL);

     // dont know exactly but, orientation for landscape may be 180 or 270.
     //  From the orientation value you can convert image according to orientation and can be set it vertically, horrizentally as below

             int rotate = 0;
             switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_270:
                     rotate = 270;
                     break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                     rotate = 180;
                     break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                     rotate = 90;
                     break;
              }
              Log.e(Common.TAG, "Exif orientation: " + orientation);
            }
       catch (Exception e) {
           e.printStackTrace();
       }

      // Now Change image as your wish...
      // Image rotation //
      Matrix matrix = new Matrix();
      matrix.postRotate(orientation);
      Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true);  
Arwy Shelke
  • 333
  • 1
  • 2
  • 12