Thursday, June 25, 2020

android kotlin - Bitmap color balance

MainActivity.kt

package com.example.jetpack

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.io.IOException


class MainActivity : AppCompatActivity() {

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

        // get the bitmap from assets folder
        val bitmap = assetsToBitmap("redFlowers.png")

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

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

            // bitmap color balance on red seek bar change
            seekBarRed.setOnSeekBarChangeListener(object:SeekBar
            .OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    val intensityRed = progress - 100
                    val intensityGreen = seekBarGreen.progress - 100
                    val intensityBlue = seekBarBlue.progress - 100

                    // show color balanced bitmap on image view
                    imageView2.setImageBitmap(
                        colorBalance(
                            intensityRed, intensityGreen, intensityBlue
                        )
                    )
                    tvIntensity.text = "Intensity (Red $intensityRed :" +
                            " Green $intensityGreen : Blue $intensityBlue)"
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

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


            // bitmap color balance on green seek bar change
            seekBarGreen.setOnSeekBarChangeListener(object:SeekBar
            .OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    val intensityRed = seekBarRed.progress - 100
                    val intensityGreen = progress - 100
                    val intensityBlue = seekBarBlue.progress - 100

                    // show color balanced bitmap on image view
                    imageView2.setImageBitmap(
                        colorBalance(
                            intensityRed, intensityGreen, intensityBlue
                        )
                    )
                    tvIntensity.text = "Intensity (Red $intensityRed :" +
                            " Green $intensityGreen : Blue $intensityBlue)"
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

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


            // bitmap color balance on blue seek bar change
            seekBarBlue.setOnSeekBarChangeListener(object:SeekBar
            .OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    val intensityRed = seekBarRed.progress - 100
                    val intensityGreen = seekBarGreen.progress - 100
                    val intensityBlue = progress - 100

                    // show color balanced bitmap on image view
                    imageView2.setImageBitmap(
                        colorBalance(
                            intensityRed, intensityGreen, intensityBlue
                        )
                    )
                    tvIntensity.text = "Intensity (Red $intensityRed :" +
                            " Green $intensityGreen : Blue $intensityBlue)"
                }

                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 for bitmap color balance
fun Bitmap.colorBalance(
    intensityRed:Int = 0,
    intensityGreen:Int = 0,
    intensityBlue:Int = 0
):Bitmap?{
    val bitmap = copy(config,true)
    var alpha:Int
    var red:Int
    var green:Int
    var blue:Int
    var pixel:Int

    // scan through all pixels
    for (x in 0 until width){
        for (y in 0 until height){
            pixel = getPixel(x,y)
            alpha = Color.alpha(pixel)
            red = Color.red(pixel)
            green = Color.green(pixel)
            blue = Color.blue(pixel)

            red += intensityRed
            if (red > 255) red = 255 else if (red < 0) red = 0

            green += intensityGreen
            if (green > 255) green = 255 else if (green < 0) green = 0

            blue += intensityBlue
            if (blue > 255) blue = 255 else if (blue < 0) blue = 0

            val color = Color.argb(
                alpha,
                red,
                green,
                blue
            )
            bitmap.setPixel(x,y,color)
        }
    }

    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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EDEAE0"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

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

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_marginTop="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:srcCompat="@tools:sample/avatars" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        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_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:max="200"
            android:progress="100"
            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_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:max="200"
            android:progress="100"
            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_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:max="200"
            android:progress="100"
            android:progressTint="#0000ff"
            android:progressBackgroundTint="#0000ff"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/seekBarGreen" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>