Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the resolution of a jpg image in Java?

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?

like image 231
Quintis555 Avatar asked Jul 19 '12 14:07

Quintis555


2 Answers

You can scale an image using Graphics2D methods (from java.awt). This tutorial at mkyong.com explains it in depth.

like image 79
toniedzwiedz Avatar answered Oct 05 '22 23:10

toniedzwiedz


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() );
}
like image 27
Jonathan Payne Avatar answered Oct 06 '22 00:10

Jonathan Payne