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 |
|
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.
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
:
Leadtools.framework
Leadtools.Codecs.framework
Leadtools.Codecs.Cmp.framework
Leadtools.Document.Writer.framework
Leadtools.Drawing.framework
Leadtools.Forms.Commands.framework
Leadtools.ImageProcessing.Color.framework
Leadtools.ImageProcessing.Core.framework
Leadtools.ImageProcessing.Effects.framework
Leadtools.ImageProcessing.Utilities.framework
Leadtools.Ocr.framework
Leadtools.Svg.framework
Edit theLeadtools-Bridging-Header.h
file 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.
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.
Using theProject Navigator, openmain.swift
. Add a new function namedExtractMRTD(image: LTRasterImage)
. Call this new function below the call toLoadImage
, which returns theRasterImage
object 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 the
LoadImage()
function is covered in theLoad and Save Imagestutorial.
Add the code below to theExtractMRTD
function 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 Image
do{
trymrtdReader.processImage(image)
}catch{
print("Failed to process Image.")
}
// Output values
ifmrtdReader.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("***********************")
}
}
}
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.
This tutorial showed how to load an image and run MRTD recognition. Also, it covered how to use theMRTDReader
class and theLTOcrEngine
interface.