Java Program to Rotate an Image Last Updated : 25 Nov, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The problem statement is to rotate an image clockwise 90 degrees for which here we will be using some in-built methods of BufferedImage class and Color c Classes required to perform the operation is as follows: To read and write an image file we have to import the File class. This class represents file and directory path names in general.To handle errors we use the IOException class.To hold the image we create the BufferedImage object for that we use BufferedImage class. This object is used to store an image in RAM.To perform the image read-write operation we will import the ImageIO class. This class has static methods to read and write an image.This Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering 2-dimensional shapes, text, and images on the Java(tm) platform. Example: Java // Java program to rotate image by 90 degrees clockwise // Importing classes from java.awt package for // painting graphics and images import java.awt.Graphics2D; import java.awt.image.BufferedImage; // Importing input output classes import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; // Main class public class GFG { // Method 1 // To return rotated image public static BufferedImage rotate(BufferedImage img) { // Getting Dimensions of image int width = img.getWidth(); int height = img.getHeight(); // Creating a new buffered image BufferedImage newImage = new BufferedImage( img.getWidth(), img.getHeight(), img.getType()); // creating Graphics in buffered image Graphics2D g2 = newImage.createGraphics(); // Rotating image by degrees using toradians() // method // and setting new dimension t it g2.rotate(Math.toRadians(90), width / 2, height / 2); g2.drawImage(img, null, 0, 0); // Return rotated buffer image return newImage; } // Method 2 // Main driver method public static void main(String[] args) { // try block to check for exceptions try { // Reading original image BufferedImage originalImg = ImageIO.read( new File("D:/test/Image.jpeg")); // Getting and Printing dimensions of original // image System.out.println("Original Image Dimension: " + originalImg.getWidth() + "x" + originalImg.getHeight()); // Creating a subimage of given dimensions BufferedImage SubImg = rotate(originalImg); // Printing Dimensions of new image created // (Rotated image) System.out.println("Cropped Image Dimension: " + SubImg.getWidth() + "x" + SubImg.getHeight()); // Creating new file for rotated image File outputfile = new File("D:/test/ImageRotated.jpeg"); // Writing image in new file created ImageIO.write(SubImg, "jpg", outputfile); // Printing executed message System.out.println( "Image rotated successfully: " + outputfile.getPath()); } // Catch block to handle the exception catch (IOException e) { // Print the line number where exception // occurred e.printStackTrace(); } } } Output: After executing the program console will show dimensions and executed message and a new rotated image will be created at the path entered as shown: Comment More infoAdvertise with us Next Article Java Program to Convert Byte Array to Image S sam_2200 Follow Improve Article Tags : Misc Java Java Programs Practice Tags : JavaMisc Similar Reads Java Program to Rotate bits of a number Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end. In the left rotation, the bits that fall off at the left end are put back at the right end. In the right rotation, the bits that fall off at th 3 min read Java Program to Rotate Matrix Elements A matrix is simply a two-dimensional array. So, the goal is to deal with fixed indices at which elements are present and to perform operations on indexes such that elements on the addressed should be swapped in such a manner that it should look out as the matrix is rotated.For a given matrix, the ta 6 min read Java Program to Rotate Matrix Elements Given a matrix, clockwise rotate elements in it. Examples: Input 1 2 3 4 5 6 7 8 9 Output: 4 1 2 7 5 3 8 9 6 For 4*4 matrix Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. 3 min read Java Program to Convert Byte Array to Image A byte array is the array of bytes that is used to store the collection of binary data. For example, the byte array of an image stores the information of every pixel of the image. In the byte array, we can store the content of any file in binary format. We can initialize the byte array with the byte 3 min read Java Program to Convert PNG Images to JPEG PNG and JPG formats are used for image illustrations. Both the formats are used to provide good compatibilities with certain types of images like PNG works better with line drawings and icon graphics whereas JPG works well with photographs. However, both are interconvertible with respect to each oth 4 min read Java Program to Convert GIF Images to JPEG JPEG and GIF both are a type of image format to store images. JPEG uses lossy compression algorithm and the image may lose some of its data whereas GIF uses a lossless compression algorithm and no image data loss is present in GIF format. GIF images support animation and transparency. Sometimes it i 4 min read Like