Android - Gallery & Camera Rotation problem. Fixed Camera preview but photo from Gallery shows wrong...
I almost checked all the Q&A about this topic. However, I couldn't find the right solution yet.
I am developing OCR app and my phone had an orientation problem. And finally, I fixed the Camera Preview part. And OCR works fine with the correctly oriented image.
However, Now, I have another problem with a photo from the Gallery. Only the photos that I took a picture with the Camera button(left button) has that problem when I load it from the gallery. The other photos from Google worked fine. So, I think the problem occurs in Camera function because it didn't change the orientation of the Google photos or any other photos from other apps.
So, I thought it is the saved file problem. So, I tried this method.
public static void saveBitmaptoJpeg(Bitmap bitmap, String folder, String name) {
String ex_storage = Environment.getExternalStorageDirectory().getAbsolutePath(); // Get Absolute Path in External Sdcard
String foler_name = "/" + folder + "/";
String file_name = name + ".jpg";
String string_path = ex_storage + foler_name;
File file_path;
try {
file_path = new File(string_path);
if (!file_path.isDirectory()) {
file_path.mkdirs();
}
FileOutputStream out = new FileOutputStream(string_path + file_name);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (FileNotFoundException exception) {
Log.e("FileNotFoundException", exception.getMessage());
} catch (IOException exception) {
Log.e("IOException", exception.getMessage());
}
}
to save. And load the correctly orientated photo. However, it doesn't work. So, I check the orientation of the photo with these methods.
public Bitmap modifyOrientation(Bitmap bitmap, String image_absolute_path) throws IOException {
ExifInterface ei = new ExifInterface(image_absolute_path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.d(TAG, "갤러리 orientation: " + orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotate(bitmap, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotate(bitmap, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotate(bitmap, 270);
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
return flip(bitmap, true, false);
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
return flip(bitmap, false, true);
default:
return bitmap;
}
}
public 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);
}
public Bitmap flip(Bitmap bitmap, boolean horizontal, boolean vertical) {
Matrix matrix = new Matrix();
matrix.preScale(horizontal ? -1 : 1, vertical ? -1 : 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
And it says the orientation is 0. On the other hand, when I take a photo, the orientation is 6.(*ㅡ oriented photo to |, * is top)
And I used these methods for Camera.
// 카메라 결과 알맞게 조절
private int resolveBitmapOrientation(File bitmapFile) {
Log.d(TAG, "resolveBitmapOrientation: " + bitmapFile.getAbsolutePath());
if (!bitmapFile.exists()) {
Log.d(TAG, "bitmapFile 존재하지 않음.");
} else {
Log.d(TAG, "bitmapFile 존재함.");
}
ExifInterface exif = null;
try {
exif = new ExifInterface(bitmapFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
}
private Bitmap applyOrientation(Bitmap bitmap, int orientation) {
int rotate = 0;
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;
default:
return bitmap;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
I thought the methods for the Camera may be helpful for the Gallery photo too. So, I applied those methods. But it didn't work either.
How can I fix this problem?
java android
add a comment |
I almost checked all the Q&A about this topic. However, I couldn't find the right solution yet.
I am developing OCR app and my phone had an orientation problem. And finally, I fixed the Camera Preview part. And OCR works fine with the correctly oriented image.
However, Now, I have another problem with a photo from the Gallery. Only the photos that I took a picture with the Camera button(left button) has that problem when I load it from the gallery. The other photos from Google worked fine. So, I think the problem occurs in Camera function because it didn't change the orientation of the Google photos or any other photos from other apps.
So, I thought it is the saved file problem. So, I tried this method.
public static void saveBitmaptoJpeg(Bitmap bitmap, String folder, String name) {
String ex_storage = Environment.getExternalStorageDirectory().getAbsolutePath(); // Get Absolute Path in External Sdcard
String foler_name = "/" + folder + "/";
String file_name = name + ".jpg";
String string_path = ex_storage + foler_name;
File file_path;
try {
file_path = new File(string_path);
if (!file_path.isDirectory()) {
file_path.mkdirs();
}
FileOutputStream out = new FileOutputStream(string_path + file_name);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (FileNotFoundException exception) {
Log.e("FileNotFoundException", exception.getMessage());
} catch (IOException exception) {
Log.e("IOException", exception.getMessage());
}
}
to save. And load the correctly orientated photo. However, it doesn't work. So, I check the orientation of the photo with these methods.
public Bitmap modifyOrientation(Bitmap bitmap, String image_absolute_path) throws IOException {
ExifInterface ei = new ExifInterface(image_absolute_path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.d(TAG, "갤러리 orientation: " + orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotate(bitmap, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotate(bitmap, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotate(bitmap, 270);
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
return flip(bitmap, true, false);
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
return flip(bitmap, false, true);
default:
return bitmap;
}
}
public 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);
}
public Bitmap flip(Bitmap bitmap, boolean horizontal, boolean vertical) {
Matrix matrix = new Matrix();
matrix.preScale(horizontal ? -1 : 1, vertical ? -1 : 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
And it says the orientation is 0. On the other hand, when I take a photo, the orientation is 6.(*ㅡ oriented photo to |, * is top)
And I used these methods for Camera.
// 카메라 결과 알맞게 조절
private int resolveBitmapOrientation(File bitmapFile) {
Log.d(TAG, "resolveBitmapOrientation: " + bitmapFile.getAbsolutePath());
if (!bitmapFile.exists()) {
Log.d(TAG, "bitmapFile 존재하지 않음.");
} else {
Log.d(TAG, "bitmapFile 존재함.");
}
ExifInterface exif = null;
try {
exif = new ExifInterface(bitmapFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
}
private Bitmap applyOrientation(Bitmap bitmap, int orientation) {
int rotate = 0;
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;
default:
return bitmap;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
I thought the methods for the Camera may be helpful for the Gallery photo too. So, I applied those methods. But it didn't work either.
How can I fix this problem?
java android
add a comment |
I almost checked all the Q&A about this topic. However, I couldn't find the right solution yet.
I am developing OCR app and my phone had an orientation problem. And finally, I fixed the Camera Preview part. And OCR works fine with the correctly oriented image.
However, Now, I have another problem with a photo from the Gallery. Only the photos that I took a picture with the Camera button(left button) has that problem when I load it from the gallery. The other photos from Google worked fine. So, I think the problem occurs in Camera function because it didn't change the orientation of the Google photos or any other photos from other apps.
So, I thought it is the saved file problem. So, I tried this method.
public static void saveBitmaptoJpeg(Bitmap bitmap, String folder, String name) {
String ex_storage = Environment.getExternalStorageDirectory().getAbsolutePath(); // Get Absolute Path in External Sdcard
String foler_name = "/" + folder + "/";
String file_name = name + ".jpg";
String string_path = ex_storage + foler_name;
File file_path;
try {
file_path = new File(string_path);
if (!file_path.isDirectory()) {
file_path.mkdirs();
}
FileOutputStream out = new FileOutputStream(string_path + file_name);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (FileNotFoundException exception) {
Log.e("FileNotFoundException", exception.getMessage());
} catch (IOException exception) {
Log.e("IOException", exception.getMessage());
}
}
to save. And load the correctly orientated photo. However, it doesn't work. So, I check the orientation of the photo with these methods.
public Bitmap modifyOrientation(Bitmap bitmap, String image_absolute_path) throws IOException {
ExifInterface ei = new ExifInterface(image_absolute_path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.d(TAG, "갤러리 orientation: " + orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotate(bitmap, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotate(bitmap, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotate(bitmap, 270);
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
return flip(bitmap, true, false);
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
return flip(bitmap, false, true);
default:
return bitmap;
}
}
public 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);
}
public Bitmap flip(Bitmap bitmap, boolean horizontal, boolean vertical) {
Matrix matrix = new Matrix();
matrix.preScale(horizontal ? -1 : 1, vertical ? -1 : 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
And it says the orientation is 0. On the other hand, when I take a photo, the orientation is 6.(*ㅡ oriented photo to |, * is top)
And I used these methods for Camera.
// 카메라 결과 알맞게 조절
private int resolveBitmapOrientation(File bitmapFile) {
Log.d(TAG, "resolveBitmapOrientation: " + bitmapFile.getAbsolutePath());
if (!bitmapFile.exists()) {
Log.d(TAG, "bitmapFile 존재하지 않음.");
} else {
Log.d(TAG, "bitmapFile 존재함.");
}
ExifInterface exif = null;
try {
exif = new ExifInterface(bitmapFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
}
private Bitmap applyOrientation(Bitmap bitmap, int orientation) {
int rotate = 0;
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;
default:
return bitmap;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
I thought the methods for the Camera may be helpful for the Gallery photo too. So, I applied those methods. But it didn't work either.
How can I fix this problem?
java android
I almost checked all the Q&A about this topic. However, I couldn't find the right solution yet.
I am developing OCR app and my phone had an orientation problem. And finally, I fixed the Camera Preview part. And OCR works fine with the correctly oriented image.
However, Now, I have another problem with a photo from the Gallery. Only the photos that I took a picture with the Camera button(left button) has that problem when I load it from the gallery. The other photos from Google worked fine. So, I think the problem occurs in Camera function because it didn't change the orientation of the Google photos or any other photos from other apps.
So, I thought it is the saved file problem. So, I tried this method.
public static void saveBitmaptoJpeg(Bitmap bitmap, String folder, String name) {
String ex_storage = Environment.getExternalStorageDirectory().getAbsolutePath(); // Get Absolute Path in External Sdcard
String foler_name = "/" + folder + "/";
String file_name = name + ".jpg";
String string_path = ex_storage + foler_name;
File file_path;
try {
file_path = new File(string_path);
if (!file_path.isDirectory()) {
file_path.mkdirs();
}
FileOutputStream out = new FileOutputStream(string_path + file_name);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (FileNotFoundException exception) {
Log.e("FileNotFoundException", exception.getMessage());
} catch (IOException exception) {
Log.e("IOException", exception.getMessage());
}
}
to save. And load the correctly orientated photo. However, it doesn't work. So, I check the orientation of the photo with these methods.
public Bitmap modifyOrientation(Bitmap bitmap, String image_absolute_path) throws IOException {
ExifInterface ei = new ExifInterface(image_absolute_path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.d(TAG, "갤러리 orientation: " + orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotate(bitmap, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotate(bitmap, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotate(bitmap, 270);
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
return flip(bitmap, true, false);
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
return flip(bitmap, false, true);
default:
return bitmap;
}
}
public 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);
}
public Bitmap flip(Bitmap bitmap, boolean horizontal, boolean vertical) {
Matrix matrix = new Matrix();
matrix.preScale(horizontal ? -1 : 1, vertical ? -1 : 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
And it says the orientation is 0. On the other hand, when I take a photo, the orientation is 6.(*ㅡ oriented photo to |, * is top)
And I used these methods for Camera.
// 카메라 결과 알맞게 조절
private int resolveBitmapOrientation(File bitmapFile) {
Log.d(TAG, "resolveBitmapOrientation: " + bitmapFile.getAbsolutePath());
if (!bitmapFile.exists()) {
Log.d(TAG, "bitmapFile 존재하지 않음.");
} else {
Log.d(TAG, "bitmapFile 존재함.");
}
ExifInterface exif = null;
try {
exif = new ExifInterface(bitmapFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
}
private Bitmap applyOrientation(Bitmap bitmap, int orientation) {
int rotate = 0;
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;
default:
return bitmap;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
I thought the methods for the Camera may be helpful for the Gallery photo too. So, I applied those methods. But it didn't work either.
How can I fix this problem?
java android
java android
edited Nov 16 '18 at 6:27
c-an
asked Nov 16 '18 at 6:14
c-anc-an
567426
567426
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332408%2fandroid-gallery-camera-rotation-problem-fixed-camera-preview-but-photo-from%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332408%2fandroid-gallery-camera-rotation-problem-fixed-camera-preview-but-photo-from%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown