In Java, to resize (or scale) an image read from an image file and save the scaled image into another image file, we can follow these steps:

    •           Create a BufferedImage object for the input image by calling the method read(File) of the ImageIO class.
    •           Create a BufferedImage object for the output image with a desired width and height.
    •           Obtain a Graphics2D object from the output image’s BufferedImage object.
    •           Draw the input image’s BufferedImageobject onto the output image’s Graphics2D object.
    •            Save the output image to a file on disk using the method write(File) of the ImageIO class.

 

Here is a utility class which implements two methods for resizing an image and saves the result to another image file. 

package net.codejava.graphic;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * This program demonstrates how to resize an image.
 *
 * @author www.codejava.net
 *
 */
public class ImageResizer {

	/**
	 * Resizes an image to a absolute width and height (the image may not be
	 * proportional)
	 * @param inputImagePath Path of the original image
	 * @param outputImagePath Path to save the resized image
	 * @param scaledWidth absolute width in pixels
	 * @param scaledHeight absolute height in pixels
	 * @throws IOException
	 */
	public static void resize(String inputImagePath,
			String outputImagePath, int scaledWidth, int scaledHeight)
			throws IOException {
		// reads input image
		File inputFile = new File(inputImagePath);
		BufferedImage inputImage = ImageIO.read(inputFile);

		// creates output image
		BufferedImage outputImage = new BufferedImage(scaledWidth,
				scaledHeight, inputImage.getType());

		// scales the input image to the output image
		Graphics2D g2d = outputImage.createGraphics();
		g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
		g2d.dispose();

		// extracts extension of output file
		String formatName = outputImagePath.substring(outputImagePath
				.lastIndexOf(".") + 1);

		// writes to output file
		ImageIO.write(outputImage, formatName, new File(outputImagePath));
	}

	/**
	 * Resizes an image by a percentage of original size (proportional).
	 * @param inputImagePath Path of the original image
	 * @param outputImagePath Path to save the resized image
	 * @param percent a double number specifies percentage of the output image
	 * over the input image.
	 * @throws IOException
	 */
	public static void resize(String inputImagePath,
			String outputImagePath, double percent) throws IOException {
		File inputFile = new File(inputImagePath);
		BufferedImage inputImage = ImageIO.read(inputFile);
		int scaledWidth = (int) (inputImage.getWidth() * percent);
		int scaledHeight = (int) (inputImage.getHeight() * percent);
		resize(inputImagePath, outputImagePath, scaledWidth, scaledHeight);
	}

	/**
	 * Test resizing images
	 */
	public static void main(String[] args) {
		String inputImagePath = "D:/Photo/Puppy.jpg";
		String outputImagePath1 = "D:/Photo/Puppy_Fixed.jpg";
		String outputImagePath2 = "D:/Photo/Puppy_Smaller.jpg";
		String outputImagePath3 = "D:/Photo/Puppy_Bigger.jpg";

		try {
			// resize to a fixed width (not proportional)
			int scaledWidth = 1024;
			int scaledHeight = 768;
			ImageResizer.resize(inputImagePath, outputImagePath1, scaledWidth, scaledHeight);

			// resize smaller by 50%
			double percent = 0.5;
			ImageResizer.resize(inputImagePath, outputImagePath2, percent);

			// resize bigger by 50%
			percent = 1.5;
			ImageResizer.resize(inputImagePath, outputImagePath3, percent);

		} catch (IOException ex) {
			System.out.println("Error resizing the image.");
			ex.printStackTrace();
		}
	}

}

Note:the resized images may not have same quality as the original ones.

 

Other Java Graphics Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#11yusuf2021-04-29 03:23
thank you for software, it works
Quote
#10Sneh2020-12-06 12:44
doesn't work :(y'all know how I can use "variable name".scale?
Quote
#9Noah Ortega2020-05-10 16:07
Thank you very much! Your code helped me out in creating a project :) credited you in the code
github.com/NoahOrtega/NMSVR-Screenshot-Fix
Quote
#8John2020-02-28 05:30
Please I need source code to resize the passport in the registration form I programmed using filechooser. Thanks brotheru
Quote
#7Tyron2019-12-12 18:09
nice code . Can easily be understood
Quote