Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //rotating image based on uri / file
  2. //note that uri is retrieved from contentresolver, and it returns rotate degree,
  3. //while exif returns orientation code from 1 to 10
  4.  
  5. public static Bitmap adjust_bitmap_orientation(File file, Bitmap im) throws Exception {
  6.         //File imageFile = new File(cursor.getString(0));
  7.  
  8.         if (! file.exists()) {
  9.             throw new FileNotFoundException("file missing: "+file.getAbsolutePath());
  10.         }
  11.         ExifInterface exif = new ExifInterface(
  12.                 file.getAbsolutePath());
  13.         int orientation = exif.getAttributeInt(
  14.                 ExifInterface.TAG_ORIENTATION,
  15.                 ExifInterface.ORIENTATION_NORMAL);
  16.  
  17.         //int rotate = exifToDegrees(orientation);
  18.         Log.i(LOG_TAG, "photo-orientation-file path: "+file.getAbsolutePath());
  19.  
  20.         Matrix matrix = exifMatrix(orientation);
  21.         if(matrix != null)
  22.             im = Bitmap.createBitmap(im, 0, 0, im.getWidth(), im.getHeight(), matrix, true);
  23.         return im;
  24.     }
  25.  
  26.     public static Bitmap adjust_bitmap_orientation(Uri uri, Bitmap im) throws Exception {
  27.         //File imageFile = new File(cursor.getString(0));
  28.         Cursor cursor = AppActivityManager.getContentResolver().query(uri,
  29.                 new String[] { MediaStore.Images.ImageColumns.ORIENTATION,
  30.                         MediaStore.Images.ImageColumns.DATA }, null, null, null);
  31.  
  32.         if (cursor.getCount() != 1) {
  33.             Log.e(LOG_TAG, "no orientation: "+uri.toString());
  34.             throw new Exception(("No orientation for: "+uri.toString()));
  35.             //return -1; //not found
  36.         }
  37.  
  38.         cursor.moveToFirst();
  39.         int rotation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
  40.         String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
  41.  
  42.         Log.i(LOG_TAG, "photo-orientation-uri: " + uri.toString());
  43.         if (rotation != 0) {
  44.             //cursor returned orientation is rotation degree!
  45.             Matrix matrix = new Matrix();
  46.             matrix.postRotate(rotation);
  47.             Log.i(LOG_TAG, "has rotation: " + rotation + ", matrix: " + matrix.toString());
  48.             im = Bitmap.createBitmap(im, 0, 0, im.getWidth(), im.getHeight(), matrix, true);
  49.         }
  50.         return im;
  51.     }
  52.  
  53. public static Matrix exifMatrix(int orientation) throws Exception{
  54.         Matrix mtx = new Matrix();
  55.         Log.i(LOG_TAG, "exif: "+orientation);
  56.  
  57.         switch (orientation) {
  58.             case ExifInterface.ORIENTATION_UNDEFINED:
  59.                 //throw new Exception("Unable to get orientation");
  60.                 Log.e(LOG_TAG, "exif orientation failed / no exif");
  61.                 mtx = null;
  62.                 break;
  63.             case ExifInterface.ORIENTATION_NORMAL:
  64.                 mtx = null;
  65.                 break;
  66.             case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
  67.                 mtx.setScale(-1, 1);
  68.                 break;
  69.             case ExifInterface.ORIENTATION_ROTATE_180:
  70.                 mtx.setRotate(180);
  71.                 break;
  72.             case ExifInterface.ORIENTATION_FLIP_VERTICAL:
  73.                 mtx.setRotate(180);
  74.                 mtx.postScale(-1, 1);
  75.                 break;
  76.             case ExifInterface.ORIENTATION_TRANSPOSE:
  77.                 mtx.setRotate(90);
  78.                 mtx.postScale(-1, 1);
  79.                 break;
  80.             case ExifInterface.ORIENTATION_ROTATE_90:
  81.                 Log.i(LOG_TAG, "rotate 90");
  82.                 mtx.setRotate(90);
  83.                 break;
  84.             case ExifInterface.ORIENTATION_TRANSVERSE:
  85.                 mtx.setRotate(-90);
  86.                 mtx.postScale(-1, 1);
  87.                 break;
  88.             case ExifInterface.ORIENTATION_ROTATE_270:
  89.                 mtx.setRotate(-90);
  90.                 break;
  91.             default:
  92.                 return null;
  93.         }
  94.  
  95.         return mtx;
  96.     }