Skip to content

Quickstart — Kotlin (Android)

Add the Android library module (AAR) to your app — see the repository packages/kotlin for the Gradle setup. The bindings are generated by UniFFI from the core, so the API matches the Rust core exactly.

import uniffi.dusimagez.*
val input: ByteArray = File("input.jpg").readBytes()
val output: ByteArray = compressImage(input, CompressOptions(quality = 80u))
File("output.jpg").writeBytes(output)
// 200 KB or less, or a DusimagezException with code TARGET_SIZE_UNREACHABLE.
val output = compressToSize(input, 200_000u, CompressOptions())
val info = getImageInfo(input)
// info.format, info.width, info.height
val report = compressWithReport(input, CompressOptions(preset = Preset.WEB))
println("${report.originalSize} -> ${report.outputSize} bytes, " +
"saved ${report.savedPercent}%, quality ${report.qualityUsed}")
val results = bulkCompress(listOf(data1, data2, ByteArray(0)), CompressOptions())
for (result in results) {
result.onSuccess { println("saved ${it.savedBytes} bytes") }
result.onFailure { e -> println((e as DusimagezException).code) } // per-item error
}

Failures throw DusimagezException with code, messageEn and messageHi — see Error codes for all 9 codes.

Full option list: Options reference.