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 |
|
Get familiar with the basic steps of creating a project by reviewing theAdd References and Set a Licensetutorial, before working on the图像预处理OCR - Javatutorial.
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
leadtools.jar
leadtools.codecs.jar
leadtools.document.writer.jar
leadtools.ocr.jar
For a complete list of which JAR files are required for your application, refer toFiles 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 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.
With the project created, the references added, and the license set, coding can begin.
In thePackage Explorer, open the_Main.java
class. Add the followingimport
statements to the import block at the top.
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_Main
class. Call it inside therun()
method, after the SetLicense() call.
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.
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 it
ocrPage.autoPreprocess(OcrAutoPreprocessPageCommand.DESKEW,null);
ocrPage.autoPreprocess(OcrAutoPreprocessPageCommand.INVERT,null);
ocrPage.autoPreprocess(OcrAutoPreprocessPageCommand.ROTATE,null);
// Recognize it and save it as PDF
ocrPage.recognize(null);
ocrDocument.save(pdfFileName, DocumentFormat.PDF,null);
System.out.println("File saved successfully.");
}
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.
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.