Recognize Handwritten Text From Images With ICR - Java

This tutorial shows how to create a Java application that runs ICR to recognize handwritten text on a given image using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use LEADTOOLS Recognition SDK technology in a Java application.
Completion Time 30 minutes
Eclipse Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Recognize Handwritten Text From Images With ICR - Java tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If that project is unavailable, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by local .jar files located at <INSTALL_DIR>\LEADTOOLS21\Bin\Java.

For this project, the following references are needed:

For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Initialize OCR Engine and Recognize Hand-written Text

With the project created, the references added, and the license set, coding can begin.

Open the _Main.java class in the Project Explorer. Add the following statements to the import block at the top.

Java
// Import block at the top 
import java.io.IOException; 
import java.nio.file.*; 
 
import leadtools.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 

Inside the main() method, add the following code to set the library path to where the C DLL files are located, and load the LEADTOOLS libraries that were previously imported.

Java
public static void main(String[] args) throws IOException  
{ 
    Platform.setLibPath("C:\\LEADTOOLS21\\Bin\\CDLL\\x64"); 
    Platform.loadLibrary(LTLibrary.LEADTOOLS); 
    Platform.loadLibrary(LTLibrary.OCR); 
         
    SetLicense(); 
         
    RunICR(); 
} 

Add a new method to the _Main class called RunICR(). Call this method inside the main() method below the call to the SetLicense() method, as shown above. Add the code below to run Intelligent Character Recognition on the loaded image and export the results to a searchable PDF. For the purposes of this tutorial the sample image located in the following file path will be used: C:\LEADTOOLS21\Resources\Images\demoicr2.tif.

Java
static void RunICR() 
{ 
    String _file = "C:\\LEADTOOLS21\\Resources\\Images\\demoicr2.tif"; 
         
    OcrEngine _ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
    _ocrEngine.startup(null, null, null, "C:\\LEADTOOLS21\\Bin\\Common\\OcrLEADRuntime"); 
         
    // Create an OCR document 
    OcrDocument _ocrDocument = _ocrEngine.getDocumentManager().createDocument(); 
    ILeadStream _fileStream = new LeadFileStream(_file); 
    OcrPage _ocrPage = _ocrDocument.getPages().addPage(_fileStream, null); 
    _ocrPage.autoZone(null); 
         
    for (int i = 0; i < _ocrPage.getZones().size(); i++) 
    { 
        OcrZone _zone = _ocrPage.getZones().get(i); 
        _zone.setZoneType(OcrZoneType.ICR); 
    } 
         
    _ocrPage.recognize(null); 
 
    _ocrDocument.save("C:\\LEADTOOLS21\\Resources\\Images\\icr.pdf", DocumentFormat.PDF, null); 
 
    _ocrDocument.dispose(); 
    _ocrEngine.dispose(); 
} 

Run the Project

Run the project by pressing Ctrl + F11, or by selecting Run -> Run.

If the steps were followed correctly, the application will auto-zone the loaded image, run ICR, then output the recognized image to a searchable PDF.

Wrap-up

This tutorial showed how to run ICR on an image and output to a searchable PDF. It also covered how to use the OcrZone structure.

See Also

Help Version 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.