8

here is my code

        //
        // reading an image captured using phone camera. Orientation of this
        // image is always return value 6 (ORIENTATION_ROTATE_90) no matter if
        // it is captured in landscape or portrait mode
        //
        Bitmap bmp = BitmapFactory.decodeFile(imagePath.getAbsolutePath());

        //
        // save as : I am compressing this image and writing it back. Orientation 
        //of this image always returns value 0 (ORIENTATION_UNDEFINED)
        imagePath = new File(imagePath.getAbsolutePath().replace(".jpg", "_1.jpg"));



        FileOutputStream fos0 = new FileOutputStream(imagePath);
        boolean b = bmp.compress(CompressFormat.JPEG, 10, fos0);
        fos0.flush();
        fos0.close();
        fos0 = null;

After compression and saving, image is rotated by 90 degrees though ExifInterface returns 0 (ORIENTATION_UNDEFINED). Any pointer, how can I retain the orientation of source image; in this case it is 6 (or ORIENTATION_ROTATE_90).

thanks.

Stefan
  • 5,203
  • 8
  • 27
  • 51
iuq
  • 1,487
  • 1
  • 20
  • 42

1 Answers1

0

Following code will return Angle of image by its ORIENTATION.

public static float rotationForImage(Context context, Uri uri) {
        if (uri.getScheme().equals("content")) {
        String[] projection = { Images.ImageColumns.ORIENTATION };
        Cursor c = context.getContentResolver().query(
                uri, projection, null, null, null);
        if (c.moveToFirst()) {
            return c.getInt(0);
        }
    } else if (uri.getScheme().equals("file")) {
        try {
            ExifInterface exif = new ExifInterface(uri.getPath());
            int rotation = (int)exifOrientationToDegrees(
                    exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL));
            return rotation;
        } catch (IOException e) {
            Log.e(TAG, "Error checking exif", e);
        }
    }
        return 0f;
    }

    private static float exifOrientationToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
        return 90;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
        return 180;
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
        return 270;
    }
    return 0;
}
}
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • thanks Chintan for response. What you have shown isn't exactly my problem. I know the orientation. Problem is, after compression, saved image always automatically rotated by 90 degrees and return 0 or UNDEFINED if queried through ExifInterface. What I want is, same orientation of compressed image while writing to file. Hope I've explained the issue. – iuq Dec 17 '12 at 18:28