|
| 1 | +package com.mindee.image; |
| 2 | + |
| 3 | +import java.awt.Graphics2D; |
| 4 | +import java.awt.Image; |
| 5 | +import java.awt.image.BufferedImage; |
| 6 | +import java.io.ByteArrayInputStream; |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.Iterator; |
| 10 | +import javax.imageio.IIOImage; |
| 11 | +import javax.imageio.ImageIO; |
| 12 | +import javax.imageio.ImageWriteParam; |
| 13 | +import javax.imageio.ImageWriter; |
| 14 | + |
| 15 | +/** |
| 16 | + * Image compression class. |
| 17 | + */ |
| 18 | +public final class ImageCompressor { |
| 19 | + public static BufferedImage resize( |
| 20 | + BufferedImage inputImage, Integer newWidth, |
| 21 | + Integer newHeight |
| 22 | + ) { |
| 23 | + Image scaledImage = inputImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH); |
| 24 | + BufferedImage outImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); |
| 25 | + |
| 26 | + Graphics2D g2d = outImage.createGraphics(); |
| 27 | + g2d.drawImage(scaledImage, 0, 0, null); |
| 28 | + g2d.dispose(); |
| 29 | + |
| 30 | + return outImage; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + public static byte[] compressImage( |
| 35 | + byte[] imageData, Integer quality, Integer maxWidth, |
| 36 | + Integer maxHeight |
| 37 | + ) throws IOException { |
| 38 | + |
| 39 | + ByteArrayInputStream bis = new ByteArrayInputStream(imageData); |
| 40 | + BufferedImage original = ImageIO.read(bis); |
| 41 | + ImageUtils.Dimensions dimensions = |
| 42 | + ImageUtils.calculateNewDimensions(original, maxWidth, maxHeight); |
| 43 | + return compressImage(original, quality, dimensions.width, dimensions.height); |
| 44 | + } |
| 45 | + |
| 46 | + public static byte[] compressImage(byte[] imageData, Integer quality, Integer finalWidth) |
| 47 | + throws IOException { |
| 48 | + return compressImage(imageData, quality, finalWidth, null); |
| 49 | + } |
| 50 | + |
| 51 | + public static byte[] compressImage(byte[] imageData, Integer quality) throws IOException { |
| 52 | + return compressImage(imageData, quality, null, null); |
| 53 | + } |
| 54 | + |
| 55 | + public static byte[] compressImage(byte[] imageData) throws IOException { |
| 56 | + return compressImage(imageData, null, null, null); |
| 57 | + } |
| 58 | + |
| 59 | + public static byte[] compressImage( |
| 60 | + BufferedImage original, |
| 61 | + Integer quality, |
| 62 | + Integer finalWidth, |
| 63 | + Integer finalHeight |
| 64 | + ) throws IOException { |
| 65 | + if (quality == null) { |
| 66 | + quality = 85; |
| 67 | + } |
| 68 | + BufferedImage resizedImage = resize(original, finalWidth, finalHeight); |
| 69 | + return encodeToJpegByteArray(resizedImage, (float) quality / 100f); |
| 70 | + } |
| 71 | + |
| 72 | + public static BufferedImage removeAlphaChannel(BufferedImage original) { |
| 73 | + if (original.getType() == BufferedImage.TYPE_INT_RGB) { |
| 74 | + return original; |
| 75 | + } |
| 76 | + BufferedImage newImage = new BufferedImage( |
| 77 | + original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB); |
| 78 | + Graphics2D g = newImage.createGraphics(); |
| 79 | + g.drawImage(original, 0, 0, null); |
| 80 | + g.dispose(); |
| 81 | + return newImage; |
| 82 | + } |
| 83 | + |
| 84 | + public static byte[] encodeToJpegByteArray( |
| 85 | + BufferedImage image, float quality |
| 86 | + ) throws IOException { |
| 87 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 88 | + |
| 89 | + |
| 90 | + Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg"); |
| 91 | + ImageWriter writer = writers.next(); |
| 92 | + |
| 93 | + ImageWriteParam params = writer.getDefaultWriteParam(); |
| 94 | + params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
| 95 | + params.setCompressionQuality(quality); |
| 96 | + |
| 97 | + writer.setOutput(ImageIO.createImageOutputStream(outputStream)); |
| 98 | + BufferedImage alphaCheckedImage = removeAlphaChannel(image); |
| 99 | + writer.write(null, new IIOImage(alphaCheckedImage, null, null), params); |
| 100 | + writer.dispose(); |
| 101 | + |
| 102 | + return outputStream.toByteArray(); |
| 103 | + } |
| 104 | +} |
0 commit comments