Read and Write Bytes in a Barcode - Python

This tutorial shows how to create a barcode by encoding and then decoding raw binary data in a Python application using the LEADTOOLS SDK. This can be useful when Barcode Symbologies use extended character sets.

Overview  
Summary This tutorial covers how to encode and decode raw binary data in barcodes in a Python Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform Python Console Application
IDE Visual Studio 2022
Runtime Target Python 3.10 or Higher
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 - Python tutorial.

Barcode Symbologies

For a visual representation of the barcode symbologies supported by LEADTOOLS, refer to Supported Barcode Symbologies.

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 .NET DLLs:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

This tutorial uses QR Barcodes. For a complete list of which DLLs are required for other barcode symbologies, refer to Barcode Support.

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:

Add the Barcode Encoding and Decoding Code

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

In the Solution Explorer, open Project-Name.py and place the following references below the "Add references to LEADTOOLS" comment

# Add references to LEADTOOLS 
from leadtools import LibraryLoader 
LibraryLoader.add_reference("Leadtools") 
from Leadtools import * 
LibraryLoader.add_reference("Leadtools.Barcode") 
from Leadtools.Barcode import * 
 
from System.Text import *  

Add two new methods named create_barcode(data) that returns a RasterImage object and read_barcode(barcode_image) that returns a string value. Call both of these methods inside the main() method, as shown below.

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS23/Support/Common/License")) 
 
    # Create barcode and write barcode to an image 
    barcode_image = create_barcode("Unicode-encoded data") 
 
    # Read barcode on image 
    decoded_text = read_barcode(barcode_image) 
 
    print(decoded_text) 

Add the below code to the create_barcode(data) method to create a new QR barcode and encode raw binary data to it.

def create_barcode(data): 
 
    chars = Encoding.Unicode.GetBytes(data) 
     
    image = RasterImage.Create(2200, 3300, 24, 300, RasterColor.White) 
     
    engine = BarcodeEngine() 
    qr_barcode_data = QRBarcodeData() 
 
    qr_barcode_data.SetData(chars) 
    engine.Writer.WriteBarcode(image, qr_barcode_data, None) 
 
    return image 

Add the code below to the read_barcode(barcode_image) method to decode the binary data from the newly created QR barcode.

def read_barcode(barcode_image): 
 
    engine = BarcodeEngine() 
    data = engine.Reader.ReadBarcodes(barcode_image, LeadRect.Empty, 0, engine.Reader.GetAvailableSymbologies()) 
     
    if (data.Length > 0): 
        chars = data[0].GetData() 
        value = Encoding.Unicode.GetString(chars) 
        return value 
 
    return "" 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application 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 on the command line.

Decoded barcode data displayed to the user.

Wrap-up

This tutorial showed how to create a barcode from raw byte data, then decode that barcode back into the raw byte data it was created from. This raw byte data can be either binary information or a string converted from a different character set. Also, we covered how to use the BarcodeEngine and BarcodeData classes.

See Also

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

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