Nirali Nirali is a Java Developer at IDR Solutions and also enjoys being part of the business development team. She is currently studying for her MSc Computing at De Montfort University.

Image Rotation In Java

1 min read

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

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. You can use Java’s ImageIO or a third-party 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.

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")); 

And that is all you need to do!!! 

This article is part of our series index Image Processing in Java. about how to process images in Java including Image Scaling in Java, Image Scaling options in Java.

More about JDeli

In this article, we show you how to use our Java Image library as well as ImageIO to save images in Java. JDeli offers a range of advantages over ImageIO and alternatives, including:

  • prevents heap related JVM crashes
  • implements unsupported image formats
  • reduce output file size
  • improve read/write performance
  • supports threading
  • superior image scaling algorithms

Learn more about JDeli, or download to try it yourself.

Nirali Nirali is a Java Developer at IDR Solutions and also enjoys being part of the business development team. She is currently studying for her MSc Computing at De Montfort University.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2019. All rights reserved.