본문 바로가기

AOS - Kotlin

[android/kotlin] 안드로이드 카메라 이미지 회전, 리사이징

반응형
SMALL

카메라로 찍은 이미지 파일 방향에 맞게 회전하고 리사이징 하는 코드 , bitmap으로 리턴

 

//path 파일 저장된 경로
fun modifyImage(path: String) : Bitmap{
    val bmpFactoryOptions = BitmapFactory.Options()
    var src : Bitmap

    //4분의1 사이즈로 리사이징
    bmpFactoryOptions.inSampleSize = 4
    bmpFactoryOptions.inJustDecodeBounds = false
    var bm = BitmapFactory.decodeFile(path, bmpFactoryOptions)

    val exif = ExifInterface(path)
    val orientString: String? = exif.getAttribute(ExifInterface.TAG_ORIENTATION)
    val orientation = orientString?.toInt() ?: ExifInterface.ORIENTATION_NORMAL

    var rotationAngle = 0
    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90
    if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180
    if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270

    val matrix = Matrix()
    matrix.setRotate(rotationAngle.toFloat(), bm.width.toFloat(), bm.height.toFloat())
    src = Bitmap.createBitmap(bm, 0, 0, bm.width, bm.height, matrix, true)

    return src
}
반응형
LIST