Getting Orientation as 0 and still getting the rotated image in ImageView in android










1















I have tried most of the code on stackoverflow but none of them are working.
I am using moto x4 for uploading picture using camera. when I use back camera it gets rotated 90 degree left and when I use front camera it gets rotated 90 degree right. but in debug mode, in both case I found the orientation = 0;



 else if (requestCode == CAMERA) 

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
String imagePath = saveImage(thumbnail);

File imageFile = new File(imagePath);
ExifInterface exif = null;
try
exif = new ExifInterface(imageFile.getAbsolutePath());
catch (IOException e)
e.printStackTrace();

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
bmap = GetandSetBitmap.rotateBitmap(thumbnail,orientation);


mImageView.setBackgroundResource(0);
mImageView.setImageBitmap(bmap);










share|improve this question




























    1















    I have tried most of the code on stackoverflow but none of them are working.
    I am using moto x4 for uploading picture using camera. when I use back camera it gets rotated 90 degree left and when I use front camera it gets rotated 90 degree right. but in debug mode, in both case I found the orientation = 0;



     else if (requestCode == CAMERA) 

    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    String imagePath = saveImage(thumbnail);

    File imageFile = new File(imagePath);
    ExifInterface exif = null;
    try
    exif = new ExifInterface(imageFile.getAbsolutePath());
    catch (IOException e)
    e.printStackTrace();

    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    bmap = GetandSetBitmap.rotateBitmap(thumbnail,orientation);


    mImageView.setBackgroundResource(0);
    mImageView.setImageBitmap(bmap);










    share|improve this question


























      1












      1








      1


      1






      I have tried most of the code on stackoverflow but none of them are working.
      I am using moto x4 for uploading picture using camera. when I use back camera it gets rotated 90 degree left and when I use front camera it gets rotated 90 degree right. but in debug mode, in both case I found the orientation = 0;



       else if (requestCode == CAMERA) 

      Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
      String imagePath = saveImage(thumbnail);

      File imageFile = new File(imagePath);
      ExifInterface exif = null;
      try
      exif = new ExifInterface(imageFile.getAbsolutePath());
      catch (IOException e)
      e.printStackTrace();

      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
      bmap = GetandSetBitmap.rotateBitmap(thumbnail,orientation);


      mImageView.setBackgroundResource(0);
      mImageView.setImageBitmap(bmap);










      share|improve this question
















      I have tried most of the code on stackoverflow but none of them are working.
      I am using moto x4 for uploading picture using camera. when I use back camera it gets rotated 90 degree left and when I use front camera it gets rotated 90 degree right. but in debug mode, in both case I found the orientation = 0;



       else if (requestCode == CAMERA) 

      Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
      String imagePath = saveImage(thumbnail);

      File imageFile = new File(imagePath);
      ExifInterface exif = null;
      try
      exif = new ExifInterface(imageFile.getAbsolutePath());
      catch (IOException e)
      e.printStackTrace();

      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
      bmap = GetandSetBitmap.rotateBitmap(thumbnail,orientation);


      mImageView.setBackgroundResource(0);
      mImageView.setImageBitmap(bmap);







      android






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 13:05









      amir133

      13914




      13914










      asked Nov 14 '18 at 12:27









      suyash saurabhsuyash saurabh

      142




      142






















          1 Answer
          1






          active

          oldest

          votes


















          0














          This probably has to do with the fact that one camera is by default in landscape and the other in reverse landscape, so orientation = 0, as the orientation is detected to be the normal one in both cases. Unfortunately, I don't have a solution that wouldn't involve manually rotating the image to cover all cases. Personally I've used a switch to cater for the cases where my photo was saved with a 90 degrees rotation:



          ExifInterface exifInterface = new ExifInterface(pictureFile.getPath());
          int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

          Bitmap correctedBitmap;
          switch(orientation)
          case ExifInterface.ORIENTATION_ROTATE_90:
          correctedBitmap = bitmap;
          capturedImageHolder.setImageBitmap(correctedBitmap);
          break;

          case ExifInterface.ORIENTATION_ROTATE_180:
          correctedBitmap = rotateImage(bitmap, 90);
          capturedImageHolder.setImageBitmap(correctedBitmap);
          break;

          case ExifInterface.ORIENTATION_ROTATE_270:
          correctedBitmap = rotateImage(bitmap, 180);
          capturedImageHolder.setImageBitmap(correctedBitmap);

          break;

          case ExifInterface.ORIENTATION_NORMAL:
          default:
          correctedBitmap = rotateImage(bitmap, 270);
          capturedImageHolder.setImageBitmap(correctedBitmap);
          break;


          } catch (IOException e)



          public static Bitmap rotateImage(Bitmap source, float angle)
          Matrix matrix = new Matrix();
          matrix.postRotate(angle);
          return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
          matrix, true);



          In your case, you would need to detect if the photo is taken from the front-facing camera or the back and adjust the values accordingly.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300256%2fgetting-orientation-as-0-and-still-getting-the-rotated-image-in-imageview-in-and%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            This probably has to do with the fact that one camera is by default in landscape and the other in reverse landscape, so orientation = 0, as the orientation is detected to be the normal one in both cases. Unfortunately, I don't have a solution that wouldn't involve manually rotating the image to cover all cases. Personally I've used a switch to cater for the cases where my photo was saved with a 90 degrees rotation:



            ExifInterface exifInterface = new ExifInterface(pictureFile.getPath());
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

            Bitmap correctedBitmap;
            switch(orientation)
            case ExifInterface.ORIENTATION_ROTATE_90:
            correctedBitmap = bitmap;
            capturedImageHolder.setImageBitmap(correctedBitmap);
            break;

            case ExifInterface.ORIENTATION_ROTATE_180:
            correctedBitmap = rotateImage(bitmap, 90);
            capturedImageHolder.setImageBitmap(correctedBitmap);
            break;

            case ExifInterface.ORIENTATION_ROTATE_270:
            correctedBitmap = rotateImage(bitmap, 180);
            capturedImageHolder.setImageBitmap(correctedBitmap);

            break;

            case ExifInterface.ORIENTATION_NORMAL:
            default:
            correctedBitmap = rotateImage(bitmap, 270);
            capturedImageHolder.setImageBitmap(correctedBitmap);
            break;


            } catch (IOException e)



            public static Bitmap rotateImage(Bitmap source, float angle)
            Matrix matrix = new Matrix();
            matrix.postRotate(angle);
            return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
            matrix, true);



            In your case, you would need to detect if the photo is taken from the front-facing camera or the back and adjust the values accordingly.






            share|improve this answer



























              0














              This probably has to do with the fact that one camera is by default in landscape and the other in reverse landscape, so orientation = 0, as the orientation is detected to be the normal one in both cases. Unfortunately, I don't have a solution that wouldn't involve manually rotating the image to cover all cases. Personally I've used a switch to cater for the cases where my photo was saved with a 90 degrees rotation:



              ExifInterface exifInterface = new ExifInterface(pictureFile.getPath());
              int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

              Bitmap correctedBitmap;
              switch(orientation)
              case ExifInterface.ORIENTATION_ROTATE_90:
              correctedBitmap = bitmap;
              capturedImageHolder.setImageBitmap(correctedBitmap);
              break;

              case ExifInterface.ORIENTATION_ROTATE_180:
              correctedBitmap = rotateImage(bitmap, 90);
              capturedImageHolder.setImageBitmap(correctedBitmap);
              break;

              case ExifInterface.ORIENTATION_ROTATE_270:
              correctedBitmap = rotateImage(bitmap, 180);
              capturedImageHolder.setImageBitmap(correctedBitmap);

              break;

              case ExifInterface.ORIENTATION_NORMAL:
              default:
              correctedBitmap = rotateImage(bitmap, 270);
              capturedImageHolder.setImageBitmap(correctedBitmap);
              break;


              } catch (IOException e)



              public static Bitmap rotateImage(Bitmap source, float angle)
              Matrix matrix = new Matrix();
              matrix.postRotate(angle);
              return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
              matrix, true);



              In your case, you would need to detect if the photo is taken from the front-facing camera or the back and adjust the values accordingly.






              share|improve this answer

























                0












                0








                0







                This probably has to do with the fact that one camera is by default in landscape and the other in reverse landscape, so orientation = 0, as the orientation is detected to be the normal one in both cases. Unfortunately, I don't have a solution that wouldn't involve manually rotating the image to cover all cases. Personally I've used a switch to cater for the cases where my photo was saved with a 90 degrees rotation:



                ExifInterface exifInterface = new ExifInterface(pictureFile.getPath());
                int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

                Bitmap correctedBitmap;
                switch(orientation)
                case ExifInterface.ORIENTATION_ROTATE_90:
                correctedBitmap = bitmap;
                capturedImageHolder.setImageBitmap(correctedBitmap);
                break;

                case ExifInterface.ORIENTATION_ROTATE_180:
                correctedBitmap = rotateImage(bitmap, 90);
                capturedImageHolder.setImageBitmap(correctedBitmap);
                break;

                case ExifInterface.ORIENTATION_ROTATE_270:
                correctedBitmap = rotateImage(bitmap, 180);
                capturedImageHolder.setImageBitmap(correctedBitmap);

                break;

                case ExifInterface.ORIENTATION_NORMAL:
                default:
                correctedBitmap = rotateImage(bitmap, 270);
                capturedImageHolder.setImageBitmap(correctedBitmap);
                break;


                } catch (IOException e)



                public static Bitmap rotateImage(Bitmap source, float angle)
                Matrix matrix = new Matrix();
                matrix.postRotate(angle);
                return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
                matrix, true);



                In your case, you would need to detect if the photo is taken from the front-facing camera or the back and adjust the values accordingly.






                share|improve this answer













                This probably has to do with the fact that one camera is by default in landscape and the other in reverse landscape, so orientation = 0, as the orientation is detected to be the normal one in both cases. Unfortunately, I don't have a solution that wouldn't involve manually rotating the image to cover all cases. Personally I've used a switch to cater for the cases where my photo was saved with a 90 degrees rotation:



                ExifInterface exifInterface = new ExifInterface(pictureFile.getPath());
                int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

                Bitmap correctedBitmap;
                switch(orientation)
                case ExifInterface.ORIENTATION_ROTATE_90:
                correctedBitmap = bitmap;
                capturedImageHolder.setImageBitmap(correctedBitmap);
                break;

                case ExifInterface.ORIENTATION_ROTATE_180:
                correctedBitmap = rotateImage(bitmap, 90);
                capturedImageHolder.setImageBitmap(correctedBitmap);
                break;

                case ExifInterface.ORIENTATION_ROTATE_270:
                correctedBitmap = rotateImage(bitmap, 180);
                capturedImageHolder.setImageBitmap(correctedBitmap);

                break;

                case ExifInterface.ORIENTATION_NORMAL:
                default:
                correctedBitmap = rotateImage(bitmap, 270);
                capturedImageHolder.setImageBitmap(correctedBitmap);
                break;


                } catch (IOException e)



                public static Bitmap rotateImage(Bitmap source, float angle)
                Matrix matrix = new Matrix();
                matrix.postRotate(angle);
                return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
                matrix, true);



                In your case, you would need to detect if the photo is taken from the front-facing camera or the back and adjust the values accordingly.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 12:39









                Nikos HidalgoNikos Hidalgo

                918215




                918215



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300256%2fgetting-orientation-as-0-and-still-getting-the-rotated-image-in-imageview-in-and%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    27

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    Category:Rhetoric