如何使用Java压缩PNG图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2721303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to compress a PNG image using Java
提问by ubernoob
Hi I would like to know if there is any way in Java to reduce the size of an image (use any kind of compression) that was loaded as a BufferedImage and is going to be saved as an PNG.
嗨,我想知道 Java 中是否有任何方法可以减小作为 BufferedImage 加载并要保存为 PNG 的图像的大小(使用任何类型的压缩)。
Maybe some sort of png imagewriteparam? I didnt find anything helpful so im stuck.
也许某种png imagewriteparam?我没有发现任何有用的东西,所以我被卡住了。
heres a sample how the image is loaded and saved
这是如何加载和保存图像的示例
public static BufferedImage load(String imageUrl) {
Image image = new ImageIcon(imageUrl).getImage();
bufferedImage = new BufferedImage(image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = bufferedImage.createGraphics();
g2D.drawImage(image, 0, 0, null);
return bufferedImage;
}
public static void storeImageAsPng(BufferedImage image, String imageUrl) throws IOException {
ImageIO.write(image, "png", new File(imageUrl));
}
回答by aioobe
Have a look at the ImageWriterParam
class in the same package as the ImageIO
class. It mentions compression.
查看与ImageWriterParam
类位于同一包中的ImageIO
类。它提到了压缩。
https://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageWriteParam.html
https://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageWriteParam.html
Also look at the example at http://exampledepot.com/egs/javax.imageio/JpegWrite.htmland see if it translates well for PNG files.
另请查看http://exampledepot.com/egs/javax.imageio/JpegWrite.html上的示例,看看它是否可以很好地转换为 PNG 文件。
回答by leonbloy
If it's going to be saved as PNG
, compression will be done at that stage. PNG has a lossless compression algorithm (basically prediction followed by lempel-ziv compression) with few adjustable parameters (types of "filters") and not much impact in compression amount - in general the default will be optimal.
如果it's going to be saved as PNG
,将在该阶段进行压缩。PNG 具有无损压缩算法(基本上是预测,然后是 lempel-ziv 压缩),可调整参数很少(“过滤器”类型)并且对压缩量没有太大影响 - 通常默认值将是最佳的。
回答by depsypher
回答by Kristof Neirynck
By calling ImageIO.write
you use the default compression quality for pngs. You can choose an explicit compression mode which reduces file size a lot.
通过调用,ImageIO.write
您可以使用 png 的默认压缩质量。您可以选择显式压缩模式,从而大大减少文件大小。
String inputPath = "a.png";
String outputPath = "b.png";
BufferedImage image;
IIOMetadata metadata;
try (ImageInputStream in = ImageIO.createImageInputStream(Files.newInputStream(Paths.get(inputPath)))) {
ImageReader reader = ImageIO.getImageReadersByFormatName("png").next();
reader.setInput(in, true, false);
image = reader.read(0);
metadata = reader.getImageMetadata(0);
reader.dispose();
}
try (ImageOutputStream out = ImageIO.createImageOutputStream(Files.newOutputStream(Paths.get(outputPath)))) {
ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(image);
ImageWriter writer = ImageIO.getImageWriters(type, "png").next();
ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.0f);
}
writer.setOutput(out);
writer.write(null, new IIOImage(image, null, metadata), param);
writer.dispose();
}