问题
I am capturing image from camera and selecting image from gallery. In samsung devices the images gets rotate after captured.
I want to rotate image to straight if they are rotated.
I tried to do it but its not working. I am getting EXIF orientation as 0 always though the image is rotated.
private void onCaptureImageResult(Intent data) {
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Uri tempUri = getImageUri(getApplicationContext(), thumbnail);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
ExifInterface ei = new ExifInterface(String.valueOf(finalFile));
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(finalFile));
Bitmap rotatedBitmap = null;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(bitmap, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = bitmap;
break;
}
if(rotatedBitmap != null)
{
profile_image.setImageBitmap(rotatedBitmap);
thumbnail = rotatedBitmap;
}
}
catch (IOException ex) { }
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
true);
}
What is going wrong here? Please help.. Thank you..
回答1:
Replace
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
With
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
And tell if it works
回答2:
I also faced the same issue once in my project and got it going by below code :
//imageFileUri is fileUri.getPath() that is being passed
private Bitmap getImageBitmap(String imageFileUri) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 3;
Bitmap bm = BitmapFactory.decodeFile(imageFileUri, opts);
ExifInterface exif;
String orientString = null;
try {
exif = new ExifInterface(imageFileUri);
orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
} catch (IOException e) {
if (BuildConfig.DEBUG) {
e.printStackTrace();
}
}
int orientation = (orientString != null) ? Integer.parseInt(orientString)
: ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
rotationAngle = 270;
int w = bm.getWidth();
int h = bm.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotationAngle);
return Bitmap.createBitmap(bm, 0, 0, w, h, mtx, true);
}
来源:https://stackoverflow.com/questions/40172441/rotate-image-taken-from-camera-or-gallery