JAVA-6503: Image compression in Java (#14081)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
+19
@@ -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);
|
||||
}
|
||||
}
|
||||
+25
@@ -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");
|
||||
}
|
||||
}
|
||||
+24
@@ -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 |
Reference in New Issue
Block a user