Thursday, June 25, 2020

android kotlin - Bitmap remove transparency

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 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("whiteFlowers.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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EFDFBB"
    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="Transparent 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/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">

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

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

            <com.google.android.material.radiobutton.MaterialRadioButton
                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>