我可以在Java中更改jpg图像的分辨率吗?

我有一些.jpg,我正在面板中显示。 不幸的是,它们都是1500×1125像素,这对我想要的东西来说太大了。 有没有一种程序化的方法来改变这些.jpg的分辨率?

您可以使用Graphics2D方法(来自java.awt )缩放图像。 mkyong.com上的这个教程深入地解释了它。

加载它作为ImageIcon,这将做的伎俩:

 import java.awt.Graphics2D; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; public static ImageIcon resizeImageIcon( ImageIcon imageIcon , Integer width , Integer height ) { BufferedImage bufferedImage = new BufferedImage( width , height , BufferedImage.TRANSLUCENT ); Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.drawImage( imageIcon.getImage() , 0 , 0 , width , height , null ); graphics2D.dispose(); return new ImageIcon( bufferedImage , imageIcon.getDescription() ); } 

你可以试试:

 private BufferedImage getScaledImage(Image srcImg, int w, int h) { BufferedImage resizedImg = new BufferedImage(w, h, Transparency.TRANSLUCENT); Graphics2D g2 = resizedImg.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(srcImg, 0, 0, w, h, null); g2.dispose(); return resizedImg; }