[英]Fit Java Image into JFrame
I'm trying to display an Image that is bigger than the JFrame Dimensions. 我正在尝试显示大于JFrame尺寸的图像。 If I try to resize the Image smaller, then the Image quality is lost.
如果我尝试缩小图像的尺寸,则会失去图像质量。
If I make the Image larger, the quality is not lost. 如果我放大图像,则画质不会丢失。 Is this normal behavior in Java image package?
这是Java映像包中的正常行为吗?
I guess what I'm trying to figure out is that probably I'm doing something wrong when reducing the Image size. 我想我要弄清楚的是缩小图像大小时可能做错了什么。 So does Java provide a method to automatically do this without loosing image quality?
那么Java是否提供了一种在不降低图像质量的情况下自动执行此操作的方法?
Same behavior like JButtons, where java automatically adjusts the space occupied by a JButton in a JPanel. 行为与JButton相同,在Java中Java自动调整JPanel中JButton占用的空间。
bufferedImage = resize(bufImage,500,600);
ImageIcon imageIcon = new ImageIcon(bufferedImage);
resizedIMage = imageIcon.getImage();
The actual resize is below. 实际的调整大小如下。 I took it from the internet.
我从互联网上拿走的。
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;
}
If you are using Java 5 or newer you can try RenderingHints.VALUE_INTERPOLATION_BICUBIC for your interpolation hint. 如果您使用的是Java 5或更高版本,则可以尝试使用RenderingHints.VALUE_INTERPOLATION_BICUBIC作为插值提示。
There is a very detailed description of the different scaling behaviours of Java2D in this article: perils-of-getScaledInstance 本文对Java2D的不同缩放行为进行了非常详细的描述: perils-of-getScaledInstance
It contains examples of the different downscale results you can expect to see with the different approaches available in Java. 它包含使用Java中可用的不同方法可以看到的不同缩减结果的示例。
It also provides sample code that uses a multi-step aproach to downscale the image which appears to produce much better results than VALUE_INTERPOLATION_BILINEAR. 它还提供了示例代码,该示例代码使用多步方法缩小了图像的比例,看起来比VALUE_INTERPOLATION_BILINEAR产生更好的结果。
Override the paintComponent(Graphics g)
method of whatever component will display the image and look at Graphics. 重写任何将显示图像并查看Graphics的组件的
paintComponent(Graphics g)
方法。 drawImage () drawImage ()
You can even cast your Graphics
instance into a Graphics2D for more functionalities. 您甚至可以将
Graphics
实例转换为Graphics2D以获得更多功能。 For example setRenderingHings () 例如setRenderingHings ()
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints ();
rh.put (RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints (rh);
I would get rid of this: 我会摆脱这个:
int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType();
And always use BufferedImage.TYPE_INT_ARGB
并始终使用
BufferedImage.TYPE_INT_ARGB
That seems to give me good results. 那似乎给了我很好的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.