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)|
|Development License||Download LEADTOOLS|
|Try it in another language|
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.
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
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
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.
With the project created, the references added, and the license set, coding can begin.
_Main.java class in the Project Explorer. Add the following statements to the
import block at the top.
// Import block at the top
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.
public static void main(String args) throws IOException
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.
static void LoadAndSaveOcrZones() throws IOException
RasterCodecs codecs = new RasterCodecs();
OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD);
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");
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.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 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.
This tutorial showed how to load and save OCR zones. It also covered how to use the
OcrZone Java classes.