JAVA-6503: Image compression in Java (#14081)

This commit is contained in:
Harry9656 2023-06-08 21:58:24 +02:00 committed by GitHub
parent 5bfb63b2a1
commit 4bff7895a8
8 changed files with 143 additions and 0 deletions

View File

@ -0,0 +1 @@
This module contains tutorials related to the image compression in Java.

36
image-compressing/pom.xml Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>image-compressing</artifactId>
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>${thumbnailator.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
</plugins>
</build>
<properties>
<thumbnailator.version>0.4.19</thumbnailator.version>
</properties>
</project>

View File

@ -0,0 +1,36 @@
package com.baeldung.image.compression;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
public class ImageCompressor {
public static void compressImage(String inputPath, String outputPath) throws IOException {
File inputFile = new File(inputPath);
BufferedImage inputImage = ImageIO.read(inputFile);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = writers.next();
File outputFile = new File(outputPath);
ImageOutputStream outputStream = ImageIO.createImageOutputStream(outputFile);
writer.setOutput(outputStream);
ImageWriteParam params = writer.getDefaultWriteParam();
params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
params.setCompressionQuality(0.5f);
writer.write(null, new javax.imageio.IIOImage(inputImage, null, null), params);
outputStream.close();
writer.dispose();
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.image.compression;
import net.coobird.thumbnailator.Thumbnails;
import java.io.File;
import java.io.IOException;
public class ThumbnailsCompressor {
public static void compressImage(String inputPath, String outputPath) throws IOException {
File input = new File(inputPath);
File output = new File(outputPath);
Thumbnails.of(input)
.scale(1)
.outputQuality(0.5)
.toFile(output);
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.image.compression;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ImageCompressorUnitTest {
@Test
void testSecondFileIsSmaller() throws IOException {
String inputImagePath = ImageCompressorUnitTest.class.getClassLoader()
.getResource("input.jpg").getPath();
File inputImage = new File(inputImagePath);
long inputImageSize = inputImage.length();
String outputPath = inputImagePath + "compressed.jpg";
ImageCompressor.compressImage(inputImagePath, outputPath);
File compressedImage = new File(outputPath);
long compressedImageSize = compressedImage.length();
assertTrue(compressedImageSize < inputImageSize, "The compressed image is smaller in size");
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.image.compression;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
class ThumbnailsCompressorUnitTest {
@Test
void testCompressedImageIsSmaller() throws IOException {
String inputImagePath = ThumbnailsCompressor.class.getClassLoader()
.getResource("input.jpg").getPath();
File inputImage = new File(inputImagePath);
long inputImageSize = inputImage.length();
String outputPath = inputImagePath + "compressed.jpg";
ThumbnailsCompressor.compressImage(inputImagePath, outputPath);
File compressedImage = new File(outputPath);
long compressedImageSize = compressedImage.length();
assertTrue(compressedImageSize < inputImageSize, "The compressed image is smaller in size");
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 789 KiB

View File

@ -811,6 +811,7 @@
<module>lightrun</module>
<module>tablesaw</module>
<module>image-compressing</module>
<module>geotools</module>
<module>jws</module>
@ -1073,6 +1074,7 @@
<module>lightrun</module>
<module>tablesaw</module>
<module>image-compressing</module>
<module>geotools</module>
<module>jws</module>