Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rotation on Android, using EXIF data #135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
Expand Down Expand Up @@ -69,10 +71,30 @@ public boolean openImageFile(String filename, String directory, String mode) {
"drawable",
mContext.getPackageName());
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
File file = new File(filename, directory == null ? "" : directory);
Bitmap bitmap = res == 0 ?
BitmapFactory.decodeFile(new File(filename, directory == null ? "" : directory).toString(), bitmapOptions) :
BitmapFactory.decodeFile(file.toString(), bitmapOptions) :
BitmapFactory.decodeResource(mContext.getResources(), res);
if(bitmap != null) {

try {
ExifInterface exif = new ExifInterface(file.getAbsolutePath());
Matrix matrix = new Matrix();

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
matrix.postRotate(90);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
matrix.postRotate(180);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
matrix.postRotate(270);
}

bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // rotating bitmap
} catch (Exception e) {

}

if (bitmap != null) {
mBackgroundImage = bitmap;
mOriginalHeight = bitmap.getHeight();
mOriginalWidth = bitmap.getWidth();
Expand Down