Detailed analysis of three efficient compression schemes of Java image compression
1. Intelligent size compression based on OpenCV
java public static void extracted2() { ' String path = "C:\"; String savePath = "D:\"; int maxWidth = 800; int maxHeight = 600; compressImage(new File(path), new File(savePath), maxWidth, maxHeight); } compressImage The writing method iskotlinSyntax,Need to convert by yourself fun compressImage(inputFile: File, outputFile: File, maxWidth: Int, maxHeight: Int) { try { val image = (inputFile) val originalWidth = val originalHeight = var newWidth = originalWidth var newHeight = originalHeight // Calculate the new width and height to maintain the proportion if (originalWidth > maxWidth || originalHeight > maxHeight) { val ratio = (() / originalWidth, () / originalHeight) newWidth = (originalWidth * ratio).toInt() newHeight = (originalHeight * ratio).toInt() } val resizedImage = BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB) ().apply { drawImage((newWidth, newHeight, .SCALE_SMOOTH), 0, 0, null) dispose() } // Make sure the output directory exists val outputPath: Path = () if (!(outputPath)) { (outputPath) } (resizedImage, "jpg", outputFile) } catch (e: IOException) { () } }
Technical Highlights:
- Dynamic size adjustment: Automatically maintain the original image scale by setting the maximum width and height (800x600).
- OpenCV support: Use () for high-quality scaling
- Cross-platform support: Need to configure OpenCV local library ()
Applicable scenarios:
- Mobile picture display
- Upload user avatar
2. JPEG quality parameter compression
java public static void extracted4() { for (int i = 1; i <=10; i++) { float quality = 0.1f * i; compressImage(inputFile, outputFile, quality); } } public static void compressImage(File inputFile, File outputFile, float quality) throws IOException { // Read pictures BufferedImage image = (inputFile); // Get the image writer Iterator<ImageWriter> writers = ("webp"); ImageWriter writer = (); // Set the output target of the writer ImageOutputStream ios = (outputFile); (ios); // Create image writer configuration IIOImage imageIO = new IIOImage(image, null, null); ImageWriteParam param = (); // Set compression quality if (()) { (ImageWriteParam.MODE_EXPLICIT); (quality); } // Write to the picture (null, imageIO, param); // Close the resource (); (); }
Key technologies:
- Mass gradient test: Perform 10-level compression test from 0.1 to 1.0
- Lossless compression support: Control compression mode through ImageWriteParam
- Visual quality balance: Find the best balance between file size and clarity
Comparison of compression effects:
Quality parameters | File size | Clarity |
---|---|---|
0.3 | 45KB | Acceptable |
0.7 | 120KB | good |
1.0 | 350KB | No loss |
3. WebP efficient format conversion
public static void extracted6() { String path = "C:\\Users\\Meizhong\\Pictures\\"; for (int i = 1; i <=10; i++) { float quality = 0.0f + i * 0.1f; ("quality:" + quality); String savePath = "D:\\save\\test2-webp-"+quality+".jpg"; File inputFile = new File(path); // Original picture file File outputFile = new File(savePath); try { jpg2webp(inputFile, outputFile,quality); } catch (Exception e) { throw new RuntimeException(e); } } } public static void jpg2webp(File oldfile, File newfile,float quality){ try { // Get the original file encoding BufferedImage image = (oldfile); // Create a WebP ImageWriter instance ImageWriter writer = ("image/webp").next(); // Configure encoding parameters WebPWriteParam writeParam = new WebPWriteParam(()); // Set compression mode (WebPWriteParam.MODE_EXPLICIT); ("getCompressionTypes:"+(())); // "Lossy"-lossy, "Lossless"-no loss (()[0]); (quality); // Configure ImageWriter output (new FileImageOutputStream(newfile)); // Encoding and regenerating new pictures (null, new IIOImage(image, null, null), writeParam); ("Jpg file converted to webp format successfully"); } catch (FileNotFoundException e) { (); } catch (IOException e) { (); } }
Core advantages:
- Improve compression rate: Save 25-35% space than JPEG
- Transparent channel support: Supports Alpha channel transparency effect
- Progressive loading: Supports progressive decoding loading
Performance comparison:
Format | Quality 0.8 | Loading speed | compatibility |
---|---|---|---|
JPEG | 150KB | quick | 100% |
WebP | 95KB | Faster | 95%+ |
4. Solution selection suggestions
- Mobile terminal priority: WebP + Quality Compression (0.6-0.8)
- User upload processing: Size compression + JPEG quality 0.7
- Professional gallery storage: OpenCV dual algorithm verification (histogram comparison + size compression)
Summarize
This is the end of this article about three efficient compression solutions for Java image compression. For more relevant content on efficient compression of Java image, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!
Related Articles
Detailed explanation of Java basic checking and unchecked exception handling
This article introduces the handling of exceptions in Java basics, and mainly explains the detailed explanation of Java checking and unchecked exception handling examples for detailed explanations for friends in need, please refer to it. I hope it can be helpful.2021-10-10SpringBoot uses Maven to implement multi-environment configuration management
There are often development environments, test environments, and production environments in software development, and the configurations of these environments will be different. This article mainly introduces SpringBoot to use Maven to implement multi-environment configuration management. Those who are interested can learn about it.2024-01-01Detailed example of Java shift operator (summary)
This article mainly introduces detailed examples of Java shift operators (summary). The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.2019-12-12Detailed explanation of how SpringBoot integrates RabbitMQ to achieve message confirmation
This article mainly introduces how SpringBoot integrates RabbitMQ to achieve message confirmation. This article introduces you very detailedly and has certain reference value for your study or work. Friends who need it can refer to it.2022-05-05Detailed explanation of two ways to execute groovy scripts in Java
This article mainly introduces two ways of Java executing groovy scripts. This article introduces you very detailedly and has certain reference value for your study or work. Friends who need it can refer to it.2021-04-04Selenium handles the drop-down box of select tags
Selenium is an open source and portable automated software testing tool for testing web applications that have the ability to run on different browsers and operating systems. Next, through this article, I will introduce to you the drop-down box for Selenium processing the select tag. Friends who need it, let’s learn together.2016-04-04Java has a deep understanding of common sorting algorithms in data structures
This article mainly introduces the commonly used sorting algorithms and code implementations of Java. In Java development, you need to master the sorting application proficiently, so as to ensure that you have solid basic abilities when learning Java. So what sorting algorithms do Java have? This article will talk about the common sorting algorithms in Java in detail. Friends who need it can refer to it.2022-01-01JAVA algorithm starting with heap sorting example
This article mainly introduces the heap sorting example of the starting heap of JAVA algorithm. Friends who need it can refer to it2014-02-02Java code statistics on the number of visits to users in different provinces on the website
This article mainly introduces the relevant information on the number of visits of users in different provinces in the Java code statistics website. It is very valuable for reference. Interested friends, let’s learn together.2016-05-05Comparison of Java Kryo, Protostuff, Hessian serialization methods
This article mainly introduces the comparison of Java Kryo, Protostuff, Hessian serialization methods. The article focuses on the topic and has certain reference value. Friends who need it can refer to it.2022-07-07