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.
! Gracias por compartir tu codigo ! ……(Thank you for sharing your code)
Me sirvio mucho
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?
Image quality is not good