Life Life Life
이미지파일 회전 정보 조회 및 회전 본문
이미지 파일에 대한 EXIF 정보를 조회하여 이미지 회전
public static Bitmap makeBitmap(String filename)
{
File file = new File(filename);
if (file.exists())
{
Bitmap downSize = null;
try
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
downSize = BitmapFactory.decodeFile(filename, options);
ExifInterface exif = new ExifInterface(filename);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int exifDegree = exifOrientationToDegrees(exifOrientation);
return rotate(downSize, exifDegree);
}
catch (Exception e)
{
return downSize;
}
}
else
{
return null;
}
}
/**
* EXIF정보를 회전각도로 변환하는 메서드
*
* @param exifOrientation
* EXIF 회전각
* @return 실제 각도
*/
private static int exifOrientationToDegrees(int exifOrientation)
{
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90)
{
return 90;
}
else
{
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180)
{
return 180;
}
else
{
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270)
{
return 270;
}
}
}
return 0;
}
/**
* 이미지를 회전시킵니다.
*
* @param bitmap
* 비트맵 이미지
* @param degrees
* 회전 각도
* @return 회전된 이미지
*/
private static Bitmap rotate(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 converted = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
if (bitmap != converted)
{
bitmap.recycle();
bitmap = converted;
}
}
catch (OutOfMemoryError ex)
{
// System.gc();
// 메모리가 부족하여 회전을 시키지 못할 경우 그냥 원본을 반환합니다.
}
}
return bitmap;
}
'개발 > 안드로이드' 카테고리의 다른 글
BroadcastReceiver 편하게 관리하기 (0) | 2014.04.10 |
---|---|
TextView에 폰트 적용하기 (0) | 2014.04.04 |
안드로이드 최상위 Toast 만들기 (0) | 2014.04.02 |
Dialog 및 ProgressDialog 하드웨어 Key 막기 (0) | 2014.04.02 |
GPS활성하게끔 설정화면 이동 (0) | 2014.04.02 |