Rotate and Flip Images in Java

Rotate or Flip images in Java

Image rotation and flipping are among the key features of the image editors. While working with images or creating your own image editor in Java, you may need to implement these features. To achieve that, this article shows how to rotate and flip images programmatically in Java. Particularly, we will cover how to rotate an image, flip an image, and rotate and flip an image at the same time.

Java API to Rotate and Flip Images

Aspose.Imaging for Java is an image processing API that allows you to manipulate a wide range of image formats. The API makes it quite easier for you to manipulate images without writing a lot of code. You can download the API or install it into your Java applications using the following Maven configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-imaging-java</artifactId>
    <version>21.12</version>
    <classifier>jdk16</classifier>
</dependency>

Rotate and Flip an Image in Java

You can either rotate an image at a particular angle or the standard rotation angles which are 90, 180, and 270 degrees. In addition, the image can be flipped horizontally or vertically. Aspose.Imaging for Java provides a single method to rotate as well as flip an image. The RotateFlipType enum is used to specify the rotation angle and flip type. You can perform one of the following operations on an image:

  • Rotate an image
  • Flip an image
  • Rotate and flip an image at the same time

Rotate an Image in Java

To rotate an image without flipping, you can choose one of the following values of RotateFlipType enum.

  • Rotate180FlipNone: 180-degree rotation without flipping
  • Rotate270FlipNone: 270-degree rotation without flipping
  • Rotate90FlipNone: 90-degree rotation without flipping

Let’s see how to rotate an image at 270 degrees in Java.

The following code snippet shows how to rotate an image in Java.

The following are the input image (left) and the resultant image (right) that we get after rotation.

rotate an image in java
Rotate an Image

Flip an Image in Java

To flip an image, the following RotateFlipType values are used.

  • RotateNoneFlipX: No rotation with horizontal flipping
  • RotateNoneFlipY: No rotation with vertical flipping
  • RotateNoneFlipXY: No rotation with horizontal and vertical flipping

The following are the steps to flip an image in Java.

The following code sample shows how to flip an image.

The following are the input image and the resultant flipped image.

Flip an image in Java
Flip an Image

Rotate and Flip an Image in Java

In the previous sections, you have seen how to either rotate an image or flip it. However, in some cases, you have to perform both operations at the same time. To achieve this, the following RotateFlipType values are used.

  • Rotate180FlipX: 180-degree rotation with horizontal flipping
  • Rotate180FlipXY: 180-degree rotation with horizontal and vertical flipping
  • Rotate180FlipY: 180-degree rotation with vertical flipping
  • Rotate270FlipX: 270-degree rotation with horizontal flipping
  • Rotate270FlipXY: 270-degree rotation with horizontal and vertical flipping
  • Rotate270FlipY: 270-degree rotation with vertical flipping
  • Rotate90FlipX: 90-degree rotation with horizontal flipping
  • Rotate90FlipXY: 90-degree rotation with horizontal and vertical flipping
  • Rotate90FlipY: 90-degree rotation with vertical flipping

The following are the steps to rotate and flip an image at the same time in Java.

The following code snippet shows how to perform rotation and flipping of an image at the same time.

The following is the resultant image (right) that we get after applying rotation and flipping.

Rotate and flip an image in Java
Rotate and Flip an Image

Rotate Image on a Particular Angle

You can also rotate an image at an angle other than 90, 180, and 270. For this, the API provides the Image.rotate(float angle) method which accepts the rotation angle as a parameter. The following code sample shows how to rotate an image at 20 degrees in Java.

The following is the output of the code sample above.

rotate image at particular angle in java

Get a Free API License

You can get a free temporary license to use Aspose.Imaging for Java without evaluation limitations.

Conclusion

In this article, you have learned how to rotate and flip images in Java. Furthermore, you have seen how to rotate an image, flip an image, and rotate and flip an image at the same time. In addition, the article also covered how to rotate an image at a particular angle other than 90, 180, and 270 degrees. In case you want to explore more about the Java image processing API, visit documentation. Also, you can download the source code samples of the API from GitHub. In case of any questions, you can reach us at our forum.

See Also