Wednesday, June 24, 2020

android kotlin - Bitmap sharpen

MainActivity.kt

package com.example.jetpack

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.renderscript.Allocation
import android.renderscript.Element
import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicConvolve3x3
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)

        val context = this

        // different types of sharpen effect
        val sharp = floatArrayOf(
            -0.60f, -0.60f, -0.60f,
            -0.60f, 5.81f, -0.60f,
            -0.60f, -0.60f, -0.60f
        )

        val sharp2 = floatArrayOf(
            0f, -1f, 0f,
            -1f, 5f, -1f,
            0f, -1f, 0f
        )

        val sharp3 = floatArrayOf(
            -0.15f, -0.15f, -0.15f,
            -0.15f, 2.2f, -0.15f,
            -0.15f, -0.15f, -0.15f
        )

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

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

            // show sharpen bitmap on second image view
            imageView2.setImageBitmap(applySharpen(context,sharp))
            //imageView2.setImageBitmap(applySharpen(context,sharp2))
            //imageView2.setImageBitmap(applySharpen(context,sharp3))
        }
    }
}


// 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 sharpen bitmap
fun Bitmap.applySharpen(
    context:Context,
    radius:FloatArray
):Bitmap?{
    val bitmap = copy(config,true)

    RenderScript.create(context).apply {
        val input = Allocation.createFromBitmap(this,this@applySharpen)
        val output = Allocation.createFromBitmap(this, this@applySharpen)

        ScriptIntrinsicConvolve3x3.create(this, Element.U8_4(this)).apply {
            setInput(input)
            setCoefficients(radius)
            forEach(output)
        }

        output.copyTo(bitmap)
        destroy()
    }

    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="#EEDC82"
    tools:context=".MainActivity">

    <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"
        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="225dp"
        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:gravity="center_horizontal"
        android:text="Sharpen"
        app:layout_constraintEnd_toEndOf="@+id/imageView2"
        app:layout_constraintStart_toStartOf="@+id/imageView2"
        app:layout_constraintTop_toBottomOf="@id/imageView2" />

</androidx.constraintlayout.widget.ConstraintLayout>