如何将Zbar导入到Python 3.4脚本中?

时间:2021-10-25 01:51:48

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:

我对编程非常陌生,以前从未使用过Zbar。我正在尝试编写一个简单的脚本,它允许我导入Zbar并使用它来解码一个条码图像。我已经设置了一个脚本,用于从使用Pytesseract和Tesseract OCR的图像中解码文本,但我还需要能够解码条形码。我有Windows 7 32位,使用的是Python 3.4。我已经安装了Zbar,并从命令行成功地使用它来解码他们的条形码示例。我尝试过使用>pip安装zbar,但是我一直得到错误:

"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"

"致命错误C1083:无法打开包含文件:'zbar。没有这样的文件或目录错误:命令'C:\程序文件\Microsoft Visual Studio 10.0\VC\ VC\BIN\cl。exe“退出状态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.

获得pytesseract OCR是没有痛苦的,但我已经在这个条码部分浪费了很多时间,任何帮助或替代将非常感谢。

5 个解决方案

#1


1  

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

尽管@ltadams21找到了解决方法,我还是想分享我在这个问题上的探索和发现。


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.

简短的回答是:你不能。zbar模块仅与Python 2兼容。不兼容是在Python C API的级别上,这是我的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.)

这里有一个错误报告。同时,使用操作系统。系统解决@ltadams21发布的问题,或者尝试使用zbarlight ?(我自己也没试过,因为它说它只读二维码,而我需要一些能读懂条形码的东西。)


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. (重新)为Windows安装zbar (zbar-0.10 setup.exe),确保在“开发头和库”选项中勾选。
  3. Download and extract the zbar Python package (zbar-0.10.tar.bz2).
  4. 下载并提取zbar Python包(zbar-0.10.tar.bz2)。
  5. Open setup.py in your preferred text editor.
  6. 打开设置。在您的首选文本编辑器中。
  7. 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):
  8. 滚动到底部,找到启动库的那条线,并在它下面插入一条新的线,就像这样(检查您的系统上的路径是否与我系统上的路径相同):
            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. 在提取的包中打开一个命令提示符并运行python设置。py安装。
  3. Observe that the new errors now relate to PyIntObject and PyInt_Type. Discover that these are Python 2-only objects.
  4. 注意,新的错误现在与PyIntObject和PyInt_Type有关。发现这些是只针对Python 2的对象。
  5. 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).
  6. 如果不顾一切,尝试重新定义它们以使用PyLongObject和PyLong_Type代替。失败,因为这当然不容易。敲键盘(轻轻地)。
  7. Give up, and use the os.system workaround that @ltadams21 posted.
  8. 放弃,使用操作系统。@ltadams21发布的系统解决方案。

#2


2  

Forget wrestling with all of the wrappers. The easiest solution for me was to simply use

别再纠结于所有的包装了。对我来说最简单的解决办法就是简单地使用

import os

进口操作系统

os.system(r'D:\Winapps\Zbar\bin\zbarimg.exe -d d:\Winapps\Zbar\Examples \barcode.png')

os.system(r想:\ Winapps \ Zbar \ bin \ zbarimg。exe - d d:\ Winapps \ Zbar \ \ barcode.png示例”)

Worked instantly. Hope this helps anyone else struggling with that issue.

立即工作。希望这能帮助其他在这个问题上挣扎的人。

#3


2  

Seven months later you accepted the best answer, a simple solution for python3.4+ on Windows OS appeared. A ctypes-based wrapper around the zbar barcode reader, a package named pyzbar. If you are on Linux, you need to install libzbar0 but you said you are using Windows and the zbar library is included as dll with the Windows Python wheels. Today, all you need to do is launch:

7个月后,您接受了最佳答案,Windows OS上出现了一个针对python3.4+的简单解决方案。一个基于ctype的包装,围绕着zbar条形码阅读器,一个名为pyzbar的包。如果您在Linux上,您需要安装libzbar0,但是您说您正在使用Windows,并且zbar库包含在带有Windows Python*的dll中。今天,你需要做的就是启动:

pip install pyzbar

References for the package:

引用的包:

#4


1  

As of November 2016, there is the package pyzbar. To import Zbar for python3.4+ on Windows OS, all you need to do is launch

截至2016年11月,有一款名为pyzbar的套餐。要在Windows OS上为python3.4+导入Zbar,您需要做的就是启动

