ZBar 是款桌面電腦用條形碼/二維碼掃描工具
windows平台python 2.7環境編譯安裝zbar
最近一個項目需要識別二維碼,找來找去找到了zbar和zxing,中間越過無數坑,總算基本上弄明白,分享出來給大家。
一、zbar官方介紹
ZBar 是款桌面電腦用條形碼/二維碼掃描工具,支持攝像頭及圖片掃描,支持多平台,例如 iPhone,Andriod 手機,同時 ZBar封裝了二維碼掃描的 API 開發包。
ZBar 目前條碼類型有:EAN-13/UPC-A, UPC-E, EAN-8, Code 128, Code 39, Interleaved 2 of 5 and QR Code.
從上可以看出,zbar有挺多優勢,封裝了API,支持多平台,支持多種條碼類型,包括一維、二維碼。
具體主頁:http://zbar.sourceforge.net/
二、zbar的python安裝
進入https://pypi.python.org/pypi/zbar,可以看到最新版本是0.10,提供源碼以及python2.5、python2.6下的exe版本。
File | Type | Py Version | Uploaded on | Size | |
---|---|---|---|---|---|
zbar-0.10.tar.bz2 (md5, pgp) | Source | 2009-11-10 | 15KB | ||
zbar-0.10.tar.gz (md5, pgp) | Source | 2009-11-10 | 18KB | ||
zbar-0.10.win32-py2.5.exe (md5, pgp) | MS Windows installer | 2.5 | 2009-11-10 | 74KB | |
zbar-0.10.win32-py2.6.exe (md5, pgp) | MS Windows installer | 2.6 | 2009-11-10 | 205KB | |
zbar-0.10.zip (md5, pgp) | Source | 2009-11-10 | 31K | ||
結論:沒有whl,沒有python2.7的exe安裝,更不奢望64位版本之類。
1、pypi網址,找whl或者exe安裝包
百度找到非官方發布的python2.7下zbar安裝文件,https://github.com/jacobvalenta/zbar-py27-msi/blob/master/zbar-0.10.win32-py2.7_2.msi。
實測可用,這里為了說明pypi的編譯過程,不使用該方案。
2、pypi源碼安裝
2.1、配置mingw編譯環境
windows系統安裝並配置mingw編譯器,是另一個大坑,爬坑的過程不細說,簡要描述如下:
1、下載mingw 5.1.6 ,地址如下:http://d1.rsdown.cn/soft1/mingw5.1.6.zip
2、解壓后,雙擊其中的mingw.exe進行安裝,選擇常用的安裝包,例如gcc等。
3、配置mingw安裝路徑,參看:http://blog.csdn.net/wangrouyi/article/details/7454687(注意,這個說明中的mingw是按照在d:\mingw下,如果你是安裝在c盤,則要改成c:\mingw。)
右擊我的電腦,點屬性->高級->環境變量。然后: 1)、在PATH里加入D:\MinGW\bin,記得,如果里面還有其他的變量,記得要加個分號啊,分號得在英文輸入模式下輸入的。 2)、新建LIBRARY_PATH變量,如果有的話,在值中加入D:\MinGW\lib,這是標准庫的位置。 3)、新建C_INCLUDEDE_PATH變量,值設為D\MinGW\include。 4)、新建CPLUS_INCLUDE_PATH變量,值為D\MinGW\include\c++\3.4.5;D:\MinGW\include\c++\3.4.5;D:\MinGW\include\c++\3.4.5\backward;D:\MinGW\include。
4、python中配置mingw編譯器。
在C:\Python27\Lib\distutils下新建或者修改distutils.cfg文件,內容如下:
[build] compiler=mingw32
2.2、pip安裝(pip install zbar),下面開始逐項排雷。
1、pip install zbar,出現c語言編譯錯誤,沒有找到zbar.h頭文件(下圖中紅色字體部分)
C:\Users\zou>pip install zbar Collecting zbar Downloading http://mirrors.aliyun.com/pypi/packages/33/54/cc5819efc9ee7e34b60b41e1d2d4753b6dd0c26a41c9a552611f66aa106e/zbar-0.10.tar.bz2 Installing collected packages: zbar Running setup.py install for zbar ... error Complete output from command c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\joshua~1\\appdata\\local\\temp\\pip-build-pnikpa\\zbar\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record c:\users\joshua~1\appdata\local\temp\pip-2chu03-record\install-record.txt --single-version-externally-managed --compile: running install running build running build_ext building 'zbar' extension creating build creating build\temp.win32-2.7 creating build\temp.win32-2.7\Release c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall -Ic:\python27\include -Ic:\python27\PC -c zbarmodule.c -o build\temp.win32-2.7\Release\zbarmodule.o In file included from zbarmodule.c:24: zbarmodule.h:26:18: zbar.h: No such file or directory In file included from zbarmodule.c:24: zbarmodule.h:37: error: `ZBAR_ERR_NUM' undeclared here (not in a function) zbarmodule.h:67: error: syntax error before "zbar_image_t" zbarmodule.h:67: warning: no semicolon at end of struct or union zbarmodule.h:69: error: syntax error before '}' token zbarmodule.h:69: warning: type defaults to `int' in declaration of `zbarImage' zbarmodule.h:69: warning: data definition has no type or storage class
2、進入https://github.com/ZBar/ZBar/tree/master/python 目錄,查看python下的說明,發現如下文字:
To install this module type the following:: python setup.py install Dependencies ------------ This module requires the ZBar Bar Code Reader, which may be obtained from: * http://zbar.sourceforge.net/ Windows users please note: the module *will NOT load* unless the ZBar library DLL (currently libzbar-0.dll) is available in your Windows system PATH!
簡單翻譯一下:
a、python zbar安裝方法:python setup.py install
b、python zbar需要zbar reader,zbar reader可以從http://zbar.sourceforge.net/網址下載到。
c、windows平台,需要zbar dll,最新版本名字為libzbar-0.dll 。
3、進入http://zbar.sourceforge.net/download.html,下載windows下的zbar reader執行程序。
Windows Installer Note that the Windows port is in an unstable, testing phase. If you do try it out, please let us know how well it's working for you and open a support request if you encounter any problems. The Windows Installation Guide has detailed instructions for installing and getting started with ZBar on Windows.
ZBar 0.10 Windows installer The Windows installer now includes pre-built binaries of the dependencies (ImageMagick et al). Copyright, license, and source code details for these libraries may be found in the README included with the distribution. Windows binaries for the Python module are available from PyPI
具體網址:https://jaist.dl.sourceforge.net/project/zbar/zbar/0.10/zbar-0.10-setup.exe ,下載后安裝zbar reader執行程序
4、安裝zbar reader,默認路徑 C:\Program Files (x86)\ZBar 。
在C:\Program Files (x86)\ZBar\lib 目錄下,果然找到libzbar-0.lib庫文件。
在C:\Program Files (x86)\ZBar\include 目錄下,果然找到zbar.h頭文件。
5、從https://pypi.python.org/pypi/zbar下載zbar.0.1.0.zip源碼文件,解壓到d:\src目錄,修改其中的setup.py文件,指向include、lib目錄。
ext_modules = [ Extension('zbar', [ 'zbarmodule.c', 'enum.c', 'exception.c', 'symbol.c', 'symbolset.c', 'symboliter.c', 'image.c', 'processor.c', 'imagescanner.c', 'decoder.c', 'scanner.c', ],
library_dirs = ['C:\Program Files (x86)\ZBar\lib' ] ,
libraries= ['libzbar-0'],
include_dirs = ['C:\Program Files (x86)\ZBar\include'], ),
6、手工執行執行安裝。
d:\src\>python setup.py install
running install
running build
running build_ext
building 'zbar' extension
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c zbarmodule.c -o build\temp.win32-2.7\Release\zbarmodule.o
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c enum.c -o build\temp.win32-2.7\Release\enum.o
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c exception.c -o build\temp.win32-2.7\Release\exception.o
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c symbol.c -o build\temp.win32-2.7\Release\symbol.o
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c symbolset.c -o build\temp.win32-2.7\Release\symbolset.o
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c symboliter.c -o build\temp.win32-2.7\Release\symboliter.o
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c image.c -o build\temp.win32-2.7\Release\image.o
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c processor.c -o build\temp.win32-2.7\Release\processor.o
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c imagescanner.c -o build\temp.win32-2.7\Release\imagescanner.o
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c decoder.c -o build\temp.win32-2.7\Release\decoder.o
c:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall "-IC:\Program Files (x86)\ZBar\include" -Ic:\python27\include -Ic:\python27\PC -c scanner.c -o build\temp.win32-2.7\Release\scanner.o
writing build\temp.win32-2.7\Release\zbar.def
c:\mingw\bin\gcc.exe -mno-cygwin -shared -s build\temp.win32-2.7\Release\zbarmodule.o build\temp.win32-2.7\Release\enum.o build\temp.win32-2.7\Release\exception.o build\temp.win32-2.7\Release\symbol.o
build\temp.win32-2.7\Release\symbolset.o build\temp.win32-2.7\Release\symboliter.o build\temp.win32-2.7\Release\image.o build\temp.win32-2.7\Release\processor.o build\temp.win32-2.7\Release\imagescanner.o
build\temp.win32-2.7\Release\decoder.o build\temp.win32-2.7\Release\scanner.o build\temp.win32-2.7\Release\zbar.def
"-LC:\Program Files (x86)\ZBar\lib" -Lc:\python27\libs -Lc:\python27\PCbuild -Lc:\python27\PC\VS9.0 -llibzbar-0 -lpython27 -lmsvcr90 -o build\lib.win32-2.7\zbar.pyd
running install_lib
copying build\lib.win32-2.7\zbar.pyd -> c:\python27\Lib\site-packages
running install_egg_info
Writing c:\python27\Lib\site-packages\zbar-0.10-py2.7.egg-info
注意,上文中紅色字體部分,Include目錄增加了C:\Program Files (x86)\ZBar\include,Lib目錄增加了C:\Program Files (x86)\ZBar\lib目錄 。
至於 libzbar-0 是因為lib目錄中是已經有lib文件。