Detect and Extract MRTD - macOS Swift Console

This tutorial shows how to utilize MRTD extraction and processing in a macOS Swift Console application using the LEADTOOLS SDK.

Overview
Summary This tutorial covers how to use LEADTOOLS MRTD SDK technology in a macOS Swift Console application.
Completion Time 20 minutes
Project Download tutorial project (5 KB)
Platform macOS Swift Console Application
IDE Xcode
Runtime License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project and loading an image by reviewing theAdd References and Set a LicenseandLoad and Save Imagestutorials, before working on theDetect and Extract MRTD - macOS Swift Consoletutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in theLoad and Save Imagestutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. This tutorial requires the following Framework references located at\LEADTOOLS22\Bin\Xcode\Frameworks\macOS:

Edit theLeadtools-Bridging-Header.hfile to add the following imports:

#import #import #import 

For a complete list of which files are required for your application, refer toFiles to be Included in your 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 MRTD Reader and Processing Code

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

Using theProject Navigator, openmain.swift. Add a new function namedExtractMRTD(image: LTRasterImage). Call this new function below the call toLoadImage, which returns theRasterImageobject to run passport recognition on, as shown below.

SetLicense()let codecs = LTRasterCodecs()let inputFile ="/path/to/LEADTOOLS22/Resources/Images/mrz_sample.jpg"guard let image = LoadImage(inputFile: inputFile, codecs: codecs)else{ fatalError("Image failed to load.") }ExtractMRTD(image: image)

Note

Creation of theLoadImage()function is covered in theLoad and Save Imagestutorial.

Add the code below to theExtractMRTDfunction to detect the MRZ (Machine Readable Zone) and process the results, outputting to the console.

func ExtractMRTD(image: LTRasterImage) {let mrtdReader = LTMRTDReader()let ocrEngine = LTOcrEngineManager.createEngine(LTOcrEngineType.LEAD)do{tryocrEngine.startup(rasterCodecs: nil, documentWriter: nil, workDirectory: nil, engineDirectory:"/path/to/LEADTOOLS22/Bin/Common/OcrLEADRuntime")}catch{print("Failed to start OCR Engine.")}mrtdReader.ocrEngine = ocrEngine// Process Imagedo{trymrtdReader.processImage(image)}catch{print("Failed to process Image.")}// Output valuesifmrtdReader.errors == LTMRTDErrors(rawValue: 0) {forresultinmrtdReader.results! {print("Data Element Field: \(result.key)")print("Data Element Value: \(result.value.readableValue ?? "")")print("Data Element Code : \(result.value.mrzCharacters ?? "")")print("Data Element Valid: \(result.value.isValid)")print("***********************")}}}

Run the Project

Clean the project to clear any errors by selectingProduct -> Clean Build FolderorShift + Command + K.

Run the project by selectingProduct -> RunorCommand + R.

If the steps were followed correctly, the application runs and the console outputs the processing passport information gathered from the MRZ.

Console display of extracted information.

Wrap-Up

This tutorial showed how to load an image and run MRTD recognition. Also, it covered how to use theMRTDReaderclass and theLTOcrEngineinterface.

See Also

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