Captured Photo orientation is changing in android

后端 未结 5 2192
星月不相逢
星月不相逢 2020-11-30 13:05

I\'am opening camera app on click of a button. And displaying the captured photo in next activity. But the captured photo is rotating by 90 degrees. When I display the image

相关标签:
5条回答
  • 2020-11-30 13:15

    you can just simpaly rotate it

    get the orietation of the image taken using this code in onActvityresult ....

    File imageFile = new File(imageUri.toString());
           ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
           int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
           int rotate = 0;
           switch(orientation) {
             case ORIENTATION_ROTATE_270:
                 rotate-=90;break;
             case ORIENTATION_ROTATE_180:
                 rotate-=90;break
             case ORIENTATION_ROTATE_90:
                 rotate-=90;break
           }
    
    0 讨论(0)
  • 2020-11-30 13:18

    Please use Glide instead. It fixes the orientation itself and is irrespective of the mobile you're using. Here\s a sample

    Glide.with(_c).load(horizontalList.get(position).imagePath).into(holder.img_injury);
    
    0 讨论(0)
  • 2020-11-30 13:22

    I had the same problem mostly with the Samsung handsets.Apparently Samsung phones set the EXIF orientation tag, rather than rotating individual pixels.Reading the Bitmap using BitmapFactory does not support this tag.What i found the solution to this problem was using ExifInterface in onActivityResult method of the activity.Which checks for orientation associated with URI of the captured image from the camera.

                            int rotate = 0;
                            try {
                                getContentResolver().notifyChange(imageUri, null);
                                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;
                                }
                                Log.v(Common.TAG, "Exif orientation: " + orientation);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
    
                            /****** Image rotation ****/
                            Matrix matrix = new Matrix();
                            matrix.postRotate(orientation);
                            Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true);
    
    0 讨论(0)
  • 2020-11-30 13:26

    I got it somewhat working. When I take the photo in Landscape mode everything works fine as before. If I take the photo in portrait mode, I need to turn the camera upside down and the pic looks good. If you notice I changed your code a bit by adding the "ExifInterface" in front of each orientation. I used the following code:

        try {
          ExifInterface exif = new ExifInterface(filename); 
          int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                              ExifInterface.ORIENTATION_NORMAL);
          int rotate = 0;
             switch(orientation) {
                case  ExifInterface.ORIENTATION_ROTATE_270:
                     rotate-=90;break;
                case  ExifInterface.ORIENTATION_ROTATE_180:
                     rotate-=90;break;
                case  ExifInterface.ORIENTATION_ROTATE_90:
                     rotate-=90;break;
                }
                  Log.d("Fragment", "EXIF info for file " + filename + ": " + rotate);
             } catch (IOException e) {
                 Log.d("Fragment", "Could not get EXIF info for file " + filename + ": " + e);
             }
    
    0 讨论(0)
  • 2020-11-30 13:27

    call this method with path of bitmap and bitmap

       getRotateImage(path, bm)
    

    Add getRotateImage method as static in any util class and use it.

    public static Bitmap getRotateImage(String photoPath, Bitmap bitmap) throws IOException {
            ExifInterface ei = new ExifInterface(photoPath);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_UNDEFINED);
    
            Bitmap rotatedBitmap = null;
            switch (orientation) {
    
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotatedBitmap = rotateImage(bitmap, 90);
                    break;
    
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotatedBitmap = rotateImage(bitmap, 180);
                    break;
    
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotatedBitmap = rotateImage(bitmap, 270);
                    break;
    
                case ExifInterface.ORIENTATION_NORMAL:
                default:
                    rotatedBitmap = bitmap;
            }
    
            return rotatedBitmap;
    
        }
    
    
      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);
        }
    
    0 讨论(0)
提交回复
热议问题