Java: resize an image keeping the proportions correct

This ones very useful if your looking to resize an external image in Java. Recently I have been needing this solution and once I had implemented the code I decided I would share it for anyone else looking to do the same.

How it works

This function will take the Source Image and Destination Image paths and then the desired Width and Height you would like the image to resize to. It will then resize the Source Image to your desired constraints, but it will make sure the image stays in proportion! Once it has resized the image is then output into the Destination Image path.

All images are converted to PNG, but you can add your own implementation if you wish to allow for other file formats by editing the “png” part in the function.

Example Class

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author dean.williams
 */
public class NewClass {

 Boolean status = resizeImage("bond.jpg", "bong.png",100,100);

 /**
 * Resize an image
 * By Dean Williams - http://dean.resplace.net
 * @param sourceImg The source of the image to resize.
 * @param destImg The destination of the resized image.
 * @param Width The maximum width you want the new image to be, use 0 for source width.
 * @param Height The maximum height you want the new image to be, use 0 for source height.
 * @return true if successful and false if unsuccessful.
 */
 public static Boolean resizeImage(String sourceImg, String destImg, Integer Width, Integer Height) {
 BufferedImage origImage;
 try {

 origImage = ImageIO.read(new File(sourceImg));
 int type = origImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : origImage.getType();

 //*Special* if the width or height is 0 use image src dimensions
 if (Width == 0) {
 Width = origImage.getWidth();
 }
 if (Height == 0) {
 Height = origImage.getHeight();
 }

 int fHeight = Height;
 int fWidth = Width;

 //Work out the resized width/height
 if (origImage.getHeight() > Height || origImage.getWidth() > Width) {
 fHeight = Height;
 int wid = Width;
 float sum = (float)origImage.getWidth() / (float)origImage.getHeight();
 fWidth = Math.round(fHeight * sum);

 if (fWidth > wid) {
 //rezise again for the width this time
 fHeight = Math.round(wid/sum);
 fWidth = wid;
 }
 }

 BufferedImage resizedImage = new BufferedImage(fWidth, fHeight, 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(origImage, 0, 0, fWidth, fHeight, null);
 g.dispose();

 ImageIO.write(resizedImage, "png", new File(destImg));

 } catch (IOException ex) {
 Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
 return false;
 }

 return true;
 }
}

This example class takes bong.jpg and resizes it so it fits into 100x100 writing the resulting image into bond.png.

Here is an example image and example output:

image

 

Other Options

The Width/Height can be set to 0, which will tell the function to use the original dimension from the Source Image, obviously if you set both to 0 your Destination Image will be the same as the Source Image but as a PNG.

 

Hope you found this useful, if you use this please make sure to keep the credit attached on your code. And as always please leave comments, suggestions or feedback in the comments!

Facebooktwitterredditpinterestlinkedinmail
Author: Dean WilliamsI'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips

Post Tags:
0 0 votes
Article Rating
Subscribe
Notify of
2 Comments
Inline Feedbacks
View all comments

Excellent! You made my day. I was seatrching for this and when I finally got it,
it was this depreciated com.sun codes. So to come across your code is a sved by the bell.
Thank You
bashiro
Norway

Thanks mate :)