OCR an Image and Display the Results - Java

This tutorial shows how to use the LEAD OCR Engine to OCR an image and display the recognized words to the user in a Java application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to OCR an image and display the recognized text in a Java application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
Development License Download LEADTOOLS

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 OCR an Image and Display the Results 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 you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. The following JAR files are needed for this tutorial:

The JAR files are located at <INSTALL_DIR>\LEADTOOLS23\Bin\Java

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:

Add the OCR Code

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

In the Package Explorer, open the _Main.java class. Add the following import statements to the import block at the top.

Java
import java.io.IOException; 
import java.nio.file.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.ocr.*; 

Add the global variables below to the _Main class.

Java
private OcrEngine ocrEngine; 
private RasterCodecs codecs; 

Add the code below to the run() method to initialize the OcrEngine.

Java
private void run(String[] args) throws IOException { 
	Platform.setLibPath("C:\\LEADTOOLS23\\Bin\\CDLL\\x64"); 
	Platform.loadLibrary(LTLibrary.LEADTOOLS); 
	Platform.loadLibrary(LTLibrary.CODECS); 
	Platform.loadLibrary(LTLibrary.DOCUMENT_WRITER);  
	Platform.loadLibrary(LTLibrary.OCR);  
				  
	SetLicense(); 
			 
	codecs = new RasterCodecs();  
	ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD);  
	ocrEngine.startup(codecs,null,null,"C:\\LEADTOOLS23\\Bin\\Common\\OcrLEADRuntime"); 
		     
	OcrPage page = LoadImage(); 
	StringBuilder sb = OcrImageText(page); 
		     
	System.out.println(sb); 
} 

Add two new methods to the _Main class named LoadImage and OcrImageText. These methods will be called inside the run() method, as seen above. Add the below code to the LoadImage method to load in the image and create a new instance of OcrPage.

Java
private OcrPage LoadImage() 
{ 
	String inputFile = "C:\\LEADTOOLS23\\Resources\\Images\\ocr1.tif"; 
		 
	RasterImage image = codecs.load(inputFile); 
	OcrPage ocrPage = ocrEngine.createPage(image, OcrImageSharingMode.AUTO_DISPOSE); 
		 
	return ocrPage; 
} 

Add the code below to the OcrImageText method to OCR the image and create a StringBuilder object containing the results.

Java
private StringBuilder OcrImageText(OcrPage page) 
{ 
	StringBuilder sb = new StringBuilder(); 
		 
	page.recognize(null); 
		 
	for(int i = 0; i < page.getZones().size(); i++) 
	{ 
		String zoneText = page.getText(i); 
		sb.append(zoneText); 
		sb.append("\n"); 
	} 
	return sb; 
} 

Run the Project

Run the project by selecting Run -> Run.

If the steps were followed correctly, the application will OCR the given image and display the results in the console.

Wrap-up

This tutorial showed how to initialize the LEAD OCR Engine, OCR an image, gather the recognized text, and display it.

See Also

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


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