Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Find the orientation of photo was took by camera

I need to take a photo, find out its orientation, draw it on canvas and rotate the canvas. I need help to find out orientation of the photo

like image 671
StoneHeart Avatar asked Jan 10 '12 17:01

StoneHeart


2 Answers

You need to use the EXIF tag embedded in the photo:

private int getExifOrientation() {
  ExifInterface exif;
  int orientation = 0;
  try {
    exif = new ExifInterface( mImagePath );
    orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, 1 );
  } catch ( IOException e ) {
    e.printStackTrace();
  }
  Log.d(TAG, "got orientation " + orientation);
  return orientation;
}

However, the actual EXIF value returned is sorta weird. It allows for all variety of rotation and mirroring. The best reference I've found is here. In general, after you get the orientation, you'll want to run it through a lookup function to get the rotation in degrees:

private int getBitmapRotation() {
  int rotation = 0;
  switch ( getExifOrientation() ) {
    case ExifInterface.ORIENTATION_ROTATE_180:
      rotation = 180;
      break;
    case ExifInterface.ORIENTATION_ROTATE_90:
      rotation = 90;
      break;
    case ExifInterface.ORIENTATION_ROTATE_270:
      rotation = 270;
      break;
  }

  return rotation;
}
like image 115
elijah Avatar answered Oct 02 '22 08:10

elijah


I was searching for a similar solution to my problem. I am taking a photo from a camera or gallery and turning it into a bitmap to use in my app. The problem was that photos taken in PORTRAIT were being rotated -90 degrees.

Searching for answers I found this post and https://stackoverflow.com/a/11081918/3062284 which are very similar. I actually used the latter post. However, like @StoneHeart I was getting an error reading the Image Path.

I was using this code in my onActivityResult():

case CAMERA_REQUEST_CODE:
if (resultCode == RESULT_OK);
uriImage = data.getData(); 
ExifInterface exif = new ExifInterface(uriImage.getPath());

Since this error was not addressed in previous comments I thought I would share the solution I found using https://stackoverflow.com/a/10564727/3062284

I needed the real path for the file using this method from the post.

     private String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

and editing my code to the following:

case CAMERA_REQUEST_CODE:
if (resultCode == RESULT_OK);
uriImage = data.getData();
String imagePath = getRealPathFromURI(uriImage); 
ExifInterface exif = new ExifInterface(imagePath);

and that fixed the "JHEAD can't open file" error I was getting.

like image 25
mjwheat Avatar answered Oct 02 '22 09:10

mjwheat