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.
final double rads = Math.toRadians(90);
final double sin = Math.abs(Math.sin(rads));
final double cos = Math.abs(Math.cos(rads));
final int w = (int) Math.floor(image.getWidth() * cos + image.getHeight() * sin);
final int h = (int) Math.floor(image.getHeight() * cos + image.getWidth() * sin);
final BufferedImage rotatedImage = new BufferedImage(w, h, image.getType());
final AffineTransform at = new AffineTransform();
at.translate(w / 2, h / 2);
at.rotate(rads,0, 0);
at.translate(-image.getWidth() / 2, -image.getHeight() / 2);
final AffineTransformOp rotateOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
rotateOp.filter(image,rotatedImage);
You can rotate an image using JDeli library in fewer lines of code:
final Rotate rotate = new Rotate(90);
BufferedImage rotatedImage = rotate.apply(image);
Step 3: Resave the BufferedImage to a new File
You can save an image file using Java ImageIO (which is built into Java)
ImageIO.write(rotatedImage, "JPG", new File("C:\\path\\to\\rotatedImage.jpg"));
You can also save image files using other open-source or commercial libraries. Here is an example using JDeli Image library.
JDeli.write(rotatedImage, "JPG", new File("C:\\path\\to\\rotatedImage.jpg"));
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?
// Read an image
BufferedImage bufferedImage = JDeli.read(avifImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(dicomImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(heicImageFile);
// Write an image
JDeli.write(bufferedImage, "heic", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpegImageFile);
// Write an image
JDeli.write(bufferedImage, "jpeg", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpeg2000ImageFile);
// Write an image
JDeli.write(bufferedImage, "jpx", outputStreamOrFile);
// Write an image
JDeli.write(bufferedImage, "pdf", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(pngImageFile);
// Write an image
JDeli.write(bufferedImage, "png", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(tiffImageFile);
// Write an image
JDeli.write(bufferedImage, "tiff", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(webpImageFile);
// Write an image
JDeli.write(bufferedImage, "webp", outputStreamOrFile);
What is JDeli?
JDeli is a commercial Java Image library that is used to read, write, convert, manipulate and process many different image formats.
Why use JDeli?
To handle many well known formats such as JPEG, PNG, TIFF as well as newer formats like AVIF, HEIC and JPEG XL in java with no calls to any external system or third party library.
What licenses are available?
We have 3 licenses available:
Server for on premises and cloud servers, Distribution for use in a named end user applications, and Custom for more demanding requirements.
How does JDeli compare?
We work hard to make sure JDeli performance is better than or similar to other java image libraries. Check out our benchmarks to see just how well JDeli performs.
rotatedImage = rotateOp.filter(image,after);
where is local variable after?
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.