pip install pyzbar

This is possible because pyzbar is a ctypes-based wrapper around the zbar library that is included as dll with the Windows Python wheels.

这是可能的,因为pyzbar是一个围绕zbar库的基于ctype的包装器,它被包含在与Windows Python轮的dll中。

#5


0  

The ZBar for python says you need to have the ZBar library installed for it to work. http://zbar.sourceforge.net/download.html

python的ZBar表示需要安装ZBar库,以便其工作。http://zbar.sourceforge.net/download.html

#1


1  

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

尽管@ltadams21找到了解决方法,我还是想分享我在这个问题上的探索和发现。


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.

简短的回答是:你不能。zbar模块仅与Python 2兼容。不兼容是在Python C API的级别上,这是我的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.)

这里有一个错误报告。同时,使用操作系统。系统解决@ltadams21发布的问题,或者尝试使用zbarlight ?(我自己也没试过,因为它说它只读二维码,而我需要一些能读懂条形码的东西。)


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. (重新)为Windows安装zbar (zbar-0.10 setup.exe),确保在“开发头和库”选项中勾选。
  3. Download and extract the zbar Python package (zbar-0.10.tar.bz2).
  4. 下载并提取zbar Python包(zbar-0.10.tar.bz2)。
  5. Open setup.py in your preferred text editor.
  6. 打开设置。在您的首选文本编辑器中。
  7. 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):
  8. 滚动到底部,找到启动库的那条线,并在它下面插入一条新的线,就像这样(检查您的系统上的路径是否与我系统上的路径相同):
            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. 在提取的包中打开一个命令提示符并运行python设置。py安装。
  3. Observe that the new errors now relate to PyIntObject and PyInt_Type. Discover that these are Python 2-only objects.
  4. 注意,新的错误现在与PyIntObject和PyInt_Type有关。发现这些是只针对Python 2的对象。
  5. 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).
  6. 如果不顾一切,尝试重新定义它们以使用PyLongObject和PyLong_Type代替。失败,因为这当然不容易。敲键盘(轻轻地)。
  7. Give up, and use the os.system workaround that @ltadams21 posted.
  8. 放弃,使用操作系统。@ltadams21发布的系统解决方案。

#2


2  

Forget wrestling with all of the wrappers. The easiest solution for me was to simply use

别再纠结于所有的包装了。对我来说最简单的解决办法就是简单地使用

import os

进口操作系统

os.system(r'D:\Winapps\Zbar\bin\zbarimg.exe -d d:\Winapps\Zbar\Examples \barcode.png')

os.system(r想:\ Winapps \ Zbar \ bin \ zbarimg。exe - d d:\ Winapps \ Zbar \ \ barcode.png示例”)

Worked instantly. Hope this helps anyone else struggling with that issue.

立即工作。希望这能帮助其他在这个问题上挣扎的人。

#3


2  

Seven months later you accepted the best answer, a simple solution for python3.4+ on Windows OS appeared. A ctypes-based wrapper around the zbar barcode reader, a package named pyzbar. If you are on Linux, you need to install libzbar0 but you said you are using Windows and the zbar library is included as dll with the Windows Python wheels. Today, all you need to do is launch:

7个月后,您接受了最佳答案,Windows OS上出现了一个针对python3.4+的简单解决方案。一个基于ctype的包装,围绕着zbar条形码阅读器,一个名为pyzbar的包。如果您在Linux上,您需要安装libzbar0,但是您说您正在使用Windows,并且zbar库包含在带有Windows Python*的dll中。今天,你需要做的就是启动:

pip install pyzbar

References for the package:

引用的包:

#4


1  

As of November 2016, there is the package pyzbar. To import Zbar for python3.4+ on Windows OS, all you need to do is launch

截至2016年11月,有一款名为pyzbar的套餐。要在Windows OS上为python3.4+导入Zbar,您需要做的就是启动

pip install pyzbar

This is possible because pyzbar is a ctypes-based wrapper around the zbar library that is included as dll with the Windows Python wheels.

这是可能的,因为pyzbar是一个围绕zbar库的基于ctype的包装器,它被包含在与Windows Python轮的dll中。

#5


0  

The ZBar for python says you need to have the ZBar library installed for it to work. http://zbar.sourceforge.net/download.html

python的ZBar表示需要安装ZBar库,以便其工作。http://zbar.sourceforge.net/download.html