【发布时间】:2011-06-20 07:03:22
【问题描述】:
我正在尝试显示大于 JFrame 尺寸的图像。如果我尝试将图像调整为更小,则图像质量会丢失。
如果我将图像放大,质量不会丢失。这是 Java 图像包中的正常行为吗?
我想我想弄清楚的是我在减小图像大小时可能做错了什么。那么 Java 是否提供了一种在不损失图像质量的情况下自动执行此操作的方法?
类似 JButtons 的行为,java 自动调整 JPanel 中 JButton 占用的空间。
bufferedImage = resize(bufImage,500,600);
ImageIcon imageIcon = new ImageIcon(bufferedImage);
resizedIMage = imageIcon.getImage();
实际调整大小如下。我是从网上拿的。
private static BufferedImage resize(BufferedImage image, int width, int height) {
int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType();
BufferedImage resizedImage = new BufferedImage(width, height, type);
Graphics2D g = resizedImage.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
【问题讨论】:
-
你想如何调整它的大小?如果你不把它缩小,图片怎么能装进一个更小的盒子里?
-
如何在不缩小图像的情况下缩小图像?裁剪它?
-
我看到你已经编辑了你的问题,但你仍然没有告诉我们你是如何缩小你的图像的,所以我不知道除了猜测之外我们如何提供答案。