
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
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 = Imgcodecs.imread(file); //Creating an empty matrix to store the result Mat dst = new Mat(); //Creating the transformation matrix M Mat rotationMatrix = Imgproc.getRotationMatrix2D(new Point(300, 200), 30, 1); //Rotating the given image Imgproc.warpAffine(src, dst,rotationMatrix, new Size(src.cols(), src.cols())); //Writing the image Imgcodecs.imwrite("C:/opencv/rotate_output.jpg", dst); System.out.println("Image Processed"); } }
Input

Output

java_opencv
Advertisements