我使用下面的代码来减少图像尺寸。它将大小从1MB减小到250 KB。它很好,但它改变了图像的颜色。
File input = new File("ImageTOCompress.jpg");
BufferedImage image = ImageIO.read(input);
File compressedImageFile = new File("CompressedImage.jpg");
OutputStream os =new FileOutputStream(compressedImageFile);
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
writer.write(null, new IIOImage(image, null, null), param);
os.close();
ios.close();
writer.dispose();
请帮我保持图像颜色......谢谢!!!