To change the orientation of an image write the following code:
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = 2;
// Get the original bitmap from the filepath to which you want to change orientation
// fileName ist the filepath of the image
Bitmap cachedImage=BitmapFactory.decodeFile(fileName, o2);
int rotate = getCameraPhotoOrientation(imageViewer, fileName);
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
// Here you will get the image bitmap which has changed orientation
cachedImage = Bitmap.createBitmap(cachedImage , 0, 0, cachedImage.getWidth(), cachedImage.getHeight(), matrix, true);
Get the original photo orientation
public static int getCameraPhotoOrientation(Context context, String imagePath)
{
int rotate = 0;
try {
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;
}
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
Hope this will help you :)
0 Comment(s)