How Rotate Image Taken From Camera Or Gallery.?
I am capturing image from camera and selecting image from gallery. In samsung devices the images gets rotate after captured. I want rotate image to straight if they are rotated. I
Solution 1:
profileImage = destination;
You took the thumbnail which is a Bitmap and wrote it to file.
Then after that you used that file to extract an exifinterface.
But bitmaps do not contain exif information. And hence your file does not either.
So your orientation is always null;
If you had used profileImage
to launch the camera intent then leave it as is.
So remove above statement.
Solution 2:
public int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
int rotate = 0;
try {
context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
Log.i("RotateImage", "Exif orientation: " + orientation);
Log.i("RotateImage", "Rotate value: " + rotate);
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
And put this code in Activity result method and get value to rotate image...
String selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
int rotateImage = getCameraPhotoOrientation(MyActivity.this, selectedImage, filePath);
Rotate Bitmap using this
public static Bitmap rotate(Bitmap bitmap, float degrees) {
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
Post a Comment for "How Rotate Image Taken From Camera Or Gallery.?"