Add blur effect to Image in Jetpack Compose
Tags: android, jetpackcompose, kotlin, tutorial
Published: 2023-01-09 01:43:49

Hi guys, today I want to share this little article where I briefly explain how to create a blurry image on Android. Without so much introduction, let’s get to the point. 😎

Blur is a visual effect that is often used in graphics software to reduce the clarity or sharpness of an image or video. It can be used for a variety of purposes, such as to reduce distractions in the background of a photo or to create a more visually pleasing composition.


![Photo by gebhartyler on Unsplash](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syfb4rvd9o3za4xmvpgu.jpeg)

Photo by [Unsplash](https://unsplash.com/es/fotos/AQpOwYbm_Jg)



As developers, at some point we may have a need to create blurry images, so today I want to share with you a solution that is backward compatible with Android API 31.
<br /><br />

When we work with Jetpack Compose, we can apply a blur effect in a very simple way using the blur extension function of the Modifier interface.


![Example of how to create an image in Jetpack Compose](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/redngq5uyew8qoqn9kaq.png)


The problem with the example above is that if it runs on versions below Android 12, we will not see any effect because this implementation is only allowed in Android 12 (API level 31) or higher versions.

So we will have to verify if we are running the application on versions equal to or greater than Android 12.



![Image that shows how the Android version is verified](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybgc8c97n9q79917yz1n.png)


The first thing we will do is create our code for versions prior to Android 12. For this, we will use RenderScript and ScriptIntrinsicBlur. We will create a composable function called “LegacyBlurImage” that will give us an Image product of a previously processed bitmap.

```kotlin
@Composable
private fun LegacyBlurImage(
  bitmap: Bitmap,
  blurRadio: Float,
  modifier: Modifier = Modifier.fillMaxSize()
) {
  
  val renderScript = RenderScript.create(LocalContext.current)
  val bitmapAlloc = Allocation.createFromBitmap(renderScript, bitmap)
  ScriptIntrinsicBlur.create(renderScript, bitmapAlloc.element).apply {
      setRadius(blurRadio)
      setInput(bitmapAlloc)
      forEach(bitmapAlloc)
  }
  bitmapAlloc.copyTo(bitmap)
  renderScript.destroy()

  BlurImage(bitmap, modifier)
}
```


We will also create a composable function that we will call “BlurImage” with which we will create the composable for the image we want to display.



```kotlin
@Composable
fun BlurImage(
    bitmap: Bitmap,
    modifier: Modifier = Modifier.fillMaxSize(),
) {
    Image(
      bitmap = bitmap.asImageBitmap(),
      contentDescription = null,
      contentScale = ContentScale.Crop,
      modifier = modifier
    )
}
```

Finally we call our functions checking the version of Android that the device has.

```kotlin
val bitmap = BitmapFactory
  .decodeResource(LocalContext.current.resources, R.drawable.custom_image)

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
    LegacyBlurImage(bitmap, 25f)
} else {
    BlurImage(
        bitmap,
        Modifier
            .fillMaxSize()
            .blur(radiusX = 15.dp, radiusY = 15.dp)
    )
}
```


<br />Result


![Final image result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f68uhg0x1edut1j89nd2.png)


You can see the complete code [here](https://gist.github.com/devjorgecastro/f2289544d40cbf70fcab1df05d2e0b06).
<br />

If you like my content and want to support my work, you can give me a cup of coffee ☕️ 🥰

[![Ko-fi](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j18vjijbegi1dhp83ozv.png)](https://ko-fi.com/devjorgecastro)

[![Buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdz714gpdbxv6ve001hf.png)](https://www.buymeacoffee.com/jorgecastro)


## Follow me in

* **Twitter:** [@devjcastro](https://twitter.com/devjcastro)

* **Linkedin:** [dev-jorgecastro](https://www.linkedin.com/in/dev-jorgecastro/)
<br/>