Life Life Life

이미지파일 회전 정보 조회 및 회전 본문

개발/안드로이드

이미지파일 회전 정보 조회 및 회전

네버그린 2014. 4. 2. 15:03

이미지 파일에 대한 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;

}