Deskew Image

Deskew Image or Deskew PDF using Java Cloud SDK

A large number of raster images that we receive for work & personal use are scanned images either through digital scanner or may be a picture from mobile device. So there are fair chances that the text inside an image is not properly aligned and runs across page margin either horizontally or vertically. In simple words, the scanned images are often skewed (the paper was moved/rotated) on the scanner. Now in order to solve such issues, we opt for a technique named deskewing which is a process whereby skew is removed by rotating an image by the same amount as its skew but in the opposite direction. The Deskew feature is extremely useful in automated image processing, when the images come from a scanner.

Now in this article, we are looking into the details on how to Deskew image using Java Cloud SDK. So the deskew function works in two steps i.e. detects the angle of skew of the image and then rotates the image to correct the skew. We can apply this operation on received faxes and using the information (text) on the image, calculate the angle of the rotation, not the margins of the paper and accomplish the requirement. Nevertheless, we understand that image deskew operation can be performed on 1 bit, 8 bit, and 24 bit color images.

Deskew Image API

In order to support image We have REST based API offering the capabilities to manipulate images file in the Cloud. Now for Java applications, we have specifically developed Aspose.Imaging Cloud SDK for Java which enables you to implement image processing capabilities via Java code. Now in order to Deskew images using Java cloud SDK, the first step is to add its reference in java project by including following information in pom.xml (maven build type project).

<repositories> 
    <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>http://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>

<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-imaging-cloud</artifactId>
        <version>22.4</version>
    </dependency>
</dependencies>

The next step is to obtain the client credentials from Cloud Dashboard and if you do not have an account over Aspose Cloud Dashboard, please create a free account through valid email address. Now login using newly created account and lookup/create Client ID and Client Secret at Aspose Cloud Dashboard.

Deskew Image Online using Java

Let’s explore the details on how to deskew image using Java code snippet. For this example, we are loading TIFF image from local drive and apply deskew photo operation. The resultant file is then saved to cloud storage.

  • Firstly, create an object of ImagingApi based on personalized client credentials
  • Secondly, create a boolean variable which indicates to resize the image proportionally
  • Now read the content of source TIFF using readAllBytes(…) method and pass to byte[] array
  • The next step is to create an instance of CreateDeskewedImageRequest which requires Byte array, optional parameter for background color and the name of resultant deskew photo
  • Finally, call the method createDeskewedImage(…) to deskew image. The resultant TIFF is stored in cloud storage

Now if you want to save the resultant TIFF on local/network drive, then please try using the following code snippet.

deskew image

Deskew photo preview

The sample TIFF image used in above example can be downloaded from DeskewSampleImage.tif.

Deskew PDF using cURL Commands

We may receive a PDF file containing deskewed images so one quick solution is to extract images from PDF file and then deskew the images. Now in order to accomplish this requirement, we are going to use two Cloud APIs here i.e. Aspose.PDF Cloud to extract images from PDF file and Aspose.Imaging Cloud to deskew photos. However, before we perform any operation, first we need to first generate a JWT access token (based on client credentials) using following command.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once we have the JWT token, we need to extract PDF images using GrayscaleImage API of Aspose.PDF Cloud. The images are extracted in TIFF format and are stored in cloud storage.

curl -v -X PUT "https://api.aspose.cloud/v3.0/pdf/Resultant.pdf/pages/1/images/extract/tiff?width=0&height=0" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

Now that images are extracted from PDF file as TIFF format, now we need to call DeskewImage API of Aspose.Imaging Cloud to deskew image. Since the resultant file is returned in response stream, so we can save it to our local drive.

curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/image1.tiff/deskew?resizeProportionally=true&bkColor=White" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-o Deskewed.tiff

Conclusion

By this time, we have learned some amazing and simple steps to deskew image as well as deskew PDF using Java cloud SDK. So now we can accomplish our requirements either using Java code snippet or using the cURL commnds. At the same time, we highly encourage you to explore Product Documentation to learn more about other exciting features offered by the API.

Please note that all our Cloud SDKs are developed under MIT license so the complete source code can be downloaded from GitHub. Lastly, in case you encoutner any issues while using the API, you may consider approaching us for a quick resolution via free product support forum.

Please visit the following links to learn more about: