This tutorial shows how to detect and extract barcode information from an image in a macOS Swift Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to use the BarcodeReader class in a macOS Swift Console application. |
Completion Time | 30 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 Barcodes - 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>\LEADTOOLS23\Bin\Xcode\Frameworks\macOS
:
Leadtools.Barcode.OneD.framework
Leadtools.Barcode.framework
Leadtools.Codecs.Bmp.framework
Leadtools.Codecs.Cmp.framework
Leadtools.Codecs.Fax.framework
Leadtools.Codecs.Tif.framework
Leadtools.Codecs.framework
Leadtools.framework
Leadtools.ImageProcessing.Core.framework
Leadtools.ImageProcessing.Utilities.framework
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 a new function named ReadBarcode(image: LTRasterImage)
. This function takes in the RasterImage
object containing the image loaded in the LoadImage()
function. Call this new function below the call to LoadImage()
, as shown below.
SetLicense()
let codecs = LTRasterCodecs()
let inputFile = "/path/to/LEADTOOLS23/Resources/Images/barcode1.tif"
guard let image: LTRasterImage = LoadImage(inputFile: inputFile, codecs: codecs) else { fatalError("Unable to load image") }
ReadBarcode(image: image)
Note: Creation of the
LoadImage()
function is covered in the Load and Save Images tutorial.
Add the code below to the ReadBarcode()
function to detect the barcodes on the image, if any, and output the extracted information to the console.
func ReadBarcode(image: LTRasterImage) {
let bcEngine = LTBarcodeEngine()
guard let dataArray: [LTBarcodeData] = bcEngine.reader.readBarcodes(image, searchBounds: LeadRect.zero, maximumBarcodes: 0, symbologies: nil, error: nil) else { fatalError("No barcodes found!")
}
var sb = ""
sb.append("\(dataArray.count) barcode(s) found")
sb.append("\n")
for i in 0..<dataArray.count {
let data = dataArray[i]
sb.append("Symbology: \(LTBarcodeEngine.friendlyName(for: data.symbology))" +
", Location: \(data.bounds.x), \(data.bounds.y), \(data.bounds.width), \(data.bounds.height)" +
", Data: \(data.value?.replacingOccurrences(of: "/r/n", with: "") ?? "")")
sb.append("\n")
}
print(sb)
}
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 all the relevant barcode information from the given file.
This tutorial showed how to read barcode information into the console using the BarcodeEngine and BarcodeReader classes.