我正在使用下面给出的代码对jpeg / png / gif图像进行图像压缩。在png图像而不是减小尺寸的情况下,图像的尺寸在增加。例如:我的图片大小为824 KB,压缩后的大小为1.2 MB。图像大小应减小或保持不变,但至少不应增大。我该如何在代码中解决此问题?
File input = new File("digital_image_processing.png");
BufferedImage image = ImageIO.read(input);
File compressedImageFile = new File("compress.png");
OutputStream os = new FileOutputStream(compressedImageFile);
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("png");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
if (imageWriteParam.canWriteCompressed()) {
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
if (imageWriteParam.getCompressionType() == null) {
imageWriteParam.setCompressionType(imageWriteParam.getCompressionTypes()[0]);
}
imageWriteParam.setCompressionQuality(imageQuality);
}
writer.write(null, new IIOImage(image, null, null), imageWriteParam);