Sunday, April 15, 2018

android kotlin - Resize a bitmap

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import java.io.IOException
import kotlin.random.Random


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

        // Get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)
        val ivOriginal = findViewById<ImageView>(R.id.ivOriginal)
        val ivResized = findViewById<ImageView>(R.id.ivResized)
        val tvOriginal = findViewById<TextView>(R.id.tvOriginal)
        val tvResized = findViewById<TextView>(R.id.tvResized)


        // Get the bitmap from assets and display into image view
        val bitmap = assetsToBitmap("flower.jpg")

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

            // Display the original bitmap width and height
            tvOriginal.text = "${bitmap.width} * ${bitmap.height}"
        }


        // Click listener for button widget
        button.setOnClickListener{
            if(bitmap!=null){
                // Resize the bitmap and display it into image view
                val resizedBitmap = resizeBitmap(
                    bitmap = bitmap,
                    width = Random.nextInt(300,600),
                    height = Random.nextInt(200,400)
                )
                ivResized.setImageBitmap(resizedBitmap)

                // Display the resized bitmap width and height
                tvResized.text =
                    "${resizedBitmap.width} * ${resizedBitmap.height}"
            }else{
                Log.d("Error","bitmap not found.")
            }
        }
    }


    // 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
        }
    }


    // Method to resize a bitmap programmatically
    private fun resizeBitmap(bitmap:Bitmap,width:Int,height:Int)
    :Bitmap{
        return Bitmap.createScaledBitmap(
            bitmap, width, height,false
        )
    }
}
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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    android:background="#F8F8F8"
    android:padding="12dp">

    <ImageView
        android:id="@+id/ivOriginal"
        android:layout_width="0dp"
        android:layout_height="215dp"
        android:scaleType="centerInside"
        android:background="#E6E6E6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvOriginal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginTop="8dp"
        android:gravity="center_horizontal"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ivOriginal" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:layout_marginTop="16dp"
        android:text="Resize Bitmap"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvOriginal" />

    <ImageView
        android:id="@+id/ivResized"
        android:layout_width="0dp"
        android:layout_height="215dp"
        android:layout_marginTop="16dp"
        android:background="#E6E6E6"
        android:scaleType="centerInside"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <TextView
        android:id="@+id/tvResized"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginTop="8dp"
        android:gravity="center_horizontal"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ivResized" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples