2012-07-19 74 views
4

我有一些.jpg显示在面板中。不幸的是,它们都是大约1500x1125像素,这对我所要做的事来说太大了。是否有程序化的方式来改变这些.jpg的分辨率?我可以更改Java中jpg图像的分辨率吗?

+4

看看这个http://www.mkyong.com/java/how-to-resize-an-image-in-java/ – toniedzwiedz 2012-07-19 14:46:15

+0

@汤姆,完美的工作,非常感谢你!让它成为答案,你已经得到了一个复选标记。 – Quintis555 2012-07-19 14:56:08

回答

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()); 
} 
+0

哦,我要去检查汤姆作为答案,但我已经使用ImageIcon,所以这可能是更多我需要的。等待未来的更新! – Quintis555 2012-07-19 15:08:53

+0

我得到一个“错误:无法找到符号”在这一行 BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TRANSLUCENT); 它指向构建输出窗口中的BufferedImage部分。 – Quintis555 2012-07-19 15:15:49

+0

我不确定,它编译得很好。你在用什么IDE? – 2012-07-19 15:21:12

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; 
}