嗨,我想知道Java中是否有任何方法可以减少作为BufferedImage加载的图像大小(使用任何类型的压缩),并将保存为PNG.
也许是某种png imagewriteparam?我没有找到任何有用的东西,所以我卡住了.
下面是一个示例如何加载和保存图像
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));
}
Run Code Online (Sandbox Code Playgroud)