Search code examples
windowspython-3.xqr-codebarcodezbar

How do I import Zbar into my Python 3.4 script?


I am pretty new to programming, and have never used Zbar before. I am trying to write a simple script that will allow me to import Zbar and use it to decode a barcode image. I already have a script set up to decode text from images that uses Pytesseract and Tesseract OCR, but I need to be able to decode barcodes as well. I have Windows 7 32 bit, and and am using Python 3.4. I have already installed Zbar and have used it from the command line successfully to decode their barcode sample. I have tried using >pip install zbar, but I keep getting the error:

"fatal error C1083: Cannot open include file: 'zbar.h': No such file or directory error: command 'C:\Program Files\Microsoft Visual Studio 10.0\VC\BIN\cl.exe' failed with exit status 2"

Getting the pytesseract OCR was painless but I have wasted a lot of time on this barcode portion of it, any help or alternatives would be much appreciated.


Solution

  • I thought I'd share my explorations and discoveries in relation to this problem, even though @ltadams21 has found a workaround.


    The short answer: You can't. The zbar module is only compatible with Python 2. The incompatibilities are at the level of the Python C API, which is deep magic beyond my ken.

    There's a bug report for this. In the meantime, use the os.system workaround that @ltadams21 posted, or maybe try zbarlight instead? (I haven't tried it myself, because it says it only reads QR codes, and I need something that reads EAN barcodes.)


    The long answer: You can follow along with these steps, which represent my best efforts to get it working, but (spoiler alert) it still won't work at the end.

    1. (Re)install zbar for Windows (zbar-0.10-setup.exe), making sure to tick the "Development Headers and Libraries" option.
    2. Download and extract the zbar Python package (zbar-0.10.tar.bz2).
    3. Open setup.py in your preferred text editor.
    4. Scroll to the bottom, find the line that starts libraries, and insert a new line below it, like so (checking that the path is the same on your system as it is on mine):
                libraries = [ 'zbar' ],
                include_dirs = ['C:/Program Files (x86)/ZBar/include'],
    
    1. Open a command prompt inside the extracted package and run python setup.py install.
    2. Observe that the new errors now relate to PyIntObject and PyInt_Type. Discover that these are Python 2-only objects.
    3. If desperate, try to re-#define them to use PyLongObject and PyLong_Type instead. Fail, because of course it's not that easy. Bang head against keyboard (gently).
    4. Give up, and use the os.system workaround that @ltadams21 posted.