android kotlin - Bitmap remove transparency

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.content.Context
import android.graphics.*
import android.os.Bundle
import android.renderscript.*
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 textView2 = findViewById<TextView>(R.id.textView2)
        val radioGroup = findViewById<RadioGroup>(R.id.radioGroup)


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

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

            // show bitmap without transparent pixels on second image view
            imageView2.setImageBitmap(removeTransparency())

            // remove bitmap transparency by specified solid color
            radioGroup.setOnCheckedChangeListener { radioGroup, i ->
                var color = Color.WHITE
                var name = "White"
                when(i){
                    R.id.radioWhite->{color = Color.WHITE; name = "White"}
                    R.id.radioGray->{color = Color.GRAY; name = "Gray"}
                    R.id.radioYellow->{color = Color.YELLOW; name = "Yellow"}
                }

                textView2.text = "Replace By $name"
                imageView2.setImageBitmap(removeTransparency(color))
            }
        }
    }
}


// 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 remove bitmap transparency
fun Bitmap.removeTransparency(backgroundColor:Int = Color.WHITE):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)

            if (alpha == 0){
                // if pixel is full transparent then
                // replace it by solid background color
                bitmap.setPixel(x,y,backgroundColor)
            }else{
                // if pixel is partially transparent then
                // set pixel full opaque
                val color = Color.argb(
                    255,
                    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: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="225dp"
        android:layout_marginTop="8dp"
        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:padding="8dp"
        android:text="Transparent 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="225dp"
        android:layout_marginTop="24dp"
        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="16dp"
        android:padding="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView2">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Replace By White"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

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

            <RadioButton
                android:id="@+id/radioWhite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="White" />

            <RadioButton
                android:id="@+id/radioGray"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Gray" />

            <RadioButton
                android:id="@+id/radioYellow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Yellow" />

        </RadioGroup>

    </androidx.constraintlayout.widget.ConstraintLayout>

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