本篇的教程会发布一个java代码片段,你可以用它读一本护照提取其中的数据。
记住,为了获得最精确的结果,你需要有一个清晰的图像,这样引擎在这个过程中才能获得精确的结果。

// Set your license
RasterSupport.setLicense(licenseFile, developerKey);

try{
    if(RasterSupport.getKernelExpired()) {
    System.out.println("License NOT Set Successfully");
    }
    else {
    System.out.println("License Set Successfully");
    }

RasterCodecs rasterCodecs = new RasterCodecs();
MRTDReader mrtdReader = new MRTDReader();
String stream = "PASSPORT_IMAGE.jpg";

RasterImage rasterImage = rasterCodecs.load(stream);

OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.ADVANTAGE);
ocrEngine.startup(rasterCodecs, null, null, null);

        
mrtdReader.setOcrEngine(ocrEngine);
mrtdReader.processImage(rasterImage);        
mrtdReader.setImproveResults(true);
HashMap<MRTDField, MRTDDataElement> ar = new HashMap<>();
ar = mrtdReader.getResults();

String[] string = mrtdReader.getLines();
        
for (String string2 : string) {
    System.out.println(string2);
}
                
for (Map.Entry<MRTDField, MRTDDataElement> map : ar.entrySet()) {
MRTDField key = map.getKey();
System.out.println(key);
MRTDDataElement value = map.getValue();
System.out.println(value.getReadableValue());
}
        
ocrEngine.shutdown();
}
catch(Exception e)
{
    e.printStackTrace();
    throw new Exception(e);
}

 

被识别的护照如下:

mrz_sample

 

最终输出的结果如下:

2017-07-25