Site iconJava PDF Blog

Image Rotation In Java

In this article I will show you how to rotate images in Java.

Introduction

The simplest way to rotate an image in Java is to use the AffineTransformOp class. You can load an image into Java as a BufferedImage and then apply the rotate operation to generate a new BufferedImage.

Your options are Java’s ImageIO or a third-party pure Java image library such as JDeli to load and save the image. We have used JDeli in our example below because it works with all JPEG files and the widest range of filetypes.

Designed with enterprise needs in mind, JDeli delivers fast, efficient image handling for Java applications across various formats and workflows.

Step 1: Load an image file into Java as a BufferedImage

You can load an image file using Java ImageIO (which is built into Java)

BufferedImage image = ImageIO.read(new File("C:\\path\\to\\image.jpg"));

You can also load image files using other open source or commercial libraries. Here is an example using JDeli Image library.

BufferedImage image = JDeli.read(new File("C:\\path\\to\\image.jpg"));

Step 2: Rotate the BufferedImage in Java

This code will rotate the image by 90 degrees.

You can rotate an image using JDeli library in fewer lines of code:


 

Step 3: Resave the BufferedImage to a new File

You can save an image file using Java ImageIO (which is built into Java)

You can also save image files using other open-source or commercial libraries. Here is an example using JDeli Image library.


 

Common Rotation Scenarios and Tips

JDeli can do this and much more! Find out other ways you can manipulate images in Java using JDeli.

FAQs

Q: Can I rotate images in formats other than JPG?

A: Yes, JDeli supports many formats including PNG, TIFF, HEIC, WebP, while ImageIO supports popular formats like JPG and PNG natively.

Q: How do I rotate images dynamically based on user input?

A: Capture rotation degrees from user input, convert to radians, and apply the affine rotation as shown. Both ImageIO and JDeli methods support dynamic angles.