Detect and Extract Barcodes - Python

This tutorial shows how to create a Python application that uses the BarcodeEngine and BarcodeReader to read barcodes from an image and display their data on the console.

Overview  
Summary This tutorial covers how to use the BarcodeEngine and BarcodeReader Class in a Python 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

Before working on the Detect and Extract Barcodes - Python tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in this tutorial: Add References and Set a License

If you do not have that project, follow the steps in the relevant tutorial to create it.

This tutorial requires the following DLLs:

For a complete list of which DLLs are required for specific barcode features, 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 Reader Code

With the project created, 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.Codecs") 
from Leadtools.Codecs import * 
LibraryLoader.add_reference("Leadtools.Barcode") 
from Leadtools.Barcode import * 
from System.IO import *  
from System.Text import * 

In the main() method add the below code to load the image that contains the barcodes.

filename = r"C:\LEADTOOLS22\Resources\Images\barcode1.tif" 
codecs = RasterCodecs() 
image = codecs.Load(filename) 
read_barcode(image) 

In the Project-Name.py file, add a new method called read_barcode(image). This method will be called in the main() method as shown above. Add the below code to detect all barcodes in the image and output their data to the console.

def read_barcode(image): 
    barcode_engine_instance = BarcodeEngine() 
 
    data_array = barcode_engine_instance.Reader.ReadBarcodes(image, LeadRect.Empty, 0, None) 
 
    sb = StringBuilder() 
    sb.AppendFormat("{0} barcode(s) found", data_array.Length) 
    sb.AppendLine() 
 
    for i in range(data_array.Length): 
        data = data_array[i]     
        sb.AppendFormat("Symbology: {0}, Location: {1}, Data: {2}", data.Symbology, data.Bounds, data.Value) 
        sb.AppendLine() 
 
    print(sb.ToString()) 

Handling Streams

To load a file using memory stream add the following code into the main() method under the filename declaration:

bytes = File.ReadAllBytes(filename) 
ms = MemoryStream(bytes) 
ms.Position = 0 
image = codecs.Load(ms) 
read_barcode(image) 

Run the Project

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

If the steps were followed correctly, the application runs and outputs all relevant barcode information from the LEADTOOLS test file, barcode1.tif.

The application runs and the console detects the barcodes and displays the extracted information

Wrap-up

This tutorial showed how to read barcode information from an image and display them into the Python console using the LEADTOOLS SDK.

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.