Image Resizing Techniques on Android: Nearest‑Neighbour, Bilinear, Bicubic, and Lanczos Resampling
The article explains Android image downsampling, comparing built‑in nearest‑neighbour and bilinear methods with higher‑quality bicubic and Lanczos resampling, detailing their implementation, performance trade‑offs, and visual results, and advises choosing the fastest method for simple cases or the more intensive algorithms when superior image fidelity is required.
In the previous article we covered image quality compression and its algorithms. This article focuses on image size reduction and introduces several common size‑compression algorithms used on Android.
1. Android Size‑Compression Logic
Changing an image's dimensions is essentially a resampling process. Upsampling enlarges an image, while downsampling reduces it. The discussion below concentrates on downsampling.
Android provides two built‑in resampling methods:
Nearest Neighbour Resampling
Bilinear Resampling
Other widely used methods include:
Bicubic Resampling
Lanczos Resampling
Additional algorithms such as Hermite Resampling , Bell Resampling , and Mitchell Resampling
We will focus on the first four methods.
2. Nearest Neighbour Resampling
Nearest Neighbour Resampling is a common Android compression technique. Example code:
BitmapFactory.Options options = new BitmapFactory.Options();
// or use inDensity together with inTargetDensity, algorithm same as inSampleSize
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/test.png");
Bitmap compress = BitmapFactory.decodeFile("/sdcard/test.png", options);The resulting image often appears with a single dominant colour because the algorithm simply picks one pixel and discards the others. This is known as the nearest‑point interpolation algorithm.
3. Bilinear Resampling
Bilinear Resampling can be used in Android in two ways:
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/test.png");
Bitmap compress = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth()/2, bitmap.getHeight()/2, true);or by applying a matrix:
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/test.png");
Matrix matrix = new Matrix();
matrix.setScale(0.5f, 0.5f);
bm = Bitmap.createBitmap(bitmap, 0, 0, bit.getWidth(), bit.getHeight(), matrix, true);The createScaledBitmap method internally uses the matrix approach. Bilinear resampling employs a bilinear interpolation algorithm that considers the 2×2 neighbourhood of each source pixel, weighting them according to their relative positions. This yields smoother results than nearest‑neighbour, though it may introduce slight blurring, especially when upsampling.
4. Comparison of Nearest Neighbour and Bilinear
Nearest neighbour is the fastest but can produce noticeable aliasing, especially on text‑heavy images. Bilinear provides a smoother blend of colours and reduces jagged edges.
5. Bicubic (Bicubic/Triple‑Cubic) Resampling
Bicubic resampling uses a bicubic interpolation algorithm that examines a 4×4 neighbourhood of source pixels, offering better detail preservation than bilinear. Android does not provide native support; developers can implement it manually or rely on third‑party libraries such as FFmpeg (see libswscale/swscale.c ).
6. Lanczos Resampling
Lanczos resampling applies a Lanczos filter, often used as a low‑pass filter for up‑ or down‑sampling. It generally yields smoother results with fewer artifacts compared to bicubic, though it is computationally intensive. FFmpeg also implements Lanczos in libswscale/swscale.c . Example FFmpeg command:
ffmpeg -s 600x500 -i input.jpg -s 300x250 -sws_flags lanczos lanczos.jpgThe -sws_flags option can be set to bilinear , bicubic , lanczos , etc., to choose the desired resampling algorithm.
7. Performance on Binary Images
When processing binary (black‑and‑white) images, the four algorithms show distinct quality differences. Downsampling results improve from nearest neighbour → bilinear → bicubic → Lanczos, with Lanczos delivering the highest visual quality. The same order holds for upsampling.
8. Summary
The article introduced four common image resampling algorithms on Android. For most scenarios, developers can choose between nearest neighbour (fastest) and bilinear (balanced speed and quality). When higher image quality is required and performance budget permits, bicubic or Lanczos may be used, keeping in mind their higher computational cost.
Tencent Music Tech Team
Public account of Tencent Music's development team, focusing on technology sharing and communication.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.