Preprocess an Image for OCR - Java

This tutorial shows how to create a Java application that uses the LEADTOOLS SDK to preprocess images for OCR Recognition.

Overview
Summary This tutorial covers how to use LEADTOOLS Image Processing SDK technology in a Java application
Completion Time 30 minutes
Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
Runtime License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing theAdd References and Set a Licensetutorial, before working on the图像预处理OCR - Javatutorial.

克雷亚te the Project and Add LEADTOOLS References

Start with a copy of the project created in theAdd References and Set a Licensetutorial. If that project is unavailable, 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\LEADTOOLS22\Bin\Java

For a complete list of which JAR files are required for your application, refer toFiles 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 toSetting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS references and setting a license are covered in more detail in theAdd References and Set a Licensetutorial.

Add the Image Preprocessing and OCR Code

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

In thePackage Explorer, open the_Main.javaclass. Add the followingimportstatements to the import block at the top.

Java
importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Paths;importleadtools.*;importleadtools.codecs.*;importleadtools.document.writer.*;importleadtools.ocr.*;

Add a new method calledOCRPreprocess()to the_Mainclass. Call it inside therun()method, after the SetLicense() call.

Java
publicstaticvoidmain(String[] args)throwsIOException{new_Main().run(args);}privatevoidrun(String[] args) {try{Platform.setLibPath("C:\\LEADTOOLS22\\Bin\\CDLL\\x64");Platform.loadLibrary(LTLibrary.LEADTOOLS);Platform.loadLibrary(LTLibrary.CODECS);Platform.loadLibrary(LTLibrary.DOCUMENT_WRITER);Platform.loadLibrary(LTLibrary.OCR);SetLicense();OCRPreprocess();}catch(Exception ex) {System.err.println(ex.getMessage());ex.printStackTrace();}}

Add the code below to theOCRPreprocess()method to initialize the LEAD OCR Engine, process the specified input file, preprocess it, and output the recognition results to the specified output file in the specified format.

Java
voidOCRPreprocess(){String tifFileName ="C:\\LEADTOOLS22\\Resources\\Images\\ocr1.tif";String pdfFileName ="C:\\LEADTOOLS22\\Resources\\Images\\cleanupTIF.pdf";RasterCodecs codecs =newRasterCodecs();RasterImage image = codecs.load(tifFileName);OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD);ocrEngine.startup(newRasterCodecs(),newDocumentWriter(),null,null);OcrDocument ocrDocument = ocrEngine.getDocumentManager().createDocument();OcrPage ocrPage = ocrDocument.getPages().addPage(image,null);// Auto-preprocess itocrPage.autoPreprocess(OcrAutoPreprocessPageCommand.DESKEW,null);ocrPage.autoPreprocess(OcrAutoPreprocessPageCommand.INVERT,null);ocrPage.autoPreprocess(OcrAutoPreprocessPageCommand.ROTATE,null);// Recognize it and save it as PDFocrPage.recognize(null);ocrDocument.save(pdfFileName, DocumentFormat.PDF,null);System.out.println("File saved successfully.");}

Run the Project

Run the project by selectingRun->Run.

If the steps were followed correctly, the application should OCR the TIFF and provide a cleaned up searchable PDF document.

Wrap-up

This tutorial showed how to initialize the LEAD OCR Engine, process the specified input file, preprocess it, and output the recognition results to the specified output file in the specified format.

See Also

iOS
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2022LEAD Technologies, Inc.All Rights Reserved.