Export OCR Results to JSON - Java

This tutorial shows how to OCR an image and export the OCR results to a JSON file in a Java application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to save OCR results to JSON in a Java application.
Completion Time 30 minutes
Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse / IntelliJ
Runtime 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 Export OCR Results to JSON - 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 local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Initialize the OcrEngine

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 com.google.gson.*; 
 
import java.io.File; 
import java.io.IOException; 
import java.nio.file.*; 
import java.util.*; 
 
import leadtools.*; 
import leadtools.document.*; 
import leadtools.ocr.*; 

Inside the run() method, add the following to set the library path to where the C DLL files are located, as well as load the LEADTOOLS libraries that were previously imported. Also, add two new String values that will represent the input file and the directory to output the JSON to. For the purposes of this tutorial, the PDF in the following file path is used: <INSTALL_DIR>\LEADTOOLS21\Resources\Images\leadtools.pdf

Java
public static void main(String[] args) throws IOException { 
	new _Main().run(args); 
} 
private void run(String[] args) { 
	try { 
		Platform.setLibPath("C:\\LEADTOOLS21\\Bin\\CDLL\\x64"); 
	    Platform.loadLibrary(LTLibrary.LEADTOOLS); 
	    Platform.loadLibrary(LTLibrary.DOCUMENT); 
	    Platform.loadLibrary(LTLibrary.DOCUMENT_WRITER); 
	    Platform.loadLibrary(LTLibrary.OCR); 
 
	    SetLicense(); 
 
	    String file = "C:\\LEADTOOLS21\\Resources\\Images\\leadtools.pdf"; 
        String outputDirectory = "C:\\LEADTOOLS21\\Resources\\Images"; 
	             
	    OcrEngine ocrEngine = InitOcrEngine(); 
	    OCRandSaveResults(ocrEngine, file, outputDirectory); 
	}  
	catch(Exception ex) { 
	    System.err.println(ex.getMessage()); 
	    ex.printStackTrace(); 
	} 
} 

Add a new method to the _Main class called InitOcrEngine(). Call this method inside the run() method below the call to the SetLicense() method and the setting of the String values, as shown above. Add the below code to return the initialized OcrEngine.

Java
OcrEngine InitOcrEngine() 
{ 
	// Initialize OCR engine 
	OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
	ocrEngine.startup(null, null, null, "C:\\LEADTOOLS21\\Bin\\Common\\OcrLEADRuntime"); 
	return ocrEngine; 
} 

Add the Text Recognition and Export to JSON Code

Add another method at the bottom of the _Main class called OCRandSaveResults(OcrEngine ocrEngine, String file, String outputDirectory). Call the newly created method in the run() method after the OcrEngine ocrEngine = InitOcrEngine(); line of code, as shown above.

Java
void OCRandSaveResults(OcrEngine ocrEngine, String file, String outputDirectory) throws IOException  
{ 
	LoadDocumentOptions options = new LoadDocumentOptions(); 
	options.setFirstPageNumber(1); 
	options.setLastPageNumber(-1); 
		 
	LEADDocument document = DocumentFactory.loadFromFile(file, options); 
		 
	document.getText().setOcrEngine(ocrEngine); 
	List<DocumentPageText> documentPageTexts = new ArrayList<DocumentPageText>(); 
		 
	for (DocumentPage page : document.getPages()) 
	{ 
		// Parse the text and build the DocumentPageText object 
		DocumentPageText pageText = page.getText(); 
		pageText.buildText(); 
		pageText.buildWords(); 
		documentPageTexts.add(pageText); 
	} 
    // Get the filename and create the output directory 
    File fileWithoutExt = new File(file.substring(0, file.lastIndexOf("."))); 
    String fileName = fileWithoutExt.getName(); 
    String outputDir = PathCombine(outputDirectory, fileName + "-results"); 
    Files.createDirectory(Path.of(outputDir)); 
    // Save the recognized words to JSON 
    Gson gson = new Gson(); 
    var json = gson.toJson(documentPageTexts); 
    var jsonPath = PathCombine(outputDir, fileName + "-words.json"); 
    Files.write(Path.of(jsonPath), json.getBytes()); 
} 

Next, add a new method to the _Main class called PathCombine(String... paths). This method will be called twice inside the OCRandSaveResults() method, as shown above. Add the below code to combine the file paths and return the corresponding String.

Java
public String PathCombine(String... paths) 
{ 
   File file = new File(paths[0]); 
 
   for (int i = 1; i < paths.length ; i++) { 
      file = new File(file, paths[i]); 
   } 
 
   return file.getPath(); 
} 

Run the Project

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

If the steps were followed correctly, the application will OCR the given document and create a JSON file containing the recognition results in the specified output directory.

Download the resulting JSON here.

Wrap-up

This tutorial showed how to run OCR on a document and export the results to JSON. Also, it covered how to use the IOcrEngine interface, along with the LEADDocument, DocumentPage and DocumentPageText classes.

See Also

Help Version 21.0.2021.9.3
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.