rotate bitmap based on exif data - Android Graphics

Android examples for Graphics:Bitmap Rotate

Description

rotate bitmap based on exif data

Demo Code


import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;

public class Main {
  /**/*from w w  w.  j a  v  a  2s . c  o m*/
   * rotate bitmap based on exif data thank you
   * http://stackoverflow.com/questions/20478765/how-to-get-the-correct-orientation-of-the-image-selected-from-the-default-image
   */
  public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    Matrix matrix = new Matrix();
    switch (orientation) {
    case ExifInterface.ORIENTATION_NORMAL:
      return bitmap;
    case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
      matrix.setScale(-1, 1);
      break;
    case ExifInterface.ORIENTATION_ROTATE_180:
      matrix.setRotate(180);
      break;
    case ExifInterface.ORIENTATION_FLIP_VERTICAL:
      matrix.setRotate(180);
      matrix.postScale(-1, 1);
      break;
    case ExifInterface.ORIENTATION_TRANSPOSE:
      matrix.setRotate(90);
      matrix.postScale(-1, 1);
      break;
    case ExifInterface.ORIENTATION_ROTATE_90:
      matrix.setRotate(90);
      break;
    case ExifInterface.ORIENTATION_TRANSVERSE:
      matrix.setRotate(-90);
      matrix.postScale(-1, 1);
      break;
    case ExifInterface.ORIENTATION_ROTATE_270:
      matrix.setRotate(-90);
      break;
    default:
      return bitmap;
    }
    try {
      Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
      bitmap.recycle();
      return bmRotated;
    } catch (OutOfMemoryError e) {
      e.printStackTrace();
      return null;
    }
  }
}

Related Tutorials