Skip to content

Instantly share code, notes, and snippets.

@walkingError
Created December 8, 2016 10:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkingError/9315c2255aee64e13ad7a5cd7c95a0b2 to your computer and use it in GitHub Desktop.
Save walkingError/9315c2255aee64e13ad7a5cd7c95a0b2 to your computer and use it in GitHub Desktop.
Camera orientation
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.net.Uri;
import java.io.IOException;
public class BitmapUtils {
public static int getCameraPhotoOrientation(Uri imageUri){
int rotate = 0;
try {
String filePath = imageUri.getPath();
ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
rotate = 0;
break;
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) {
e.printStackTrace();
}
return rotate;
}
public static Bitmap getBitmapWithExif(Uri myUri , Bitmap bitmap) throws NullPointerException {
ExifInterface exif;
int angle = 0;
try {
exif = new ExifInterface(myUri.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
} catch (IOException e) {
e.printStackTrace();
}
Matrix matrix1 = new Matrix();
matrix1.postRotate(angle);
//Create bitmap with new values.
Bitmap photo = Bitmap.createBitmap( bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix1, true);
return photo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment