Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using LEADTOOLS Cloud Services Web API For OCR, Barcode, and More

20 Aug 2019 1  
LEADTOOLS Cloud Services provides all the speed, accuracy, and reliability you have come to expect from LEADTOOLS Document Imaging SDKs but is neatly packaged into a simple, pay-as-you-go Web API hosted on Microsoft Azure.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

LEADTOOLS released a new product to complement its world-renowned and award-winning line of imaging SDKs: LEADTOOLS Cloud Services. This high-powered and scalable Web API gives developers a hassle-free interface for integrating advanced recognition and document conversion into any application.

This new product provides all the speed, accuracy, and reliability you have come to expect from LEADTOOLS Document Imaging SDKs but is neatly packaged into a simple, pay-as-you-go Web API hosted on Microsoft Azure. The Web API is language agnostic, so you can use LEADTOOLS Cloud Services in any programming language that has access to the Internet. The online documentation provides examples and tutorials for .NET, JavaScript, Python, PHP, and Perl. In the following example, we will be demonstrating a simple Node.js application.

Setting up Your App

The first step is to create a free LEADTOOLS Cloud Services account which includes 50 free pages for your first application. Once you create your application with an AppId of your choosing, you will be sent a private API Password that you must use in each request.

After you unzip the Node.js demo attached to this tutorial, set the values of the InternalAppId and InternalPw varaibles in router/RouteHelper.js:

var LeadtoolsCloudServicesUrl = "https://azure.leadtools.com/api/";
var InternalAppId = "LCSDemo";
var InternalPw = "Enter Application Password Here";

Now open a command prompt, browse to the extracted folder, and run npm install. Once complete, run node server.js to start the application. By default, the demo is configured to run on http://localhost:3000.

Making Service Calls

LEADTOOLS Cloud Services is very simple and affordable to use. There are no startup or licensing fees, you just pay a few pennies for each call you make to the service, depending on the number of pages the request consumes. You can purchase pre-paid packs or choose from several monthly subscription levels so you never have to worry about running out of pages.

Each service request requires the URL and authorization information. Here we are building the URL and parameters for the ExtractText method:

//The first page in the file to mark for processing
var firstPage = 1;
 
//Sending a value of -1 will indicate to the services that
//the rest of the pages in the file should be processed.
var lastPage = -1;
 
//We will be uploading the file via a URl.  Files can also
//be passed by adding a PostFile to the request.  Only 1 file will be accepted
//per request.
var fileURL =
'http://demo.leadtools.com/images/tiff/ocr1-4.tif';
var recognitionUrl = LeadtoolsCloudServicesUrl + 
   'Recognition/ExtractText?firstPage=' + firstPage + 
   '&lastPage=' + lastPage + 
   '&fileurl=' + fileURL;
request.post(getRequestOptions(recognitionUrl),
   recognitionCallback);
 
function getRequestOptions(url) {
   //Function to generate and return HTTP request options.
   var requestOptions = {
      url: url,
      //If uploading a file as multi-part content, remove
      //the Content-Length header.
      headers: {
         'Content-Length': 0
      },
      auth: {
         user: InternalAppId,
         password: InternalPw
      }
   };
   return requestOptions;
}

To receive the results, we implement a callback that will recursively check the results using the Query method using the GUID returned from the recognition or conversion method:

function recognitionCallback(error, response, body) {
   if (!error && response.statusCode === 200) {
      var guid = body;
      console.log("Unique ID returned by the Services: " + guid);
      queryServices(guid);
   }
}
 
function queryServices(guid) {
   //Function to query the status of a request.  
   //If the request has not yet finished, this function
   //will recursively call itself until the file has finished.
   var queryUrl = LeadtoolsCloudServicesUrl +
      "Query?id=" + guid;
   request.post(getRequestOptions(queryUrl), 
                async function (error, response, body) {
   
      var results = JSON.parse(body);
      if (!error && results['FileStatus'] !== 100)
      {
         console.log("File finished processing with return code: " 
            + response.statusCode);
         if (results['FileStatus'] !== 200) {
            return;
         }
 
         // SUCCESS! do something with the results
 
      } else {
         //The file has not yet finished processing.
         await function () {
            //Sleep for 5 seconds before trying again
            return new Promise(resolve => setTimeout(resolve, 5000));
         };
         queryServices(guid); //Call the method again.
      }
   });
}

And that’s it! Each method may differ on a few parameters, but the workflow is essentially the same whether you are extracting text from an image, decoding data from barcodes, parsing the form fields and MICR line on a bank check, or converting files from one format to another.

Extracting Text with OCR

Optical Character Recognition is one of the most popular and reputable technologies from LEADTOOLS. The ExtractText method provides simple recognition that returns the plain text extracted from the image. If desired, enabling the CharacterData option will return additional data for advanced processing, such as the location and bounds of each word, character, line break, and direction.

Recognizing Barcodes

LEADTOOLS Cloud Services includes two methods for extracting data from barcodes on images. Barcode recognition methods differ slightly from other methods in that they charge a page per barcode. Therefore, two methods are offered to better manage your pages. You can choose to recognize only one barcode from the image with ExtractBarcode or use ExtractAllBarcodes to process every barcode on the image.

Extracting Data from Checks

The ExtractCheck method can be used to extract data from scanned images or photographs of bank checks. It uses a combination of Magnetic Ink Character Recognition (MICR) and OCR to extract the account number, routing number, name, date, amount, and more from bank checks.

Converting Images and Documents

LEADTOOLS got its start decades ago in image file formats, and it brings that core functionality to LEADTOOLS Cloud Services as well. Developers can use the Convert method to upload a file and convert it to a raster image like JPEG or TIFF, or searchable document formats like PDF, DOC, and RTF.

Conclusion

LEADTOOLS Cloud Services brings the power of LEADTOOLS document imaging to virtually any programming environment. Its affordable per-page pricing model and Web API architecture can get your application rolling quicker than most APIs and SDKs on the market. Visit https://services.leadtools.com for more information.

Download the Full LEADTOOLS Cloud Services Example

You can download the fully functional demo which includes the features discussed above. Extract the .zip file and follow the instructions in README.txt.

Support

Need help getting this sample up and going? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team (CloudSales@leadtools.com) or call us at 704-332-5532.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here