→ 검색 해보니, 이미지 회전 현상이 빈번하게 발생하나 보다~ㅋㅋ
그러다 발견한 블로그!!
https://stickyny.tistory.com/95
안드로이드 이미지가 돌아갈 때, 회전될 때! Exif 메타정보 이용하기
샤오미 노트와 넥서스 기기에서는 재현되지 않는다. LG G4에서 재현된다. 앨범에 저장된 세로 이미지를 프로필 사진으로 등록하면 90도 회전되는 현상을 겪었다. 솔루션을 요약하자면, 사진의 메�
stickyny.tistory.com
출처: https://terms.naver.com/entry.nhn?docId=1254253&cid=40942&categoryId=32828
교환이미지 파일형식
디지털카메라의 이미지 파일 안에 저장되어 있는 화상 파일 형식이다. 약칭은 Exif 또는 EXIF이다. 일본전자공업진흥협회(JEIDA)에 의해 만들어졌다. 저장된 정보를 확인하려면 exif 규격을 지원하는
terms.naver.com
→ ExifiInterface class에서 ExifInterface.TAG_ORIENTATION 값이 회전된 각도이다.
이 값이 0 or null 이면 사진이 회전되지 않은것
값이 있는 만큼 이미지를 회전 시켜주면 된다.
ExifInterface exif = null;
try {
exif = new ExifInterface(path);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
→ 이렇게 Exif를 선언해주고 exif.getAttributeInt를 통해서 TAG_ORIENTATION값을 얻어 온다!
Bitmap bmRotated = rotateBitmap(#######, orientation);
→ ######에는 원래 image(회전되서 나오는)의 bitmap변수를 적는다.
→ ###### image를 orientation만큼 rotate시킨 image를 bmRotated라 하겠다.
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
}
catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
How to get the Correct orientation of the image selected from the Default Image gallery
I have gone through some of the links to get the correct image orientation of the image selected from the default image gallery to be worked standard in all devices the exif tag always returns 0. ...
stackoverflow.com
https://stickyny.tistory.com/95
안드로이드 이미지가 돌아갈 때, 회전될 때! Exif 메타정보 이용하기
샤오미 노트와 넥서스 기기에서는 재현되지 않는다. LG G4에서 재현된다. 앨범에 저장된 세로 이미지를 프로필 사진으로 등록하면 90도 회전되는 현상을 겪었다. 솔루션을 요약하자면, 사진의 메�
stickyny.tistory.com
→ Photo를 File형식으로 만든다음 Photo.getAbsolutePath()를 이용해서 image를 rotate할 수 있다.
// 사진의 회전 정보 얻어오는 부분
ExifInterface exif = null;
try {
exif = new ExifInterface(mPhotoFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
// 찍은 사진 mPhotoFile을 bitmap으로 decodeFile
Bitmap cameraphoto = BitmapFactory.decodeFile(mPhotoFile.getAbsolutePath());
// 찍은 사진 rotate
Bitmap cameraRotated = rotateBitmap(cameraphoto, orientation);
// imageview 사진 뜨게함
image.setImageBitmap(cameraRotated);
→ 진짜 경로명인 절대경로명을 써줘야한다고 발견...!
Uri filePath = data.getData();
→ data.getData()를 통해 Uri를 filePath라고 선언
// 사진의 절대 경로명
Uri mPhotoUri = Uri.parse(getRealPathFromURI(filePath));
→ filePath라는 경로명에서 이제 진짜 경로명을 찾는 getRealPathFromURI 함수를 이용
// 사진의 절대경로명 찾게해줌 (album-rotate함수때 필요)
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) {
// Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx); cursor.close();
}
return result;
}
→이렇게 해서 경로명 찾아준 후에 ExifInterface통해서 사진 rotate하면 됨!
ExifInterface exif = null;
try {
int batchNum = 0;
InputStream buf = getContentResolver().openInputStream(filePath);
Bitmap albumphoto = BitmapFactory.decodeStream(buf);
buf.close();
// 사진의 회전 정보 얻어오는 부분
exif = new ExifInterface(mPhotoUri.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
// 선택한 사진 rotate
Bitmap albumRotated = rotateBitmap(albumphoto, orientation);
// imageview 사진 뜨게함
image.setImageBitmap(albumRotated);
// ~~~~~~ 이 뒤에 다른 코드들 있음~
}
[안드로이드] 갤러리의 실제경로 가져오기
managedQuery 메소드가 Deprecated 되었다. 이외에 startManagingCursor도 Deprecated 되었다. 그래서 새로운 방식으로 소스를 변경해야 했다. 예전의방식 public String getPath(Uri uri) { String[] projection..
dd00oo.tistory.com
ⓐ Album들어가는 Android code 정리
github.com/buglossJisoo/AI_Mushroom_App
buglossJisoo/AI_Mushroom_App
AI Mushroom App final code(JAVA). Contribute to buglossJisoo/AI_Mushroom_App development by creating an account on GitHub.
github.com