Android에서 촬영한 사진이나 앨범에서 사진을 Server Side로 전송하는 부분을 개발하다 보니 Vender 마다 차이가 있었다.
무슨 문제가 있었던 것일까?
LG Phone
세로 모드로 사진 촬영: 정상
가로 모드로 사진 촬영: 정상
Sam Sung Phone
세로 모드로 사진 촬영: 90도 회전 된 이미지가 전송
가로 모드로 사진 촬영: 정상
How can we do it?
1. Android에서 촬영된 사진은 “Orientation” 이라는 “Tag”가 존재하는데 이 Data로 사진을 촬영할 때의 Degree을 알 수 있습니다.
2. “1”번으로 Degree을 알아 보고 그 각도만큼 회전 시킨 이미지를 생성하면 됩니다.
아래는 1,2 번에 해당되는 Code을 Googing 찾아봤습니다.
/**
* 이미지 회전 각도를 알아옴.
* @param filepath
* @return 각도
*/
public synchronized static int getPhotoOrientationDegree(String filepath)
{
int degree = 0;
ExifInterface exif = null;
try
{
exif = new ExifInterface(filepath);
}
catch (IOException e)
{
Log.d(PhotoUtil.class.getSimpleName(), "Error: "+e.getMessage());
}
if (exif != null)
{
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
if (orientation != -1)
{
switch(orientation)
{
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
}
}
Log.d(PhotoUtil.class.getSimpleName(), "Photo Degree: "+degree);
return degree;
}
/**
* 이미지를 특정 각도로 회전
* @param bitmap
* @param degrees
* @return
*/
public synchronized static Bitmap getRotatedBitmap(Bitmap bitmap, int degrees)
{
if ( degrees != 0 && bitmap != null )
{
Matrix m = new Matrix();
m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2 );
try
{
Bitmap b2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
if (bitmap != b2)
{
bitmap.recycle();
bitmap = b2;
}
}
catch (OutOfMemoryError e)
{
Log.d(PhotoUtil.class.getSimpleName(), "Error: "+e.getMessage());
}
}
return bitmap;
}
Issue
이런 문제도 있을수 있습니다.
요즘 스마트폰이 좋아 사진 한 장 당 크기가 2M를 넘어갑니다. 이 정도 크기를 Server Side로 전송하는데 시간이 많이 걸리고 요금도 무시 못하기 때문에, iPhone 이든 Android 든 모든 이미지 압축을 하여 전송하게 됩니다. 그런데 이미지를 압축 하면 위의 Degree 값이 0이 됩니다.
압축 전에 원본을 돌려 놓고 압축해야 합니다.
'오래된글 > Articles' 카테고리의 다른 글
First WebSocket (0) | 2018.05.03 |
---|---|
Android Service에서 AlertDialog 띄우기 (0) | 2018.05.03 |
모바일 보안 취약점 및 대책 (0) | 2018.05.03 |
Database Design Best Practices (0) | 2018.05.03 |
Android에서 Httpclient와 WebView간 HttpSession 공유 (0) | 2018.04.19 |