如何使用Java调整图像大小? [英] How do I resize an image with Java?
本文介绍了如何使用Java调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堆需要16x16版本的48x48图像,而不是存储16x16版本,我想即时调整它们的大小.我当前的代码如下所示(model.icon()返回48x48图片):
I have a bunch of 48x48 images that I need 16x16 versions of, and instead of storing the 16x16 versions, I want to resize them on the fly. My current code looks like this (model.icon() returns the 48x48 image):
Icon icon = model.icon();
Image image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
return new ImageIcon(image.getScaledInstance(16, 16, Image.SCALE_AREA_AVERAGING));
不幸的是,运行此代码时,我得到的是16x16黑色正方形而不是图像.
Unfortunately, when this code is run, I get a 16x16 black square instead of the image.
推荐答案
尝试一下.
ImageIcon icon = model.icon();
Image image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(icon.getImage(), 0, 0, 16, 16, null);
return new ImageIcon(image);
这篇关于如何使用Java调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文