Working with Images in Kotlin: Libraries and Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Images in Kotlin: Library Overview and Practical Examples

Kotlin is a modern multiplatform language actively used for development on JVM (Desktop), Android, as well as in backend and multiplatform projects (KMP). Working with images is a common task, and the Kotlin ecosystem offers several approaches: from using standard Java libraries to specialized solutions for Android and Kotlin Multiplatform. In this article, we will review the main libraries and tools, show code examples, and provide practical recommendations.



1. Built-in Tools: AWT (Java Desktop / JVM)

For desktop applications on JVM (e.g., JavaFX or Swing), the most convenient approach is to use the standard java.awt and javax.imageio packages. These libraries are available out of the box and allow you to load, resize, convert formats, and draw on images.

Example: Loading, Resizing, and Saving

import java.awt.image.BufferedImageimport java.io.Fileimport javax.imageio.ImageIO

fun resizeImage(inputPath: String, outputPath: String, newWidth: Int, newHeight: Int) { val original: BufferedImage = ImageIO.read(File(inputPath)) val resized = BufferedImage(newWidth, newHeight, original.type) val g = resized.createGraphics() g.drawImage(original, 0, 0, newWidth, newHeight, null) g.dispose() ImageIO.write(resized, "jpg", File(outputPath)) println("Image saved: $outputPath")}

fun main() { resizeImage("input.jpg", "output.jpg", 800, 600)}

For rotation, watermarking, or cropping, you can use Graphics2D methods and affine transformations.



2. Android: Bitmap and BitmapFactory

In Android development with Kotlin, the foundation for working with images is the android.graphics.Bitmap class and BitmapFactory utilities. They allow you to load images from resources, files, or the network, as well as perform basic processing.

Example: Loading from Resources and Resizing (Android)

import android.graphics.Bitmapimport android.graphics.BitmapFactoryimport android.graphics.Matriximport java.io.ByteArrayOutputStream

fun scaleBitmap(bitmap: Bitmap, maxSize: Int): Bitmap { val width = bitmap.width val height = bitmap.height val scale = if (width > height) maxSize.toFloat() / width else maxSize.toFloat() / height val matrix = Matrix() matrix.postScale(scale, scale) return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true)}

fun bitmapToByteArray(bitmap: Bitmap, quality: Int = 90): ByteArray { val stream = ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream) return stream.toByteArray()}

For more complex processing (filters, effects), you can use android.renderscript (deprecated in API 31) or the OpenCV Android library.



3. Kotlin Multiplatform (KMP): KVision Library or Custom Solutions

In multiplatform projects, there is no single standard for working with images. Commonly used options include:

  • KVision — a framework for Kotlin/JS web applications, with built-in support for working with Canvas and ImageData.
  • Okio + ImageIO (JVM) / Bitmap (Android) — for KMP, expect/actual wrappers are often written, where each platform uses its own implementation.
  • Skiko — a library from JetBrains for working with graphics (Skia), available on JVM, JS, and Native. It allows loading, drawing, and exporting images.

Example expect/actual for KMP:

// commonMainexpect fun loadImageBytes(path: String): ByteArray

// androidMainactual fun load

Recommendations