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 the Add References and Set a License and Load and Save Images tutorials, before working on the Detect and Extract MRTD - macOS Swift Console tutorial.
Start with a copy of the project created in the Load and Save Images tutorial. 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 <INSTALL_DIR>\LEADTOOLS22\Bin\Xcode\Frameworks\macOS
:
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 the Leadtools-Bridging-Header.h
file to add the following imports:
#import <Leadtools.Codecs/Leadtools.Codecs.h>
#import <Leadtools.Forms.Commands/Leadtools.Forms.Commands.h>
#import <Leadtools.Ocr/Leadtools.Ocr.h>
For a complete list of which files are required for your application, refer to Files 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 to Setting a Runtime License.
There are two types of runtime licenses:
Note
Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, and the license set, coding can begin.
Using the Project Navigator, open main.swift
. Add a new function named ExtractMRTD(image: LTRasterImage)
. Call this new function below the call to LoadImage
, which returns the RasterImage
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 the Load and Save Images tutorial.
Add the code below to the ExtractMRTD
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 {
try ocrEngine.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 {
try mrtdReader.processImage(image)
} catch {
print("Failed to process Image.")
}
// Output values
if mrtdReader.errors == LTMRTDErrors(rawValue: 0) {
for result in mrtdReader.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 selecting Product -> Clean Build Folder or Shift + Command + K.
Run the project by selecting Product -> Run or Command + 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 the MRTDReader
class and the LTOcrEngine
interface.