0

I captured image from camera and display image into imageview. It will display perfect but the captured image rotated 90 degrees automatically in some devices. I searched a lot about it but could not get proper solution. Please tell me the solution for it. Thanks in advance.

koti
  • 23
  • 10
  • Possible duplicate of [why image captured using camera intent gets rotated on some devices in android](http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on-some-devices-in-android) – Dhinakaran Thennarasu Oct 29 '15 at 05:12
  • Its because of Exif Orientation with captured camera images.. – Meenal Oct 29 '15 at 05:15

2 Answers2

0

try this code

public Bitmap adjustImageOrientation(Bitmap image, File f) {
    int rotate = 0;
    try {

        ExifInterface exif = new ExifInterface(f.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;

        case ExifInterface.ORIENTATION_NORMAL:
            rotate = 0;
            break;

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (rotate != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);

        image = Bitmap.createBitmap(image, 0, 0, image.getWidth(),
                image.getHeight(), matrix, true);
    }


    return image;
}
Meenal
  • 2,879
  • 5
  • 19
  • 43
0

Use this method to determine image rotation.

 public int getCameraPhotoOrientation(Uri imageUri, String imagePath) {
        int rotate = 0;
        try {
            _context.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;
            }


        } catch (Exception e) {
        }
        return rotate;
    }

And if the method wouldn't return Zero rotated the image.

int degree=getCameraPhotoOrientation(Uri.fromFile(tempFile), fPath);
if (degree!= 0) {
      bitmap = tools.rotateOrientationCall(bitmap, degree);
 }

Rotate your bitmap.

 public Bitmap rotateOrientationCall(Bitmap src, float degree) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
    }
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73