Java Resize Image With Examples

Today we will look into java resize image program. Recently I had to upload a lot of images to an FTP server but the images size were huge. Since I was going to use them only on web pages, it was wise to resize them to a smaller size and then upload it.

A few days back I provided a list of online tools to reduce image size. But when you have hundreds of images, it’s better to invest some time and write a program that does this work for you. So let’s see how we can resize an image in java.

Java Resize Image

We can use java.awt.Graphics2D class to resize image in java.

Below is the program that searches all the files in a directory and resizes them to the given size and saves it to a different directory.

The sample program here saves images in both PNG and JPG format but you can easily change it to serve your specific requirement.

Java Image Resize Program

  • First few lines uses Java IO to retrieve the list of files from the directory and then in the for loop, each file is processed.
  • resizeImage is the method where Graphics2D is used to resize image and return it as BufferedImage object.
  • Again we use ImageIO to write it into the file system in both PNG and JPG format.

Java Resize Image Important Points

  1. You can use RenderingHints to get high quality image at the cost of processing time. If you are not reducing images to a very small size, you might want to avoid them for faster processing. It depends on your situation that you need fast processing or better quality.
  2. The method doesn’t throw any Exception even if there are any files that are not image file like text, doc or pdf files. In that case, it will resize it and you will get a black colored image. You can easily extend the code to skip these files by filtering through their name.
  3. PNG image size is more than JPG image size for same resolution and it’s significant. So if you want to resize the image for web pages, you might want to use JPG format to save page loading time.

Java Image Resizer Program Results

I placed one PNG and one PDF file in the image directory and executed the program.

Input image is Eclipse Juno image of size 168KB:


Here are the output images with size:

java image resize output

PNG image without Hints 23KB

image resize java program

JPG image without Hints 4KB

java image resize Graphics2D

PNG image with Hints 24KB

image resize java program

JPG image with Hints 4KB

error image resize java program

Non image file output in PNG and JPG

I hope the given program help you in resizing images quickly.

Java Resize Image with Aspect Ratio

In a normal scenario, you want to keep the image aspect ratio else it will look stretched from one side. Here is the code snippet that will help you in maintaining the aspect ratio.

Note that in this case, you would have to make sure that there are only images in the directory else it will throw exception.

Further Reading: Here is another program to upload files on server using Apache Commons Net FTP API.

Reference: Graphics2D API Doc

By admin

Leave a Reply

%d bloggers like this: