Rotate an Image in Java

In this tutorial, we will learn how to rotate an image using Java. First of all, we will download one image and will save it in any folder of our choice on our computer. Then using a java code we will create a new pdf file of any name of our choice and open our downloaded image rotated by some degrees in that pdf file. (we can set degrees in our code and change them according to our need).

Before starting to write our code we need a jar file ie. “itextpdf-5.1.0.jar”

Link to download jar file::http://www.java2s.com/Code/Jar/i/Downloaditextpdf510jar.htm

Where to write the code

We can generate this code using any IDE ie. Notepad, Notepad++, Netbeans, Eclipse, etc. But we will use Netbeans as we require jar file ie. itextpdf-5.1.0.jar and using Eclipse or Netbeans it becomes very easy for us to deal with jar-files as we can directly add them to Libraries by just right-clicking on Libraries and clicking Add Jar/Project. But this is not so in case of Notepad and Notepad++, we need to add jar-files to our classpath to use the required jar-files in case of Notepad and Notepad++.

And for writing java codes Netbeans and Eclipse are the best IDE’s. Because most of the code is generated automatically.

Source code to Rotate Image Using Java

package JavaApplication29;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class JavaApplication29 {
  public static void main(String[] args) {
    try {
      
      Document documentobj = new Document(PageSize.A4, 20, 20, 20, 20);

      PdfWriter.getInstance(documentobj, new FileOutputStream("output.pdf"));
      documentobj.open();

      Image imageobj = Image.getInstance("C:\\Users\\lenovo\\Desktop\\kamal\\cartoon.jpg");
      imageobj.scaleToFit(200f, 200f);
      imageobj.setRotationDegrees(180);

      documentobj.add(imageobj);
      documentobj.close();

      System.out.println("Task completed");
    } catch (Exception e) {
      System.out.println("Exception occured");
    }
  }
}

In the above code, JavaApplication29 is the name of the package.

Further, we imported all the required packages required in our code.

We can only import these packages by adding itextpdf.5.1.0.jar file to Libraries of our project.

JavaApplication29 is the name of our class.

In the main method of our class, we are using a try-catch block to handle all the Exceptions. Exceptions may disturb the flow of the program.

Uses of itextpdf-5.1.0.jar

Document Class:: We use the Document class of the iText library to represent pdf documents. We created an instance of Document class ie.documentobj which we can use further to open and close pdf file. To use Document Class we need itextpdf-5.1.0.jar.

PdfWriter:: PdfWriter supports the generation of PDF, XML, RTF files. In this code, we are using PdfWriter for the generation of PDF. fileOutputStream() is used for file handling in java. Output.pdf is the name of the pdf file in which we want to display our result.

“C:\\Users\\lenovo\\Desktop\\kamal\\cartoon.jpg” is the path of our image that we downloaded. Figure on which we want to perform rotation.

Functions

imageobj is the object of Image class.imageobj.scaleToFit() helps us to set the size of the image in the output file.

We use imageobj.setRotationDegrees() to rotate the image. We can use the degree of rotation as the parameter of our method.

The documentobj.open() and documentobj.close() helps us to open and close our pdf files.

Original image::

Rotate an Image in Java

Output::(180 degree rotation)

Rotate an Image in Java

Also read:

Leave a Reply