Getting Zbarlight to work on Windows

Zbarlight is a lightweight Python wrapper for the popular ZBar barcode reader library. In this post I describe how to get it to work in Windows.

Ruvinda Dhambarage
3 min readNov 12, 2020
Photo by Markus Winkler on Unsplash

Why?

I am currently developing an application that needs to quickly decode QR codes off of a video stream. I initially started development under Ubuntu and evaluated Python modules on Pypi. It appeared that all the popular libraries were based on libzbar, which is based on the popular ZBar project. The most popular module was pyzbar and it was able to decode at 30 FPS. However their is also a zbarlight module, which uses a C extension to interface with the native libzbar-dev libs. It doesn’t doesn’t return the detected QR code’s coordinates, but it did process significantly faster, running at 100 FPS. Great!

The problems began when I tried to get it to work under Windows. The zbarlight’s C extension is POSIX based and has not been developed with Windows support in mind. The unofficial Windows instructions has not been updated in about 3 years. And there is an open issue and a long list of comments saying that it doesn’t work on Windows anymore.

I thought I’d spend 1/2 day to see whether I can figure this out. It took a full day, but I was ultimately successfully. Hopefully others might find this useful.

How to

My config is as follows:

  • Windows 10 2004, 64 bit
  • Python 3.7.9, 64 bit

Step 1: Install MinGW-W64

  • After installation completes, add bin folder to path environment variable

Step 2: Get 64-bit Zbar binaries for Windows

  • Clone https://github.com/dani4/ZBarWin64
  • Make a copy of ZBarWin64\lib\libzbar64–0.dll to the same location and rename it as libzbar.dll
  • Copy ZBarWin64\lib\libzbar64–0.dll and ZBarWin64\zbar\libiconv\dll_x64\libiconv.dll to C:\Windows\System32

Step 3: Update distutils to use mingw

  • Go to your PYTHONPATH\Lib\distutils folder. For me it’s C:\Users\user\AppData\Local\Programs\Python\Python37\Lib
  • Create file called distutils.cfg with the text

[build]

compiler = mingw32

Step 4: Compile Zbarlight

  • Open the setup.py file with an editor
  • Add include_dirs and library_dirs to the Extension section, pointing to the ZBarWin64\include and ZBarWin64\lib folders
  • Run: python.exe .\setup.py bdist_wheel
  • It should compile without any error
  • The output wheel will be there in the zbarlight\dist folder
  • Success!

Deploying

  • You can use the wheel that you built to install Zbarlight
  • Remember to copy libiconv.dll and libzbar64–0.dll to C:\Windows\System32 when you are install it on another machines

Update

You can copy the two dependency .DLLs to the src\zbarlight folder. This will include it in the wheel. So, you can omit the step to copy the DLLs to System32

--

--