Amy Pearson Amy is a Java developer, member of the support team and product manager for JDeli. Out of hours she enjoys gaming and Virtual reality.

Image Rotation In Java

1 min read

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

  • Arbitrary Angles: Use Math.toRadians(angleInDegrees) to specify any rotation angle.
  • Maintaining Quality: Use bilinear interpolation (AffineTransformOp.TYPE_BILINEAR) for smoother image quality.
  • Handling Transparency: Both ImageIO and JDeli support transparent images (e.g., PNG) — ensure the output format supports alpha transparency.
  • Batch Processing: JDeli’s API works well for batch image manipulation operations.

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.



Are you a Java Developer working with Image files?

Amy Pearson Amy is a Java developer, member of the support team and product manager for JDeli. Out of hours she enjoys gaming and Virtual reality.

2 Replies to “Image Rotation In Java”

    1. Dear Cerakoted,
      Thanking you for spotting that.
      We have now updated our blog post.
      This may clear your query. Let us know if u have any other queries.

Comments are closed.