This tutorial shows how to process patch codes in a document and apply the associated action in a macOS Swift Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to process patch codes in a macOS Swift Console application. |
Completion Time | 20 minutes |
Visual Studio Project | Download tutorial project (5 KB) |
Platform | macOS Swift Console Application |
IDE | Xcode |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Split Document with Patch Codes - macOS Swift Console tutorial.
Start with a copy of the project created in the Add References and Set a License 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>\LEADTOOLS23\Bin\Xcode\Frameworks\macOS
:
Leadtools.Barcode.OneD.framework
Leadtools.Barcode.framework
Leadtools.Codecs.Fax.framework
Leadtools.Codecs.Png.framework
Leadtools.Codecs.Tif.framework
Leadtools.Codecs.framework
Leadtools.framework
Leadtools.ImageProcessing.Core.framework
Leadtools.ImageProcessing.Utilities.frameworks
Edit the Leadtools-Bridging-Header.h
file to add the following imports:
#import <Leadtools.Barcode/Leadtools.Barcode.h>
#import <Leadtools.Codecs/Leadtools.Codecs.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 the code below to the SetLicense()
call to load each page of the given image. For the purposes of this tutorial you can use this sample image.
SetLicense()
let inputFile: String = "/path/to/patch-codes.tif"
let codecs: LTRasterCodecs = LTRasterCodecs()
let info: LTCodecsImageInfo = try codecs.imageInformation(file: inputFile, totalPages: true)
for page in 1...info.totalPages {
let image: LTRasterImage = try codecs.load(file: inputFile, pageNumber: page)
SplitPatchDocument(image: image, page: page)
}
Add a new function named SplitPatchDocument(image: LTRasterImage, page: Int)
. Call the new function inside the for
loop, as shown above. Add the below code to run patch code detection and split the document based on the results.
func SplitPatchDocument(image: LTRasterImage, page: Int) {
// Check for patch code
let engine: LTBarcodeEngine = LTBarcodeEngine()
let barcodeReader: LTBarcodeReader = engine.reader
var error: NSError?
let symbologies: [NSNumber] = [NSNumber(value: LTBarcodeSymbology.patchCode.rawValue)]
let data: [LTBarcodeData] = barcodeReader.readBarcodes(image, searchBounds: LeadRect.zero, maximumBarcodes: 10, symbologies: symbologies, error: &error) ?? []
// If a patch code is here, save out the portion of the document appended so far
if data.count > 0 {
let codecs: LTRasterCodecs = LTRasterCodecs()
let fileName: String = String("split_Page\(page).tif")
let splitDir: String = String("/path/to/save/\(fileName)")
do {
try codecs.save(image, file: splitDir, format: LTRasterImageFormat.tif, bitsPerPixel: 0)
print("Page \(page): Patch code found! Split saved.")
} catch {
print(error.localizedDescription)
}
} else {
let codecs: LTRasterCodecs = LTRasterCodecs()
codecs.save(image, file: "/path/to/save/NonPatchCodeDocumentPages.tif", format: LTRasterImageFormat.tif, bitsPerPixel: 0, firstPage: 1, lastPage: -1, firstSavePageNumber: 1, pageMode: LTCodecsSavePageMode.append, completion: nil)
print("Page \(page): No path code found.")
}
}
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 splits the input sample document according to the barcode detection results.
This tutorial showed how to use the BarcodeData
class with the BarcodeSymbology.PatchCode
symbology.