Dec
03
|
There are two main ways to scale an image.
The first is to ‘paint’ a scaled version of the image to a new image of the required size.
// Create new (blank) image of required (scaled) size BufferedImage scaledImage = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB); // Paint scaled version of image to new image Graphics2D graphics2D = scaledImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(image, 0, 0, width, height, null); // clean up graphics2D.dispose();
The second is to use an AffineTransform
BufferedImage scaledImage = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = scaledImage.createGraphics(); AffineTransform xform = AffineTransform.getScaleInstance(scale, scale); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); graphics2D.drawImage(image, xform, null); graphics2D.dispose();
There’s also a getScaledInstance() method of the Image class which has been around since the early days of Java. Generally its worth avoiding for performance reasons.
Array ( ) 13 Responses to “How do I scale or resize a BufferedImage?”
Leave a Reply
You must be logged in to post a comment.
November 29th, 2009 at 6:15 am
you didn’t anwer the question. None of these create BufferedImages… they just draw scaled instances of them via grahics.
November 29th, 2009 at 9:46 am
They create a new BufferedImage which is referenced by the variable scaledImage.
June 22nd, 2010 at 11:32 am
TYPE_INT_RGB should be TYPE_INT_ARGB. This adds support for images with transparent layers.
June 22nd, 2010 at 11:35 am
Thanks Trevor, good pickup. Have updated the code accordingly.
June 30th, 2010 at 10:31 pm
My Images get a kind of Red overground. Why it happens?
July 1st, 2010 at 11:09 am
That’s strange, does it happen with all images?
July 6th, 2010 at 4:07 am
because when u save on disk the image,the jpg format don’t have transparency. switch to TYPE_INT_RGB and it will work
July 30th, 2010 at 3:25 pm
Method 1 worked great. Thanks. Too bad you can’t just scale an image.
January 28th, 2011 at 3:24 pm
I am very thankful to this topic because it really gives great information
February 25th, 2011 at 9:47 pm
You might rather want to use the original image.getType() so you always get what you started with (TYPE_INT_RGB for jpg and TYPE_INT_ARGB for gif or png).
March 4th, 2011 at 8:03 am
The both do scale I believe. What about resize? To me resize means:
1) make the image larger with the new pixels some default value
2) make the image smaller by clobbering existing pixels
March 4th, 2011 at 8:21 am
Good point. A resize would just involve creating a new image and directly copying in the pixels from the source.
Will look to add an example.
November 16th, 2011 at 5:34 am
I am using the first version and everything is working perfectly on Windows, but when I run this on Unix (at least IBM AIX, I have not tested others), it will sometimes change the colors of the image. Sometimes the images get a pink or yellow overlay on them sometimes.
Again, this only happens on Unix. It is working fine on Windows. Here is the code I am using that deals with scaling the image. Am I doing anything wrong here that would cause this to not work on Unix, or is there a better way of doing this?
Thanks for your help
int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
BufferedImage resizedImage = new BufferedImage(width, height, type);
Graphics2D g = resizedImage.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(image, 0, 0, width, height, null);
g.dispose();