How to rotate an image using Java



Problem Description

How to rotate an image using Java.

Solution

Following is the program to rotate an image using java.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Size;

import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class RotatingAnImage {
   public static void main(String args[]) {

      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      
      //Reading the Image from the file and storing it in to a Matrix object
      String file = "C:/opencv/logo.jpg";
      Mat src="/?originalUrl=https%3A%2F%2Fwww.tutorialspoint.com%2FImgcodecs.imread(file)%3B%2520%2520%2520%2520%2520%2520%2F%2FCreating%2520an%2520empty%2520matrix%2520to%2520store%2520the%2520result%2520%2520%2520%2520%2520%2520Mat%2520dst%2520%3D%2520new%2520Mat()%3B%2520%2520%2520%2520%2520%2520%2520%2520%2520%2F%2FCreating%2520the%2520transformation%2520matrix%2520M%2520%2520%2520%2520%2520%2520Mat%2520rotationMatrix%2520%3D%2520Imgproc.getRotationMatrix2D(new%2520Point(300%2C%2520200)%2C%252030%2C%25201)%3B%2520%2520%2520%2520%2520%2520%2F%2FRotating%2520the%2520given%2520image%2520%2520%2520%2520%2520%2520Imgproc.warpAffine(src%2C%2520dst%2CrotationMatrix%2C%2520new%2520Size(src.cols()%2C%2520src.cols()))%3B%2520%2520%2520%2520%2520%2520%2F%2FWriting%2520the%2520image%2520%2520%2520%2520%2520%2520Imgcodecs.imwrite("C:/opencv/rotate_output.jpg", dst);
      System.out.println("Image Processed");
   }
}

Input

OpenCV Copy Input

Output

Rotated Image Output
java_opencv
Advertisements