Java 2D - Resizing BufferedImage
I need to resize a BufferedImage that is of very high quality. The picture is as much as 8Mb, and that’s why I want to downsize it before saving in my DB. Anyway, my routine workd fine on smaller pictures, but when the picture gets this big, it simply stops. Anyone knows a better way of resizing pictures?
My code is:
BufferedImage img = ImageIO.read(picFile);
Image tmpimg = img.getScaledInstance(-1, 70, 0);
JLabel lbl = new JLabel(”");
loadImage(tmpimg, lbl);
int w = tmpimg.getWidth(null);
int h = tmpimg.getHeight(null);
BufferedImage bi = new BufferedImage(w, h, BuffferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(tmpimg, 0,0, w, h, null);
g2d.dispose();
Then the bi should be right size, which it is normally, just that such large pictures stops the whole application. I have set -Xmx512m, and have a pretty good PC (1Gb+ RAM).
And the loadImage function is:
public static void loadImage(Image image, Component comp) throws IOException
{
MediaTracker mt = new MediaTracker(comp);
mt.addImage(image, 0);
try
{
mt.waitForID(0);
} catch (InterruptedException e)
{
throw new RuntimeException(”Unexpected”);
}
if (mt.isErrorID(0))
throw new IOException(”Error during image loading”);
}
—————————-
Re: Resizing BufferedImage, SLOW??
Aug 18, 2006 3:25 PM (reply 1 of 2) (In reply to original post )
For a 1000 x 1000 jpg of size 397KB I get
C:\jexp>java LoadingTest
img type = 5
older way after loadImage = 31
older way after drawImage = 31
older way total time = 63
newer way time = 0
compatible image type = 1
compatible way time = 0
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class LoadingTest
{
public static void main(String[] args) throws IOException
{
File picFile = new File(”images/2004-07-h-full_jpg.jpg”);
BufferedImage img = ImageIO.read(picFile);
System.out.println(”img type = ” + img.getType());
JLabel olderLabel = new JLabel(”");
long start = System.currentTimeMillis();
BufferedImage older = scaleOlderWay(img, olderLabel);
long end = System.currentTimeMillis();
long duration = (end - start)/1000;
System.out.println(”older way total time = ” + duration);
olderLabel.setIcon(new ImageIcon(older));
start = System.currentTimeMillis();
BufferedImage newer = scaleNewerWay(img, false);
end = System.currentTimeMillis();
duration = (end - start)/1000;
System.out.println(”newer way time = ” + duration);
JLabel newerLabel = new JLabel(new ImageIcon(newer));
start = System.currentTimeMillis();
BufferedImage compatible = scaleNewerWay(img, true);
end = System.currentTimeMillis();
duration = (end - start)/1000;
System.out.println(”compatible way time = ” + duration);
JLabel compatLabel = new JLabel(new ImageIcon(compatible));
JPanel panel = new JPanel(new GridLayout(1,0, 5, 0));
panel.setOpaque(true);
panel.add(olderLabel);
panel.add(newerLabel);
panel.add(compatLabel);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(panel);
f.pack();
f.setVisible(true);
}
private static BufferedImage scaleOlderWay(BufferedImage source, Component c)
throws IOException
{
Image tmpimg = source.getScaledInstance(-1, 70, 0);
long start = System.currentTimeMillis();
loadImage(tmpimg, c);
long end = System.currentTimeMillis();
long duration = (end - start)/1000;
System.out.println(”older way after loadImage = ” + duration);
int w = tmpimg.getWidth(c);
int h = tmpimg.getHeight(c);
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
start = System.currentTimeMillis();
g2d.drawImage(tmpimg, 0,0, w, h, null);
end = System.currentTimeMillis();
duration = (end - start)/1000;
System.out.println(”older way after drawImage = ” + duration);
g2d.dispose();
return bi;
}
private static void loadImage(Image image, Component comp) throws IOException
{
MediaTracker mt = new MediaTracker(comp);
mt.addImage(image, 0);
try
{
mt.waitForID(0);
} catch (InterruptedException e)
{
throw new RuntimeException(”Unexpected”);
}
if (mt.isErrorID(0))
throw new IOException(”Error during image loading”);
}
private static BufferedImage scaleNewerWay(BufferedImage source, boolean compat)
{
int w = 70;
int h = 70;
BufferedImage bi = compat ? getCompatibleImage(w, h)
: new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
// assuming width == height for source
double xScale = (double)w / source.getWidth();
double yScale = (double)h / source.getHeight();
AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale);
g2d.drawRenderedImage(source, at);
g2d.dispose();
return bi;
}
private static BufferedImage getCompatibleImage(int w, int h)
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage image = gc.createCompatibleImage(w, h);
System.out.println(”compatible image type = ” + image.getType());
return image;
}
}
Tags: Resizing Buffered Image
- January 17th, 2009
- Java Programming, Programming




June 2nd, 2009 at 12:10 am
Hi, cool post. I have been wondering about this topic,so thanks for writing.
June 12th, 2009 at 5:02 pm
The article is usefull for me. I’ll be coming back to your blog.
June 16th, 2009 at 8:32 am
I have been looking looking around for this kind of information. Will you post some more in future? I’ll be grateful if you will.
July 6th, 2009 at 10:50 am
You know, I don’t read blogs. But yours is really worth beeing read.
July 6th, 2009 at 1:32 pm
I’m glad that after surfing the web for uch a long time I have found out this information. I’m really lucky.
July 17th, 2009 at 7:50 am
Very goood!!!
August 11th, 2009 at 6:41 am
e-mailed on rss
August 24th, 2009 at 3:36 am
Thanks for the contibution!!!
August 31st, 2009 at 2:51 am
Nice blog! Very interesting themes. I will allways read it. Also e-mailed on rss
September 2nd, 2009 at 9:54 pm
COOL…!!!!
September 8th, 2009 at 8:28 am
I bookmarked this link. Thank you for good job