JAVA Complete Code to Resize Images

While working with applications involving Photos, we may come across a situation where the actual photo size is not as per our need and we need to resize the images to meet the dimension needs while maintaining the aspect ratio without losing the picture quality. In java, we can make use of image editing features provided in java.awt.image package and also javax.imageio package to resize pictures. In this article, you will get Complete Java Code to Resize Images.

In this post, I am going to explain complete java code for image resizing which contains a main method and a resize method. The resize method expects following parameters:

1. Source Image name with full path

2. Resized image name with full path

3. Width (pixels) for resized image.

The method resizes the image and sends back the response (success message or exception message if there is any exception).

 

The source file being used for this example is iqbal.png which has a size 254×198. When calling the resize method from the main method; width value is specified as 200 so that resized image has a width of 200 pixels and height ratio is maintained.

Below is the complete code for image resizing in Java

import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class ResizeImage

{
public static void main(String args[]) throws Exception
{
// Call static method to resize an image to 200 pixels width
String result_message= resize(“C:\Pics\iqbal.png”,”C:\Pics\resized_pic.png”,200);
System.out.println(result_message);

}
// This method will resize the image to the specified width and will maintain aspect ratio for the height of the picture to maintain quality
public static String resize(String sourceFile, String ResizedFile, int width) throws Exception
{
try
{
File f = new File(sourceFile);
if (!f.exists())
{
return “File doesn’t exist”;
}

// Logic to implement image resizing
BufferedImage bim=ImageIO.read(new FileInputStream(sourceFile));
Image resizedImg=bim.getScaledInstance(width,-1,Image.SCALE_FAST);
int scaled_height=resizedImg.getHeight(null);

BufferedImage rBimg=new BufferedImage(width,scaled_height,bim.getType());
// Create Graphics object
Graphics2D g=rBimg.createGraphics();// Draw the resizedImg from 0,0 with no ImageObserver
g.drawImage(resizedImg,0,0,null);

// Dispose the Graphics object, we no longer need it
g.dispose();

ImageIO.write(rBimg,ResizedFile.substring(ResizedFile.indexOf(“.”)+1),new FileOutputStream(ResizedFile));
}
catch (Exception e)
{
return e.getMessage();
}
return “Picture Resized Successfully”;
}
}

 

After compiling this code and running it; you will get the new picture created in the specified location with the name resized_pic.java and the new image will have a width of 200 pixels and height will be based on the aspect ratio without losing picture quality.

Ajmal Abbasi

Ajmal Hussain Abbasi is Integration Consultant By Profession with 13+ years experience in Integration domain mainly with TIBCO products. He has extensive practical knowledge of TIBCO Business Works, TIBCO Cloud, TIBCO Flogo, TIBCO Mashery, TIBCO Spotfire, EMS and TIBCO ActiveSpaces. He has worked on a number of highly critical integration projects in various sectors by using his skills in TIBCO Flogo, TIBCO API Management (Mashery), TCI, Tibco Designer, TIBCO Business Studio, Adapters, TIBCO EMS, RV, Administrator, TIBCO BE, TIBCO ActiveSpaces etc. Ajmal Abbasi has experience with MuleSoft ESB as well. Ajmal Abbasi is also experienced in the area of API Management particularly with WSO2 API management platforms. Ajmal Abbasi is also experienced in developing solutions using Core Java and J2EE Technologies. You can contact Ajmal Abbasi for Consultancy, Technical Assistance and Technical Discussions.

More Posts - Website - Facebook - LinkedIn - YouTube

3 thoughts on “JAVA Complete Code to Resize Images

  1. William

    ! Gracias por compartir tu codigo ! ……(Thank you for sharing your code)

    Me sirvio mucho

    Reply
  2. Neil Holkar

    great sir!
    I tried to resize the image to its original size but the resized image size was less than the original image size.Maybe the bytes are losing.Is there a way to not to lose bytes during resize?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *