我有 JPG 图像文件,我想将其加载到 BufferedImage 中,然后将 BufferedImage 写回 JPG 文件中。这是我目前正在做的事情。
有没有更好的方法来不损失质量并使读/写更快?
读:
BufferedImage image = ImageIO.read(new File(storagePath + fileName + extension));
写:
BufferedImage image = // some jpg image
Iterator iter = ImageIO.getImageWritersByFormatName("JPG");
if (iter.hasNext()) {
ImageWriter writer = (ImageWriter) iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
File outFile = new File(storagePath + fileName + extension);
FileImageOutputStream output = new FileImageOutputStream(outFile);
writer.setOutput(output);
IIOImage iioImage = new IIOImage(image, null, null);
writer.write(null, iioImage, iwp);
}