Image Processing in Java-Changing Orientation of an Image

Digital image analysis and computer vision both heavily rely on image processing. In order to obtain the intended results, this calls for image alteration. Java has many libraries that are powerful and have robust features. Using them, we can manipulate the images.

Manipulation of image orientation is a prevalent operation. The change in orientation of an image means the rotation of an image to any required angle or its flipping. This paper is going to help in doing this using Java-based inbuilt libraries.

Prerequisites

The below prerequisites must be there before we dive into the concept.

JDK: JDK must be installed in your pc or system. If in case it is not installed you can easily download it from the Oracle official website.

IDE: There are many different IDEs available till to date. You can use any one of them.

ImageIO Library: The javax.imageio package must be imported because it has the ImageIO class which helps us to read or write an image file.

Let's begin with rotating an Image.

Rotating an Image

This method rotates an image in degrees such as 90, 180, and 270 degrees.

File Name: ImageRotation.java

Input Image:

Image Processing in Java-Changing Orientation of an Image

Output:

 
C:\Users\deeks\OneDrive\Desktop\java>javac ImageRotation.java
C:\Users\deeks\OneDrive\Desktop\java>java ImageRotation
The image rotated successfully.   

Output Image:

Image Processing in Java-Changing Orientation of an Image

Explanation:

BufferedImage: It will contain the data for the image.

Graphics2D: The class is used to render the 2D shapes or images. This class is under the java.awt package.

AffineTransform: The class is responsible for geometric transformations like rotation.

rotateImage Method: A method that takes in the original image and an angle, then outputs it rotated by such amount.

ImageIO: It is a class that provides methods for reading and writing images.

Flipping an Image

An image can be flipped either horizontally or vertically. The following code will flip an image.

File Name: ImageFlipping.java

Input Image:

Image Processing in Java-Changing Orientation of an Image

Output:

 
C:\Users\deeks\OneDrive\Desktop\java>javac ImageFlipping.java
C:\Users\deeks\OneDrive\Desktop\java>java ImageFlipping
The image flipped successfully.   

Output Image:

Image Processing in Java-Changing Orientation of an Image

Explanation:

FlipImage: A method that takes in an original image and another boolean for whether it should be a horizontal or vertical flip as parameters and returns the flipped image.

AffineTransform: Flips occur based on the amount of scaling along the x- or y-axis. For horizontal flipping, we scale the x-axis by -1 and then translate it with respect to the width. Similarly, for vertical flipping, we scale the y-axis by -1 and then translate it with respect to the height.

Advanced Rotation Techniques

Arbitrarily Rotating an Image

Below is an example of how to rotate an image with an arbitrary angle:

Explanation:

Sin and Cos Calculations: When we rotate by arbitrary angles, the bounding box of the rotated image may increase in size. The calculation of sine and cosine helps to determine new width and height.

Translation: We translate the image so that the rotation actually occurs about its centre and not the origin.

AffineTransform: It allow a combination of translation of an image along with a rotation in placement and alignment of the image in the correct manner.

Advanced Flipping Techniques

In simple words, flipping an image is nothing more than changing the orientation with respect to a given axis. While basic operations assume horizontal or vertical flips, the former can be combined to produce diagonal flips.

Diagonal Flipping

One can perform a diagonal flip by a horizontal flip followed by a vertical one:

Explanation:

Horizontal Flip: The initial operation is for doing the horizontal flip for the image.

Flip Vertically: The next operation is for doing the diagonal flip for the image.

Conclusion

In this section, we have run through a glimpse of image processing in Java-more precisely, handling image orientation. We have covered two primary operations: rotation and flipping. You can easily do all the manipulation of images to get any desired orientation with libraries like ImageIO, Graphics2D, and AffineTransform of Java.

These simple operations can be the basis for more advanced image processing. The more you work with image processing in Java, the more complex transformations, filtering, and varieties of analysis techniques you can keep finding to increase the potential of your projects.