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

时间:2012-07-19 14:43:27

标签: java jpeg image-resizing

我有一些我正在面板中显示的.jpg。不幸的是,它们都是1500x1125像素,这对于我的目标来说太大了。是否有一种编程方式来改变这些.jpg的分辨率?

3 个答案:

答案 0 :(得分:5)

您可以使用Graphics2D方法(来自java.awt)缩放图像。这tutorial at mkyong.com深入解释了这一点。

答案 1 :(得分:2)

将其加载为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() );
}

答案 2 :(得分: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;
}