Reputation: 669
I have some .jpg's that I'm displaying in a panel. Unfortunately they're all about 1500x1125 pixels, which is way too big for what I'm going for. Is there a programmatic way to change the resolution of these .jpg's?
Upvotes: 5
Views: 16774
Reputation: 21443
you can try:
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;
}
Upvotes: 1
Reputation: 2223
Load it as an ImageIcon and this'll do the trick:
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() );
}
Upvotes: 3
Reputation: 18543
You can scale an image using Graphics2D
methods (from java.awt
). This tutorial at mkyong.com explains it in depth.
Upvotes: 5