Detect and Extract Barcodes - macOS Swift Console

This tutorial shows how to detect and extract barcode information from an image in a macOS Swift Console application using the LEADTOOLS SDK.

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

Required Knowledge

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.

Create the Project and Add LEADTOOLS References

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:

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.

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 to Setting a Runtime License.

There are two types of runtime licenses:


Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Barcode Reader Code

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.

let codecs = LTRasterCodecs() 
let inputFile = "/path/to/LEADTOOLS22/Resources/Images/barcode1.tif" 
guard let image: LTRasterImage = LoadImage(inputFile: inputFile, codecs: codecs) else { fatalError("Unable to load image") } 
ReadBarcode(image: image) 


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:, maximumBarcodes: 0, symbologies: nil, error: nil) else { fatalError("No barcodes found!") 
    var sb = "" 
    sb.append("\(dataArray.count) barcode(s) found") 
    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: "") ?? "")") 

Run the Project

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.

Console display of extracted information.


This tutorial showed how to read barcode information into the console using the BarcodeEngine and BarcodeReader classes.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.