camera intent auto rotate to 90 degree

后端 未结 2 1176
旧巷少年郎
旧巷少年郎 2020-12-15 12:33

In my code below, I am trying to take photo using native camera and upload to server but when I take it as portrait and view it in gallery as landscape which means, its rota

相关标签:
2条回答
  • 2020-12-15 12:50
    Matrix matrix=new Matrix();
    imageView.setScaleType(ScaleType.MATRIX);   //required
    matrix.postRotate((float) angle, pivX, pivY);
    imageView.setImageMatrix(matrix);
    
    
    
    This method does not require creating a new bitmap each time.. Hope this works.
    
    0 讨论(0)
  • 2020-12-15 12:58

    I also faced this kind of problem while showing the images in listview. But using the EXIF data I was able to get a work around to set the images in proper orientation.

    This is were the bitmap object for display is prepared :

      Matrix matrix = new Matrix();
      matrix.postRotate(getImageOrientation(url));
      Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
      bitmap.getHeight(), matrix, true);
    

    This is the method used, in the 2nd line of above code, to rotate the images orientation.

     public static int getImageOrientation(String imagePath){
         int rotate = 0;
         try {
    
             File imageFile = new File(imagePath);
             ExifInterface exif = new ExifInterface(
                     imageFile.getAbsolutePath());
             int orientation = exif.getAttributeInt(
                     ExifInterface.TAG_ORIENTATION,
                     ExifInterface.ORIENTATION_NORMAL);
    
             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;
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
        return rotate;
     }
    

    This may not be the precise answer to your question, it worked for me and hope it will be useful for you.

    0 讨论(0)
提交回复
热议问题