• 11
• 9
• 10
• 9
• 10

# Scaling BufferedImage in Java

## Recommended Posts

Recently I've been porting my code from Android Java to Java. Apart from a number of APIs, much of the code remain the same. And I got solutions to several APIs by checking the docs or googling. But one  Android APIs to Java still eludes me

Creating a scaled bitmap (BufferedImage) in Java

The exact (remaining) Android scaling functionality I need to port to Java is this

myBitmap  = Bitmap.createScaledBitmap( originalBitmap, 480, 800, false );

What I could find for Java is the code below (the first loads image but doesn't allow parameters to scale to different height and width and the second creates new image but doesn't scale an existing bufferedImage )

myBitmap = ImageIO.read(new File( filePath + filename ));
AND
myBitmap = new BufferedImage(480, 800,   BufferedImage.TYPE_INT_RGB);

//(which is not scaling an existing image)

How can I set size (w,h) parameters to scale an existing bitmap to different dimensions? I guess its out there somewhere but I'm just missing it. Couldn't find it in this doc drop-down or am I looking in the wrong place

[attachment=35871:java bitmaps.jpg]

##### Share on other sites

If it's not in the API list, you can be reasonably sure it doesn't exist for BufferedImage, IDEs for Java are typically quite good at guessing what methods you can use for a class.

Next step is usually generalizing your search (scaling image sounds like a common enough problem for someone to have posted about it), something like "java scale image" or something similar. That gives me

http://stackoverflow.com/questions/15558202/how-to-resize-image-in-java

https://docs.oracle.com/javase/7/docs/api/java/awt/Image.html

and a few others, that look useful, but I am sure you can find those too :)

##### Share on other sites

Yeah the first of the links you posted had the solution, thanks

I had actually come across this during my search but I didn't bother going beyond the first 2 answers because the first two had 26 upvotes between them while the third had just 4. More so at the time, I was looking at so many other SO pages

But on Reading your post I looked more closely and I found the 3rd answer was the solution. Very simple eventually. In fact it was similar and a simpler version to the one I was looking at here just before reading your post

[attachment=35877:thumbs up.jpg]

##### Share on other sites

The joys of the Interwebs, good answers are never where you expect them :)