Skip to main content

Using Barcode Xpress for Linux from Python

Barcode Xpress 13.1 recently released for Linux, Java, and .NET Core, and with it came an exciting new component in the Linux version: official Python support. It has always been possible to use the ctypes module in Python or other modules built upon it to utilize native C libraries, but Barcode Xpress Linux now ships with an official Python sample script as well as a small library you can use to take the pain out of analyzing barcodes from your Python scripts. 

You can browse the included samples, located in the ReadBarcodesPython directory, for examples, but in this article we’ll go into some additional depth explaining how everything works and how to get started reading barcodes.


Including Barcode Xpress In Your Python Project

When adding Barcode Xpress to a Python script, there are two primary files you’ll need to locate and be aware of: BarcodeXpress.py, the wrapper library, and libbarcodexpress.so, the native library.

BarcodeXpress.py is a small wrapper library that utilizes the ctypes module to find, load, and call the Barcode Xpress for Linux native library. The simplest way to use it is to simply copy it into the directory containing your script or application and then include it normally.

import BarcodeXpress

When imported, BarcodeXpress.py will attempt to search for libbarcodexpress.so or either of the versioned names of the library associated with it’s release. It will first look in the same directory containing BarcodeXpress.py. If it does not find it there, it will attempt to locate the library in the recommended default location: ~/Accusoft/BarcodeXpress13-64/bin. It will always attempt to use the version of the library it was released with as it’s first choice, falling back to any version of the same major release and finally to any version at all. We recommend that you not rely on those fallbacks, though, and always use it with the version it was released with for maximum compatibility.

If you’ve installed Barcode Xpress in the recommended location then you should have no problem using the native library from there. For any other location make sure to copy one of the libbarcodexpress.so files to the folder your script is running from.


Barcode Xpress Licensing From Python

Barcode Xpress requires you to have either a paid or evaluation license before analyzing any barcodes. The simplest way to get started out of the box for a new user is to use the License Manager included with your Barcode Xpress installation to install an evaluation license on your computer. If you haven’t requested an evaluation license yet, you can do so at the main Barcode Xpress site: https://www.accusoft.com/products/barcode-xpress-collection/barcode-xpress/

For other licensing scenarios BarcodeXpress.py provides a set of functions mirroring the ones found in the native library which accept normal Python strings and integers:

BarcodeXpress.SetSolutionName("YourSolutionName")
BarcodeXpress.SetSolutionKey(key1, key2, key3, key4)
BarcodeXpress.SetOEMLicenseKey("2.0.AStringForOEMLicensingContactAccusoftSalesForMoreInformation...")


Setting Analysis Parameters

While the native Barcode Xpress library requires filling out fields in a struct to define any non-default parameters you would like to pass to the engine, the Python wrapper allows you to pass in values in a standard Python dictionary which it will then translate into a struct for you. Additionally, several classes containing static members are present in BarcodeXpress.py which correspond to the enums present in the main library.

options = {
"Orientation": BarcodeXpress.Orientation.HorizontalVerticalDiagonal,
"BarcodeTypes": [BarcodeXpress.BarcodeType.All]
}

 

For a full list of options and their values, check the full reference documentation.


Passing Images Into Barcode Xpress

There are currently two ways of passing your images into Barcode Xpress from Python: passing the path to a bmp file as a string, or passing an image loaded from disk by OpenCV:

# With an image path
results = BarcodeXpress.AnalyzeFile(“my-image.bmp”, options)


# With an OpenCV image


myImage = cv2.imread(“my-image.png”, cv2.IMREAD_GRAYSCALE)
results = BarcodeXpress.AnalyzeOpenCV(myImage, options)


Interpreting Barcode Results

Either Analyze function will return a Python list containing Barcode objects, the class for which is defined in BarcodeXpress.py. This class has members for all of the fields in a normal Barcode Xpress Result struct except they have been converted into Python friendly equivalents. For the complete list of returned properties and their types, see the complete reference documentation.

And that should be everything you need to know to detect barcodes in Python with Barcode Xpress! BarcodeXpress.py is completely open so feel free to look around inside that file to see how we’re using ctypes to give you access to the full API of Barcode Xpress for Linux. Feel free to contact us with any questions or for evaluation licensing, and good luck decoding barcodes!

Daniel Rabiega, Software Engineer II, Barcode Xpress

Daniel Rabiega joined Accusoft as a Software Engineer in 2018 where he contributes primarily to Barcode Xpress. Daniel is a graduate of Millersville University of Pennsylvania where he participated in research into Programming Language Theory and Design as well as Scientific Computing. In his free time, he works on hobby programming projects and appreciates the Florida weather.