Mobile Development 27 min read

Understanding Android Image Compression: Fundamentals, Algorithms, and Luban Implementation

This article explains Android image compression fundamentals—including ARGB, bitmap memory, bit‑depth vs. colour‑depth, quality and sampling compression methods—then details the Luban algorithm, its shortcomings, proposed improvements, and the underlying Skia/Huffman/JPEG native implementation with code examples.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Understanding Android Image Compression: Fundamentals, Algorithms, and Luban Implementation

The article begins with a brief overview of image compression concepts such as ARGB colour model, bitmap representation, and the difference between bit depth and colour depth, illustrating how these affect memory usage and file size on Android devices.

It then describes Android's two main compression techniques: quality compression (adjusting JPEG/WEBP quality) and size (sampling) compression, providing Java code snippets for both approaches:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile("xxx.png", options);

The article introduces the Luban library, explains its background, and walks through its algorithmic steps—ratio classification, boundary checks, size calculations, and iterative compression—highlighting the empirical values derived from reverse‑engineering WeChat's compression strategy.

It also analyses the original Luban implementation, pointing out issues such as lack of memory pre‑checks, fixed quality value, missing format options, and inefficient stream handling, then proposes a modernised solution using coroutines, LiveData, and configurable parameters.

Further, the piece delves into the Android graphics stack: how Bitmap operations flow from Java to native Skia, the role of Huffman coding in libjpeg, and the impact of the optimize_coding flag on compression efficiency. It notes that Android 7.0+ enables optimized Huffman tables, reducing file size by roughly half.

Finally, a native C++ example demonstrates manual JPEG encoding with libjpeg‑turbo, showing how to extract RGB data from an Android Bitmap, configure the compressor (including optimize_coding = TRUE ), and write the compressed image to disk.

Androidimage compressionBitmapSkiaJPEGLubanHuffman
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.