Auto Recognize and Process a Form - Java

This tutorial shows how to create a master set of forms, and then automatically recognize and process a form using the LEADTOOLS High-Level Form Interface in a Java application.

Overview  
Summary This tutorial covers how to recognize and process a form with LEADTOOLS High-Level Form Interface in a Java application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse / IntelliJ
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 Auto Recognize and Process a Form - Java tutorial.

Create the Project and Add 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 references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Form Recognize and Process Code

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

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

Java
import java.io.*; 
import java.nio.file.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.document.writer.*; 
import leadtools.forms.auto.*; 
import leadtools.forms.processing.*; 
import leadtools.ocr.*; 

Add the following global variables to the _Main class.

Java
private OcrEngine ocrEngine = null; 
private AutoFormsEngine autoEngine = null; 

Modify the main() method, as shown below to load the LEADTOOLS libraries, set the lib path to where the CDLLs reside, and make the calls to the necessary methods. Both autoEngine and ocrEngine will need to be disposed after they are used in order to properly free those resources.

Java
public static void main(String[] args) 
{ 
    Platform.setLibPath("C:\\LEADTOOLS21\\Bin\\CDLL\\x64"); 
         
    Platform.loadLibrary(LTLibrary.LEADTOOLS); 
    Platform.loadLibrary(LTLibrary.CODECS); 
    Platform.loadLibrary(LTLibrary.OCR); 
         
    String formToRecognize = "C:\\LEADTOOLS21\\Resources\\Images\\Forms\\Forms to be Recognized\\OCR\\W9_OCR_Filled.tif"; 
         
    SetLicense(); 
    InitFormsEngines(); 
    RecognizeAndProcessForm(formToRecognize); 
 
    if (autoEngine!=null) 
        autoEngine.dispose(); 
    if (ocrEngine != null) { 
        ocrEngine.shutdown(); 
        ocrEngine.dispose(); 
    } 
} 

Add a new method to the _Main class named InitFormsEngines(). Call this method inside the main() method below the call to the SetLicense() method, as shown above. Add the code to the InitFormsEngines() method to initialize the OcrEngine, DiskMasterFormsRepository, and AutoFormsEngine.

Java
private void InitFormsEngines() { 
    try { 
        ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
        ocrEngine.startup(new RasterCodecs(), new DocumentWriter(), null, null); 
         
        DiskMasterFormsRepository formsRepository = new DiskMasterFormsRepository(ocrEngine.getRasterCodecsInstance(), 
                "C:\\LEADTOOLS21\\Resources\\Images\\Forms\\MasterForm Sets\\OCR"); 
        autoEngine = new AutoFormsEngine(formsRepository, ocrEngine, null); 
         
        System.out.println("Engines initialized successfully!"); 
    } catch (Exception ex) { 
        System.err.println(ex.getMessage()); 
        ex.printStackTrace(); 
    } 
} 

Create a new method in the _Main class named RecognizeAndProcessForm(String formToRecognize). Call this method inside the main() method below the call to the InitFormsEngines(), passing in the formToRecognize string value as shown above. For the purposes of this tutorial, the form from the following directory is used: C:\LEADTOOLS21\Resources\Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif

Add the code below to the RecognizeAndProcessForm() method to run auto recognition on the form inside the given file path.

Java
private void RecognizeAndProcessForm(String formToRecognize) { 
    try { 
        String resultMessage = "Form not recognized"; 
         
        AutoFormsRunResult runResult = autoEngine.run(formToRecognize, null); 
        if (runResult != null) { 
            resultMessage = String.format("This form has been recognized as a %s with %d confidence.", 
                        runResult.getRecognitionResult().getMasterForm().getName(), 
                        runResult.getRecognitionResult().getResult().getConfidence()); 
        } 
         
        System.out.println("Recognition results:"); 
        System.out.println(resultMessage); 
        System.out.println("========================================================"); 
        ShowProcessedResults(runResult); 
    } catch (Exception ex) { 
        System.err.println(ex.getMessage()); 
        ex.printStackTrace(); 
    } 
} 

Add a new method to the _Main class named ShowProcessedResults(AutoFormsRunResult runResult). This method will be called at the bottom of the RecognizeAndProcessForm() method, as shown above, passing in the runResult gathered from the run() method inside the AutoFormsRunResult class. Add the below code to the ShowProcessedResults() method to process the form and output the results.

Java
private void ShowProcessedResults(AutoFormsRunResult runResult) { 
    if (runResult == null) 
        return; 
     
    String resultsMessage = ""; 
    try { 
        for (FormPage formPage : runResult.getFormFields()) { 
            for (FormField field : formPage) { 
                if (field != null) { 
                    TextFormFieldResult textResult = (TextFormFieldResult) field.getResult(); 
                    resultsMessage = String.format("%s%s = %s%n", 
                            resultsMessage, 
                            field.getName(), 
                            textResult.getText()); 
                } 
            } 
        } 
    } catch (Exception ex) { 
        System.err.println(ex.getMessage()); 
        ex.printStackTrace(); 
    } 
     
    System.out.println("Field Processing Results"); 
    System.out.println(resultsMessage!=null && !resultsMessage.isEmpty()  
            ? resultsMessage  
            : "No fields were processed"); 
} 

Run the Project

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

If the steps were followed correctly, the application loads the form image, recognizes the form from a set of master forms, and processes the form fields outputting the results to the console.

Wrap-Up

This tutorial showed how to use the LEADTOOLS toolkit in a Java application to identify an image as a specific form, and extract the text from the form fields. We also covered how to use the OcrEngine, AutoFormsEngine, DiskMasterFormsRepository, AutoFormsRunResult, FormPage, and FormField classes.

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.