Resize an image in Java

In this article we will see how to resize an image using the core java libraries.

Overview

In this example we will use the javax.imageio and java.awt package. We will not use any third party libraries for this example.

Code

First we will read the image and will create a BufferedImage object from it. The read method returns a BufferedImage as the result of decoding a supplied InputStream with an ImageReader chosen automatically from among those currently registered. The InputStream is wrapped in an ImageInputStream.

BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(image));

Now we will create a new object of BufferedImage by passing the new width and height.

BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, bufferedImage.getType());

Now we will draw the new image

Graphics2D graphics = resizedImage.createGraphics();
graphics.drawImage(bufferedImage, 0, 0, newWidth, newHeight, null);
graphics.dispose();

The createGraphics method creates a Graphics2D which can be used to draw into the BufferedImage.

ResizeImaga.java

package com.meraj;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ResizeImage {

    public BufferedImage resizeImage(byte[] image, int newWidth, int newHeight) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(image));
        BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, bufferedImage.getType());
        Graphics2D graphics = resizedImage.createGraphics();
        graphics.drawImage(bufferedImage, 0, 0, newWidth, newHeight, null);
        graphics.dispose();

        return resizedImage;
    }
}

Test

In this section we will see how to test our functionality

@Test
public void resizeImage() throws IOException {
    final URL imageStream = getClass().getClassLoader().getResource("cat.jpeg");
    final BufferedImage actualImage = ImageIO.read(imageStream);
    assertEquals(318, actualImage.getWidth());
    assertEquals(159, actualImage.getHeight());
    final byte[] imageBytes = IOUtils.toByteArray(imageStream);
    final BufferedImage resizedImage = this.resizeImage.resizeImage(imageBytes, 250, 140);
    assertEquals(250, resizedImage.getWidth());
    assertEquals(140, resizedImage.getHeight());
}

Conclusion

In this article we discussed how to resize an image in Java. We saw that this is achievable using the classes provided in JDK and we don’t need to rely on any third party libraries. More details of the ImageIO class can be found here. Full source code can be found at java-resize-image

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: