gitmyhub

Compressor

Kotlin ★ 7.2k updated 3mo ago

An android image compression library.

A Kotlin Android library that shrinks photo file sizes before uploading or storing them, with a one-liner API and customizable quality, resolution, output format, and maximum file-size constraints.

KotlinAndroidGradleCoroutinessetup: easycomplexity 2/5

Compressor is an Android library that shrinks the file size of photos on a user's device. When someone takes a picture with their phone, the resulting image file can be several megabytes large. Compressor lets developers reduce that size before uploading, storing, or sharing the image, with the aim of keeping the visual quality close to the original.

The library is written in Kotlin and designed to be added to an Android project with a single line in the build configuration. Basic use is a one-liner: pass the context and the image file, and get back a smaller compressed file. The library uses sensible defaults, so most projects can get started without configuration.

For cases where the defaults are not enough, developers can customize the compression settings by specifying a target resolution, a quality level from 0 to 100, an output format such as JPEG or WebP, and a maximum file size in bytes. These constraints can be combined in any combination, and developers can also write their own custom constraints if they need behavior the library does not provide out of the box. One example shown in the documentation is a constraint that renames the output file to lowercase, which can be combined with the standard quality and format settings.

The library uses Kotlin coroutines, which is the standard approach in modern Android development for running work off the main thread. Image compression can take noticeable time on large files, so doing it in a coroutine keeps the app responsive while the work runs in the background. The library also supports running compression on the main thread when needed, though the README implies this is less common.

The project is licensed under the Apache 2.0 license, which permits use in both open-source and commercial applications.

Where it fits