본문 바로가기

Android/Bitmap

[Android] 비트맵 이미지 생성, 크롭, 회전

1. 비트맵 이미지 생성

//넓이, 높이 조절로 이미지 크롭 가능
Bitmap.createBitmap([비트맵 객체], [x시작],[y시작], [너비], [높이])
Bitmap.createBitmap(bitmap, 0,0, width, height)

 

2. 비트맵 이미지 생성 및 크롭

//중앙을 기준으로 크롭
fun cropBitMap(bitmap: Bitmap, cropWidth: Int, cropHeight: Int): Bitmap {
	//(이미지 넓이 - 크롭 넓이)/ 2
    val x = (bitmap.width - cropWidth) / 2
    
    //(이미지 높이 - 높이)/ 2
    val y = (bitmap.height - cropHeight) / 2

    return Bitmap.createBitmap(bitmap, x, y, cropWidth, cropHeight)
}

 

3. 90도 회전

val matrix = Matrix()
matrix.setRotate(90f)

Bitmap.createBitmap(bitmap!!, 0,0, width, height, matrix, true)

 

4. 외부 저장소 비트맵 이미지를 Bitmap 객체로 변환하는 코드

BitmapFactory.decodeFile([경로], [opts 옵션]);
BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().toString() + FILE_NAME, null);

 

 

예시용 코드입니다. 감사합니다.

'Android > Bitmap' 카테고리의 다른 글

[Android] 비트맵 이미지를 파일로 변환  (0) 2024.04.19