How to rotate image in java?
Tags: java, oop, programming
Published: 2020-12-17 16:22:01

In this article, I am going to explain how to rotate the image in java. So you have to create a new Java project for this purpose. Let’s get into the approach of this article.

---
title:How to rotate image in java?
tags: java, programming
canonical_url: https://kodlogs.com/blog/2409/how-to-rotate-image-in-java
---

#Approach
1. Create a new Java project and I have named it as imageRotation. Inside of this project, create a package called bmg and inside of this package, create a new class and named it as ImageRotate.
2. Firstly, import the buffered image file. You have exception iterate image input output image reader and image input string classes.
3. I have created two final static fields, so public static final and it is integer which is named as ROTATE_LEFT and I have given it a value 1 and similarly the second one will be similar to the first one but it is named as ROTATE_RIGHT and its value is -1.
4. Now lets create method for rotating the image for 90 degrees either left or right. Create public static void and it is named as rotate90(file input, file output, int direction). Where file input is the parameter of this method. So the original image, the second parameter will be file output that is going to be the rotated image  and last parameter is going to be direction of rotation.
5. Create the try and catch block in order to catch any exceptions and catch the IOexception and if we catch it let’s just print stacktrace.
6. Now instead of our try block we first going to need a image count input stream so ImageInputStream and call it iis. So we need image enetered output. Call the method create iis and parameter is going to be live input file.
7. Iterator(ImageIterator)  Iterator calls the image input output get readers aand we pass iis as a parameter and we now need the image reader so ImageReader reader = iterator.next().
8. Get the format of the input image, by String format = reader.getFormatName(); Now format the input image and create the buffered image from the original image shown in the next line of program. Declare integer width, height so that we can rotate the image.
9. Follow the remaining code as mentioned below you will easilyunderstand it.
10. After the four loops we need to write image input output and we need to write the rotated image format. We have to format by calling the get format name on the image reader and the output on the output file.
11. Create a new class and call it test and it has main method. Import java.io.file here and create the file inputs and place the input image by copying the image location and paste it in test.java file and save the file.
12. Now right-click on file and select  Run as and click as java application. Now you can see you have your rotated image in your image location which is rotated by 90 degrees left or right.
##Program
```
//ImageRotate.java file


import  java.awt.image.BufferedImage;

import  java.io.file;

import  java.io.IOException;

import java.util.Iterator;

import java.imageio.ImageIO;

import java.imageio.ImageReader;

import java.imageio.stream.ImageInputStream;

public class ImageRotation {

public static final int ROTATE_LEFT = 1;

public static final int ROTATE_RIGHT= -1;

public static void main rotate90(File input, File output, int direction) {

try {

ImageInputStream iis = ImageIO.createImageInputStream(input);

Iterator(ImageReader) iterator = ImageIO.getImageReaders(iis);

ImageReader reader =iterator.next();

String format = reader.getFormatName();

BufferedImage image = ImageIO.read(iis);

int width = image.getwidth();

int height = image.height();

BufferedImage rotated = new BufferedImage(height, width, image.getType());

for(int y=0;y<height;y++) {

for(int x=0;x<width;x++) {

switch(direction) {

case ROTATE_LEFT:

rotated.setRGB(y, (width-1)-x, image.getRCB(x,y));

break;

case ROTATE_RIGHT:

rotated.setRGB((height-1)-y,x, image.getRCB(x,y));

}

}

}

ImageIO.write(rotated, format, output);

}

Catch(IOException ex) {

Ex.printStackTrace();

}

}

}
```
***Testfile.java***
```
//Test.java file

Package img;

Import java.io.file;

Public class Test {

Public static void main(String[ ] args) {

File input = new file(“image_location\\image_type”);

File output = new File((“image_location\\rotate90.image_type”);

ImageRotate.rotate9String arg0, output, ImageRotate.ROTATE_LEFT);

ImageRotate.rotate9String arg0, output, ImageRotate.ROTATE_RIGHT);

System.out.println(“FInished”);

}

}
```

###Output
`
Image rotated by 90 degrees left or right.
`