Read and Write Bytes in a Barcode - macOS Swift Console

This tutorial shows how to create a barcode by encoding raw binary data and how to decode that data in a macOS Swift Console application using the LEADTOOLS SDK. This can be useful when using characters from extended character sets.

Overview  
Summary This tutorial covers how to encode and decode barcode data 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

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Read and Write Bytes in a Barcode - macOS Swift Console tutorial.

Create the Project and Add LEADTOOLS References

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>\LEADTOOLS22\Bin\Xcode\Frameworks\macOS:

Edit the Leadtools-Bridging-Header.h file to add the following imports:

#import <Leadtools.Barcode/Leadtools.Barcode.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:

Note

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

Add the Barcode Encoding and Decoding Code

With the project created, the references added, and the license set, coding can begin.

Using the Project Navigator, open main.swift. Add two new functions to your existing project named CreateBarcode(data: String) -> LTRasterImage? and ReadBarcode(image: LTRasterImage) -> String. Both of these functions will be called after the SetLicense() call, with the CreateBarcode() function called first, as shown below. Also, add a print() function after the ReadBarcode() function to print out the decoded text.

SetLicense() 
 
// Create barcode and write barcode to image 
guard let barcodeImage: LTRasterImage = CreateBarcode(data: "Unicode-encoded data") else { fatalError("Failed to create barcode image.") } 
// Read barcode on image and display to console 
let decodedText: String = ReadBarcode(image: barcodeImage) 
 
// Console output 
print(decodedText) 

Add the code below to the CreateBarcode function create a new LTRasterImage object, encode data to a QR barcode and write that QR barcode to the raster image.

func CreateBarcode(data: String) -> LTRasterImage? { 
    let chars = data.data(using: .utf8) 
    do { 
        let image: LTRasterImage = try LTRasterImage.coloredImage(width: 2200, height: 3300, bitsPerPixel: 24, resolution: 300, backgroundColor: LTRasterColor.white) 
        let eng: LTBarcodeEngine = LTBarcodeEngine() 
        let qrBarcodeData: LTQRBarcodeData = LTQRBarcodeData() 
         
        qrBarcodeData.data = chars 
        try eng.writer.writeBarcode(image, data: qrBarcodeData, options: nil) 
        return image 
    } catch { 
        print(error.localizedDescription) 
        return nil 
    } 
} 

Add the following code to the ReadBarcode function to decode the QR barcode on the image and return a string containing the decoded text.

func ReadBarcode(image: LTRasterImage) -> String { 
    let eng: LTBarcodeEngine = LTBarcodeEngine() 
    let data: [LTBarcodeData] = eng.reader.readBarcodes(image, searchBounds: LeadRect.zero, maximumBarcodes: 0, symbologies: eng.reader.availableSymbologies, error: nil)! 
     
    if data.count > 0 { 
        let chars = data[0].data 
        let value: String = String(decoding: chars!, as: UTF8.self) 
         
        return value 
    } 
    return "" 
} 

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 creates a new barcode using raw byte data, then decodes that barcode and converts the raw byte data back to readable text, which is then displayed to the console.

Screenshot of the console display.

Wrap-up

This tutorial showed how to encode and decode raw byte data in a barcode in a macOS Swift Console application. Also it covered how to use the LTBarcodeEngine and LTBarcodeData 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.