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

I have a .jpg that I am displaying in a panel. Unfortunately, they all have a size of 1500x1125 pixels, which is too large for what I'm going to do. Is there a software way to change the resolution of these .jpg?

+4
source share
3 answers

You can scale the image using Graphics2D methods (from java.awt ). This tutorial on mkyong.com explains this in detail.

+5
source

Download it as ImageIcon and this will 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() ); } 
+2
source

you may 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; } 
+1
source

All Articles