diff --git a/image-compressing/README.md b/image-compressing/README.md new file mode 100644 index 0000000000..b2556062b8 --- /dev/null +++ b/image-compressing/README.md @@ -0,0 +1 @@ +This module contains tutorials related to the image compression in Java. \ No newline at end of file diff --git a/image-compressing/pom.xml b/image-compressing/pom.xml new file mode 100644 index 0000000000..99cdd65647 --- /dev/null +++ b/image-compressing/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + image-compressing + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + + + net.coobird + thumbnailator + ${thumbnailator.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + + + + + 0.4.19 + + + \ No newline at end of file diff --git a/image-compressing/src/main/java/com/baeldung/image/compression/ImageCompressor.java b/image-compressing/src/main/java/com/baeldung/image/compression/ImageCompressor.java new file mode 100644 index 0000000000..13b1b9560c --- /dev/null +++ b/image-compressing/src/main/java/com/baeldung/image/compression/ImageCompressor.java @@ -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 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(); + } +} \ No newline at end of file diff --git a/image-compressing/src/main/java/com/baeldung/image/compression/ThumbnailsCompressor.java b/image-compressing/src/main/java/com/baeldung/image/compression/ThumbnailsCompressor.java new file mode 100644 index 0000000000..63e81b5d70 --- /dev/null +++ b/image-compressing/src/main/java/com/baeldung/image/compression/ThumbnailsCompressor.java @@ -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); + } +} \ No newline at end of file diff --git a/image-compressing/src/test/java/com/baeldung/image/compression/ImageCompressorUnitTest.java b/image-compressing/src/test/java/com/baeldung/image/compression/ImageCompressorUnitTest.java new file mode 100644 index 0000000000..374de8a83a --- /dev/null +++ b/image-compressing/src/test/java/com/baeldung/image/compression/ImageCompressorUnitTest.java @@ -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"); + } +} \ No newline at end of file diff --git a/image-compressing/src/test/java/com/baeldung/image/compression/ThumbnailsCompressorUnitTest.java b/image-compressing/src/test/java/com/baeldung/image/compression/ThumbnailsCompressorUnitTest.java new file mode 100644 index 0000000000..cb89b56cc7 --- /dev/null +++ b/image-compressing/src/test/java/com/baeldung/image/compression/ThumbnailsCompressorUnitTest.java @@ -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"); + } +} \ No newline at end of file diff --git a/image-compressing/src/test/resources/input.jpg b/image-compressing/src/test/resources/input.jpg new file mode 100644 index 0000000000..985b089840 Binary files /dev/null and b/image-compressing/src/test/resources/input.jpg differ diff --git a/pom.xml b/pom.xml index 79b2071507..f0d70303c9 100644 --- a/pom.xml +++ b/pom.xml @@ -811,6 +811,7 @@ lightrun tablesaw + image-compressing geotools jws @@ -1073,6 +1074,7 @@ lightrun tablesaw + image-compressing geotools jws