我可以提出的建议是首先将图像调整为单独的BufferedImage
. 原因是,为了产生更好质量的缩放图像,可以获得一个Graphics2D
对象。BufferedImage
Graphics2D
可以接受“渲染提示”,它指示对象应执行图像处理的方式Graphics2D
。该setRenderingHint
方法是可用于设置那些渲染提示的方法之一。RenderingHints
可以使用类中的渲染提示。
然后,使用该Graphics2D
对象,可以BufferedImage
使用之前指定的渲染提示将图像绘制到该对象。
粗略(未经测试)的代码将按以下方式工作:
BufferedImage scaledImage = new BufferedImage(
scaledWidth,
scaledHeight,
BufferedImage.TYPE_INT_RGB
);
Graphics2D g = scaledImage.createGraphics();
g.setRenderingHints(
RenderingHints.Key.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC
);
g.drawImage(panelImage, 0, 0, scaledWidth, scaledHeight, null);
g.dispose();
其他感兴趣的渲染提示可能包括:
The Java Tutorials 的控制渲染质量部分也有更多关于如何控制Graphics2D
对象渲染质量的信息。
对于一般处理图形界面的非常好的信息来源,强烈推荐 Chet Haase 和 Romain Guy 的Filthy Rich Clients 。书中有一节涉及缩放图像的问题,这似乎很相关。