How to Shrink a 1 MB Image to 100 KB with Java and OpenCV
Learn step‑by‑step how to compress a 1 MB image down to under 100 KB using Java’s ImageIO API and OpenCV, covering the theory of image compression, code snippets for reading, configuring compression parameters, and performance comparisons between native JDK and OpenCV solutions.
Introduction
After a two‑day, two‑night effort the author managed to reduce a 1 MB picture to 100 KB, sharing the story and the technical details behind the optimization.
What Is Image Compression?
Image compression reduces redundant information in digital pictures, using either lossy or lossless techniques to store and transmit data more efficiently.
Java Digital Image Processing
Digital Image Processing (DIP) manipulates image signals to remove noise, enhance, restore, segment, or extract features. In Java, the BufferedImage class represents an image with an accessible data buffer.
Practical Image Compression with ImageIO
The following steps use the JDK’s ImageIO API:
File input = new File("ceshi.jpg");
BufferedImage image = ImageIO.read(input);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = writers.next();
File compressedImageFile = new File("bbcompress.jpg");
OutputStream os = new FileOutputStream(compressedImageFile);
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.01f);
writer.write(null, new IIOImage(image, null, null), param);
os.close();
ios.close();
writer.dispose();A full demo class is shown below:
public class Demo {
public static void main(String[] args) {
try {
File input = new File("ceshi.jpg");
BufferedImage image = ImageIO.read(input);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = writers.next();
File compressedImageFile = new File("bbcompress.jpg");
OutputStream os = new FileOutputStream(compressedImageFile);
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.01f);
writer.write(null, new IIOImage(image, null, null), param);
os.close();
ios.close();
writer.dispose();
} catch (IOException e) {
e.printStackTrace();
}
}
}With a quality factor of 0.01f the image size drops to about 19 KB, though visual quality degrades; increasing the factor to 0.5f yields a 64 KB image with acceptable quality.
Using OpenCV for Image Compression
Several open‑source libraries can be integrated, such as ImageJ, Apache Commons Imaging, ImageMagick, and OpenCV. The example below shows how to compress an image with OpenCV.
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>4.5.1-2</version>
</dependency> OpenCV.loadShared(); Mat src = Imgcodecs.imread(imagePath); MatOfInt dstImage = new MatOfInt(Imgcodecs.IMWRITE_JPEG_QUALITY, 1);
Imgcodecs.imwrite("resized_image.jpg", src, dstImage);The resulting image is also about 19 KB.
Performance Comparison
On the author’s iMac, the native JDK compression took 322 ms, while the OpenCV version took 1 070 ms. Both produced similarly sized files (≈19 KB) at the lowest quality setting.
opencvCompress finished, time: 1070
jdkCompress finished, time: 322Conclusion
Compressing a 1 MB image to 100 KB is non‑trivial; careful selection of compression parameters and libraries is required. The Java ImageIO API offers a quick solution, while OpenCV provides a more flexible but slower alternative.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
