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.

Overview  
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>\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 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.

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

Inside the main() 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.

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

Add a new method to the _Main class called LoadandSaveOcrZones(). Call this method inside the main() 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.

Java
static void LoadAndSaveOcrZones() throws IOException  
{ 
    RasterCodecs codecs = new RasterCodecs(); 
    OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
    ocrEngine.startup(codecs,null,null,null); 
    String zonesFile = "C:\\LEADTOOLS21\\Resources\\Images\\mix_omr.ozf"; // Path to OCR zones file 
    String imageFile = "C:\\LEADTOOLS21\\Resources\\Images\\mixed.tif"; // Path to image to create OcrPage 
    String zonesOutFile = "C:\\LEADTOOLS21\\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.setZoneType(OcrZoneType.TEXT); 
    zone.setBounds(new LeadRect(10, 10, ocrPage.getWidth() - 20, 100)); 
    ocrPage.getZones().add(zone); 
 
    System.out.println(ocrPage.getZones().size() + " Zones after adding zone manually.\n"); 
 
    ocrPage.saveZones(zonesOutFile); 
    System.out.println("Zones successfully saved to " + zonesOutFile); 
    System.out.println("Press 'Enter' to exit..."); 
    System.in.read(); 
 
    ocrPage.dispose(); 
    ocrEngine.dispose(); 
    codecs.dispose(); 
} 

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.

Wrap-up

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 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.