본문 바로가기
안드로이드 코틀린

[Android][코틀린] 이미지 필터 만들기

by teamnova 2024. 1. 2.

오늘은 코틀린 비트맵 합치기

https://stickode.tistory.com/1057

 

코틀린 배경처리

https://stickode.tistory.com/1058

 

에 이어 필터 기능을 만들어 보겠습니다.

자바 버전 >> https://stickode.tistory.com/961

 

[Android][Java] 이미지 필터 만들기

오늘은 비트맵 합치기 https://stickode.tistory.com/948 사람 배경 만들기 https://stickode.tistory.com/949 에 이어서 필터만들기를 해보겠습니다. 코드에 사용된 코드에 대한 설명은 위에 글들에 있으니 한번씩

stickode.tistory.com

 

MainActivity.kt

class MainActivity : AppCompatActivity() {

    // 필요한 뷰 및 비트맵 변수 선언
    private lateinit var imageView: ImageView
    private lateinit var imageView3: ImageView
    private lateinit var bitmap1: Bitmap
    private lateinit var ImageCover: Button

    // 액티비티 생성 시 호출되는 메서드
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 레이아웃 설정
        setContentView(R.layout.activity_main)

        // 뷰와 버튼 초기화
        imageView = findViewById(R.id.imageView)
        imageView3 = findViewById(R.id.imageView3)
        ImageCover = findViewById(R.id.ImageCover)

        // 첫 번째 이미지뷰에 비트맵 설정
        bitmap1 = BitmapFactory.decodeResource(applicationContext.resources, R.drawable.img_3)
        imageView.setImageBitmap(bitmap1)

        // 버튼에 클릭 리스너 등록
        ImageCover.setOnClickListener {
            // 첫 번째 이미지뷰에서 비트맵을 가져옴
            val drawable1 = imageView.drawable as BitmapDrawable
            val dst = drawable1.bitmap

            // 새로운 비트맵 생성 (크기는 원본 이미지와 동일)
            val segmentedBitmap = Bitmap.createBitmap(dst.width, dst.height, Bitmap.Config.ARGB_8888)

            // 두 번째 이미지뷰에서 비트맵을 가져옴
            val drawable2 = imageView.drawable as BitmapDrawable
            val dst_ = drawable2.bitmap

            // 필터 색상 배열 생성
            val filterColor = filterColors(dst_.width, dst_.height)

            // 필터된 비트맵 생성
            val filterBitmap = Bitmap.createBitmap(filterColor, dst_.width, dst_.height, Bitmap.Config.ARGB_8888)

            // 캔버스 생성하여 이미지를 그림
            val canvas = Canvas(segmentedBitmap)
            val paint = Paint()
            canvas.drawBitmap(dst_, 0f, 0f, paint)

            // 필터링된 이미지를 SRC_OVER 모드로 그림
            paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)
            canvas.drawBitmap(filterBitmap, 0f, 0f, paint)

            // 두 번째 이미지뷰에 필터링된 이미지 설정
            imageView3.setImageBitmap(segmentedBitmap)
        }
    }

    @ColorInt
    private fun filterColors(maskWidth: Int, maskHeight: Int): IntArray {
        val colors = IntArray(maskWidth * maskHeight)
        for (i in 0 until maskWidth * maskHeight) {
            // 색상 배열을 색상 값으로 초기화 (투명도: 128, 빨강: 255, 녹색: 0, 파랑: 255)
            colors[i] = Color.argb(128, 255, 0, 255)
        }
        return colors
    }
}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />
    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="300dp"
        android:layout_height="300dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        tools:srcCompat="@tools:sample/avatars" />
    <Button
        android:id="@+id/ImageCover"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="필터"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>