android kotlin - Bitmap color matrix set scale

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.content.Context
import android.graphics.*
import android.os.Bundle
import android.widget.*
import java.io.IOException


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        val imageView = findViewById<ImageView>(R.id.imageView)
        val imageView2 = findViewById<ImageView>(R.id.imageView2)
        val seekBarRed = findViewById<SeekBar>(R.id.seekBarRed)
        val seekBarGreen = findViewById<SeekBar>(R.id.seekBarGreen)
        val seekBarBlue = findViewById<SeekBar>(R.id.seekBarBlue)
        val seekBarAlpha = findViewById<SeekBar>(R.id.seekBarAlpha)
        val tvIntensity = findViewById<TextView>(R.id.tvIntensity)


        // get the bitmap from assets folder
        val bitmap = assetsToBitmap("flower103.jpg")


        bitmap?.apply {
            // show original bitmap in first image view
            imageView.setImageBitmap(this)

            // show initial bitmap on second image view
            imageView2.setImageBitmap(scaleChannels())


            seekBarRed.setOnSeekBarChangeListener(object:SeekBar
            .OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    val red = progress.toFloat()/10
                    val green = seekBarGreen.progress.toFloat()/10
                    val blue = seekBarBlue.progress.toFloat()/10
                    val alpha = seekBarAlpha.progress.toFloat()/10

                    imageView2.setImageBitmap(
                        scaleChannels(red,green,blue,alpha))
                    tvIntensity.text = "(Red $red : Green $green : "
                    tvIntensity.append("Blue $blue : Alpha $alpha)")
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })


            seekBarGreen.setOnSeekBarChangeListener(object:SeekBar
            .OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    val red = seekBarRed.progress.toFloat()/10
                    val green = progress.toFloat()/10
                    val blue = seekBarBlue.progress.toFloat()/10
                    val alpha = seekBarAlpha.progress.toFloat()/10

                    imageView2.setImageBitmap(
                        scaleChannels(red,green,blue,alpha))
                    tvIntensity.text = "(Red $red : Green $green : "
                    tvIntensity.append("Blue $blue : Alpha $alpha)")
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })


            seekBarBlue.setOnSeekBarChangeListener(object:SeekBar
            .OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    val red = seekBarRed.progress.toFloat()/10
                    val green = seekBarGreen.progress.toFloat()/10
                    val blue = progress.toFloat()/10
                    val alpha = seekBarAlpha.progress.toFloat()/10

                    imageView2.setImageBitmap(
                        scaleChannels(red,green,blue,alpha))
                    tvIntensity.text = "(Red $red : Green $green : "
                    tvIntensity.append("Blue $blue : Alpha $alpha)")
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })


            seekBarAlpha.setOnSeekBarChangeListener(object:SeekBar
            .OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    val red = seekBarRed.progress.toFloat()/10
                    val green = seekBarGreen.progress.toFloat()/10
                    val blue = seekBarBlue.progress.toFloat()/10
                    val alpha = progress.toFloat()/10

                    imageView2.setImageBitmap(
                        scaleChannels(red,green,blue,alpha))
                    tvIntensity.text = "(Red $red : Green $green : "
                    tvIntensity.append("Blue $blue : Alpha $alpha)")
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })
        }
    }
}


// extension function to get bitmap from assets
fun Context.assetsToBitmap(fileName:String):Bitmap?{
    return try {
        val stream = assets.open(fileName)
        BitmapFactory.decodeStream(stream)
    } catch (e: IOException) {
        e.printStackTrace()
        null
    }
}


// extension function to scale bitmap color channels
fun Bitmap.scaleChannels(
    red:Float = 1F,
    green:Float = 1F,
    blue:Float = 1F,
    alpha:Float = 1F
):Bitmap?{
    val bitmap = copy(config,true)
    val paint = Paint()

    val matrix = ColorMatrix().apply {
        setScale(red,green,blue,alpha)
    }

    val filter  = ColorMatrixColorFilter(matrix)
    paint.colorFilter = filter

    Canvas(bitmap).drawBitmap(this,0f,0f,paint)
    return bitmap
}
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"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC"
    android:padding="24dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="210dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Original Bitmap"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="210dp"
        android:layout_marginTop="16dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:padding="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView2">

        <TextView
            android:id="@+id/tvIntensity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:gravity="center_horizontal"
            android:text="Channels Intensity"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <SeekBar
            android:id="@+id/seekBarRed"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:max="20"
            android:progress="10"
            android:progressTint="#ff0000"
            android:progressBackgroundTint="#ff0000"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tvIntensity" />

        <SeekBar
            android:id="@+id/seekBarGreen"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:max="20"
            android:progress="10"
            android:progressTint="#00ff00"
            android:progressBackgroundTint="#00ff00"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/seekBarRed" />

        <SeekBar
            android:id="@+id/seekBarBlue"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:max="20"
            android:progress="10"
            android:progressTint="#0000ff"
            android:progressBackgroundTint="#0000ff"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/seekBarGreen" />

        <SeekBar
            android:id="@+id/seekBarAlpha"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:max="20"
            android:progress="10"
            android:progressTint="#000000"
            android:progressBackgroundTint="#000000"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/seekBarBlue" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials