Image Skew Correction in Java

In computer graphics and image processing, image skew is an angle deviation of rectangular images. It is also known as image tilt or distortion. The term “skew” refers to the angle of an image. Image skew occurs when the visual position of elements in an image is not uniform. Skewing can result in any corners being at an angle, or even shifted so that one side is longer than the other. Such photos or scanned images can look distorted and unappealing. In this article, we will learn how to detect image skew programmatically. We will also learn how to perform image skew correction in Java.

The following topics shall be covered in this article:

  1. Java API for Image Skew Correction
  2. Detect Image Skew Angle
  3. Detect Image Skew Angle of Live Image
  4. Automatic Image Skew Correction
  5. Recognize Image Text with Automatic Skew Correction
  6. Manual Image Skew Correction
  7. Recognize Image Text with Manual Image Skew Correction

Java API for Image Skew Correction

For image skew detection and corrections, we will be using the Aspose.OCR for Java API. It allows performing OCR on scanned images, smartphone photos, screenshots, areas of an image, and scanned PDFs. The API also allows saving the recognized text results in popular document formats.

The AsposeOCR class of the API is the main class for recognizes the text from images. It provides the CalcSkewImage(string) method that calculates the skew angle of the provided source image. It also provides another CalcSkewImageFromUri(String uri) method that calculates the skew angle of an image provided by URI link. The PreprocessingFilter class is the base class for image processing commands. The RecognitionSettings class of the API provides settings for the image recognition contains elements that allow customizing the recognition process.

Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-ocr</artifactId>
    <version>22.9</version>
</dependency>

Detect Image Skew Angle in Java

We can easily detect the skew angle of a text image by following the steps given below:

  1. Firstly, create an instance of the AsposeOCR class.
  2. Call the CalcSkewImage() method. It takes the image path as an argument.
  3. Finally, show the calculated skew angle.

The following sample code shows how to calculate the skew angle of an image in Java.

Detect Image Skew Angle from Live Image URL

We can also detect the skew angle of an image from a live URL by following the steps given below:

  1. Firstly, create an instance of the AsposeOCR class.
  2. Call the CalcSkewImageFromUri() method. It takes the image path as an argument.
  3. Finally, show the calculated skew angle.

The following sample code shows how to calculate the skew angle of an image in Java.

Automatic Image Skew Correction in Java

We can automatically correct image skew correction by following the steps given below:

  1. Firstly, create an instance of the AsposeOCR class.
  2. Next, initialize an object of the PreprocessingFilter class.
  3. Then, add the AutoSkew() filter.
  4. After that, call the PreprocessImage() method to apply the filter on the source image.
  5. Finally, save the corrected image.

The following sample code shows how to apply auto skew correction preprocessing filter in Java.

Recognize Image Text with Automatic Skew Correction in Java

We can apply automatic skew correction and recognize the text by following the steps given below:

  1. Firstly, create an instance of the AsposeOCR class.
  2. Next, initialize an object of the RecognitionSettings class.
  3. Then, call the setAutoSkew with true as an argument.
  4. After that, call the RecognizePage() method with the source image and RecognitionSettings object.
  5. Finally, show the recognized text.

The following sample code shows how to apply auto skew correction and recognize the text in Java.

Manual Image Skew Correction in Java

We can define the skew angle manually by rotating the image to the specified degree by following the steps given below:

  1. Firstly, create an instance of the AsposeOCR class.
  2. Next, initialize an object of the PreprocessingFilter class.
  3. Then, add rotate angle using the Rotate() method filter.
  4. Next, create an instance of the RecognitionSettings class.
  5. After that, call the PreprocessImage() method to apply the filter on the source image.
  6. Finally, show the recognized text.

The following sample code shows how to apply manual skew correction with preprocessing filter in Java.

Recognize Image Text with Manual Image Skew Correction in Java

We can define the skew angle manually and recognize the text by following the steps given below:

  1. Firstly, create an instance of the AsposeOCR class.
  2. Next, initialize an object of the RecognitionSettings class.
  3. Then, call the setSkew() method.
  4. After that, call the RecognizePage() method with the source image and RecognitionSettings object.
  5. Finally, show the recognized text.

The following sample code shows how to apply manual skew correction and recognize the image text in Java.

Get a Free Evaluation License

You can get a free temporary license to try Aspose.OCR for Java without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • perform OCR and recognize text on images;
  • apply filters and process images;
  • use recognition settings;
  • save the corrected images using Java.

Besides image skew correction in Java, you can learn more about Aspose.OCR for Java API using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also