5

我在面板中显示了一些 .jpg。不幸的是,它们都是大约 1500x1125 像素,这对于我想要的东西来说太大了。是否有编程方式来更改这些 .jpg 的分辨率?

4

3 回答 3

5

Graphics2D您可以使用方法(从)缩放图像java.awtmkyong.com 上的本教程对其进行了深入解释。

于 2012-07-19T15:00:15.990 回答
3

将其加载为 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() );
}
于 2012-07-19T14:49:51.180 回答
1

你可以试试:

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;
}                         
于 2012-07-19T14:50:02.973 回答