This tutorial shows how to create a Java application that will utilize MRTD extraction and processing using the LEADTOOLS SDK.
| Overview | |
|---|---|
| Summary | This tutorial covers how to use LEADTOOLS MRTD SDK technology in a Java application. |
| Completion Time | 30 minutes |
| Eclipse Project | Download tutorial project (1 KB) |
| Platform | Java Application |
| IDE | Eclipse |
| Development 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 andLoad and Save Imagestutorial, before working on theDetect and Extract MRTD - Java教程。
Start with a copy of the project created in theLoad and Save Images教程。如果项目是不可用,遵循the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added by local.jarfiles located at.
For this project, the following references are needed:
leadtools.jarleadtools.codecs.jarleadtools.document.writer.jarleadtools.forms.commands.jarleadtools.imageprocessing.core.jarleadtools.ocr.jarThis tutorial uses LEADTOOLS Codecs library support. 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.
有两个泰pes of runtime licenses:
Note
Adding LEADTOOLS references and setting a license are covered in more detail in theAdd References and Set a License教程。
With the project created, the references added, the license set, and the load image code added, coding can begin. The code pertaining to saving the image is not necessary for this tutorial and can be commented out.
Open the_Main.javaclass in theProject Explorer. Add the following statements to theimportblock at the top.
// Using import at the topimportjava.io.IOException;importjava.nio.file.*;importleadtools.*;importleadtools.codecs.*;importleadtools.forms.commands.*;importleadtools.ocr.*;
Inside therun()method add the following to set the library path to where the CDLL files are located, as well as load the LEADTOOLS library that was previously imported. Also, change theinputFilestring, containing the file path passed into theLoadImage()method to"C:\\LEADTOOLS22\\Resources\\Images\\mrz_sample.jpg".
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.OCR);SetLicense();RasterCodecs codecs =newRasterCodecs();String inputFile ="C:\\LEADTOOLS22\\Resources\\Images\\mrz_sample.jpg";//String outputFile = "C:\\Temp\\test.jpg";RasterImage image = LoadImage(inputFile, codecs);ExtractMRTD(image);//SaveImage(image, outputFile, codecs);}catch(Exception ex) {System.err.println(ex.getMessage());ex.printStackTrace();}}
Add a new method to the_Mainclass calledExtractMRTD(RasterImage image). Call this method inside therun()method below the call to theLoadImage()method, as shown above. The method's parameter will be theRasterImageloaded inRasterImage image = LoadImage(inputFile, codecs);. For this tutorial use thisMRTD sample image.
Add the below MRTD detection and recognition code inside the new method.
voidExtractMRTD(RasterImage image){MRTDReader mrtdReader =newMRTDReader();OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD);ocrEngine.startup(null,null,null,"C:\\LEADTOOLS22\\Bin\\Common\\OcrLEADRuntime");mrtdReader.setOcrEngine(ocrEngine);// Process ImagemrtdReader.processImage(image);// Output valuesif(mrtdReader.getErrors() == MRTDErrors.NO_ERROR.getValue()){for(var value : mrtdReader.getResults().entrySet()){System.out.println(String.format("Data Element Field: %s", value.getKey().toString()));System.out.println(String.format("Data Element Value: %s", value.getValue().getReadableValue()));System.out.println(String.format("Data Element Code : %s", value.getValue().getMrzCharacters()));System.out.println(String.format("Data Element Valid: %s", value.getValue().isValid()));System.out.println("************************************");}}}
Run the project by pressingCtrl + F11, or by selectingRun->Run.
If the steps were followed correctly, the application runs and displays the passport image's extracted MRTD information.
This tutorial showed how to load an image of a Machine Readable Passport's (MRP) page and used theMRTDReaderclass to extract its MRTD data.