Tuesday, June 23, 2020

android kotlin - Bitmap rotate color axis

MainActivity.kt

package com.example.jetpack

import android.content.Context
import android.graphics.*
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("rose.png")


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

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


            // show bitmap color axis rotation
            seekBar.setOnSeekBarChangeListener(object:
                SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    var axis = 0
                    var color = "Red"
                    when(radioGroup.checkedRadioButtonId){
                        R.id.radioRed->{axis = 0; color = "Red"}
                        R.id.radioGreen->{axis = 1; color = "Green"}
                        R.id.radioBlue->{axis = 2; color = "Blue"}
                    }

                    textView2.text = "Rotate Color Axis " +
                            "($color : $progress degrees)"
                    imageView2.setImageBitmap(
                        rotateColorAxis(axis,progress.toFloat())
                    )
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

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


            radioGroup.setOnCheckedChangeListener { radioGroup, i ->
                var axis:Int = 0
                var color = "Red"
                when(i){
                    R.id.radioRed->{axis = 0; color = "Red"}
                    R.id.radioGreen->{axis = 1; color = "Green"}
                    R.id.radioBlue->{axis = 2; color = "Blue"}
                }

                textView2.text = "Rotate Color Axis " +
                        "($color : ${seekBar.progress} degrees)"
                imageView2.setImageBitmap(
                    rotateColorAxis(axis,seekBar.progress.toFloat())
                )
            }
        }
    }
}


// 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 rotate bitmap color axis by color matrix
fun Bitmap.rotateColorAxis(axis:Int,degrees:Float):Bitmap?{
    val bitmap = copy(Bitmap.Config.ARGB_8888,true)
    val paint = Paint()

    val matrix = ColorMatrix().apply {
        setRotate(axis,degrees)
    }

    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: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" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="ColorMatrix Color Axis Rotation"
        android:gravity="center_horizontal"
        app:layout_constraintEnd_toEndOf="@+id/imageView2"
        app:layout_constraintStart_toStartOf="@+id/imageView2"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2">

        <com.google.android.material.radiobutton.MaterialRadioButton
            android:id="@+id/radioRed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Red" />

        <com.google.android.material.radiobutton.MaterialRadioButton
            android:id="@+id/radioGreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Green" />

        <com.google.android.material.radiobutton.MaterialRadioButton
            android:id="@+id/radioBlue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Blue" />
    </RadioGroup>

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:max="360"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

</androidx.constraintlayout.widget.ConstraintLayout>