android kotlin - Convert JPG to PNG to WebP programmatically

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.graphics.*
import android.os.Build
import android.os.Bundle
import android.widget.*
import java.io.ByteArrayOutputStream
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 textView = findViewById<TextView>(R.id.textView)
        val textView2 = findViewById<TextView>(R.id.textView2)
        val button = findViewById<Button>(R.id.button)


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


        // If bitmap is not null
        bitmap?.let {
            imageView.setImageBitmap(bitmap)
        }


        // Click listener for button widget
        button.setOnClickListener{
            // disable the button itself
            it.isEnabled = false

            if(bitmap!=null){
                // Compress bitmap and convert image
                // format from one to another
                val compressedBitmap = bitmap.compress(
                    Bitmap.CompressFormat.PNG)

                /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    val compressedBitmap = bitmap.compress(
                        Bitmap.CompressFormat.WEBP_LOSSY
                    )
                    val compressedBitmap = bitmap.compress(
                        Bitmap.CompressFormat.WEBP_LOSSLESS
                    )
                    imageView2.setImageBitmap(compressedBitmap)
                }*/


                /*val compressedBitmap = bitmap.compress(
                    Bitmap.CompressFormat.JPEG)
                val compressedBitmap = bitmap.compress(
                    Bitmap.CompressFormat.JPEG, 10)
                val compressedBitmap = bitmap.compress(
                    quality = 10) // Compress only*/


                // Display the compressed bitmap into image view
                imageView2.setImageBitmap(compressedBitmap)

                // Notify user
                textView2.text = "Bitmap format changed to PNG."
            }else{
                // Log error
            }
        }
    }



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



// Extension function to compress and change
// bitmap image format programmatically
fun Bitmap.compress(
    format:Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG,
    quality:Int = 100
):Bitmap{
    // Initialize a new ByteArrayStream
    val stream = ByteArrayOutputStream()


    // Compress the bitmap with specified format and quality
    this.compress(
        format,
        quality,
        stream
    )

    val byteArray = stream.toByteArray()

    // Finally, return the compressed bitmap
    return BitmapFactory.decodeByteArray(
        byteArray, 0, byteArray.size
    )
}
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:text="Original Bitmap"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Convert JPEG To PNG"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView" />

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Converted Bitmap"
        app:layout_constraintEnd_toEndOf="@+id/imageView2"
        app:layout_constraintStart_toStartOf="@+id/imageView2"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />

</androidx.constraintlayout.widget.ConstraintLayout>