Load and Save OCR Zones - Java

This tutorial shows how to create a Java application that uses the LEADTOOLS SDK to load and save OCR zones.

Summary This tutorial covers how to use LEADTOOLS OCR SDK technology in a Java application.
Completion Time 30 minutes
Eclipse Project Download tutorial project (1 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 Load and Save OCR Zones - 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>\LEADTOOLS22\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:


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

Add the Load and Save OCR Zones Code

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.

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

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

public static void main(String[] args) throws IOException  
	new _Main().run(args); 
private void run(String[] args) { 
    try { 
    catch(Exception ex) { 

Add a new method to the _Main class called LoadandSaveOcrZones(). Call this method inside the run() method below the call to the SetLicense() method, as shown above. Add the below code to initialize the OcrEngine, create the OcrPage, load OCR zones from file, add a new OcrZone, and save the zones to a new OZF (OCR zones file) file.

void LoadAndSaveOcrZones() throws IOException  
    RasterCodecs codecs = new RasterCodecs(); 
    OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
    String zonesFile = "C:\\LEADTOOLS22\\Resources\\Images\\mix_omr.ozf"; // Path to OCR zones file 
    String imageFile = "C:\\LEADTOOLS22\\Resources\\Images\\mixed.tif"; // Path to image to create OcrPage 
    String zonesOutFile = "C:\\LEADTOOLS22\\Resources\\Images\\saved_mix_omr.ozf"; // Path to save OCR zones 
    // Load the TIFF file as image 
    RasterImage image = ocrEngine.getRasterCodecsInstance().load(imageFile, 1); 
    OcrPage ocrPage = ocrEngine.createPage(image, OcrImageSharingMode.NONE); 
    System.out.println(ocrPage.getZones().size() + " Zones after OcrPage creation.\n"); 
    ocrPage.loadZones(zonesFile, 1); 
    System.out.println(ocrPage.getZones().size() + " Zones after loading zones from file.\n"); 
    // Add an extra zone, this is out user-defines one 
    OcrZone zone = new OcrZone(); 
    zone.setName("Custom zone"); 
    zone.setBounds(new LeadRect(10, 10, ocrPage.getWidth() - 20, 100)); 
    System.out.println(ocrPage.getZones().size() + " Zones after adding zone manually.\n"); 
    System.out.println("Zones successfully saved to " + zonesOutFile); 
    System.out.println("Press 'Enter' to exit..."); 

Run the Project

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

If the steps were followed correctly, the application creates an OcrPage, loads OCR zones from the specified file, adds a new OcrZone to the OcrPage, and then exports the zones to a new OZF file.

Results displayed to the console.


This tutorial showed how to load and save OCR zones. It also covered how to use the OcrEngine, OcrPage, and OcrZone Java classes.

See Also

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

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