最近一个项目需要识别二维码,找来找去找到了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~\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::
zbarmodule.h:26:18: zbar.h: No such file or directory
In file included from zbarmodule.c:24
:
zbarmodule.h:: error: `ZBAR_ERR_NUM' undeclared here (not in a function)
zbarmodule.h:: error: syntax error before "zbar_image_t"
zbarmodule.h:: warning: no semicolon at end of struct or union
zbarmodule.h:: error: syntax error before '}' token
zbarmodule.h:: warning: type defaults to `int' in declaration of `zbarImage'
zbarmodule.h:: 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文件。

三、总结

总结上述排雷的过程,无非是对c语言编译器的一点点基础,其次仔细查看错误,仔细查找说明文件。

windows平台python 2.7环境编译安装zbar的更多相关文章

  1. windows平台python 2.7环境编译安装zbarlight

    类似于前一篇博文,http://www.cnblogs.com/zhongtang/p/7148082.html中描述的情况. 编译zbarlight同样出现问题,简要处理步骤如下: 1.到https ...

  2. Windows平台Python Pyramid实战从入门到进阶:第一个服务

    Pyramid是比较流行的Python Web 框架,比较灵活,功能也很强大.最近项目上用到,便打算学习一下.网上教程比较少,而且很多都是针对linux平台的,我是windows土著所以对那些linu ...

  3. windows下python web开发环境的搭建

    windows下python web开发环境: python2.7,django1.5.1,eclipse4.3.2,pydev3.4.1 一. python环境安装 https://www.pyth ...

  4. Windows系统在Python2.7环境下安装numpy, matplotlib, scipy - Lichanghao Blog

    numpy, matplotlib, scipy三个包是科学计算和绘图的利器.安装它们既可以在网上下载exe安装包,也可以用python内置的包管理工具来下载安装,后者较为方便. 这几天做美赛要用到, ...

  5. Centos7之LNMP环境编译安装

    Centos7之LNMP环境编译安装 一.系统环境准备 注:安装时间过长,只做参考!!!1.系统信息 [root@localhost ~]# uname -r 3.10.0-957.el7.x86_6 ...

  6. 编译安装 zbar 时两次 make 带来的惊喜

    为了装 php 的条形码扩展模块 php-zbarcode,先装了一天的 ImageMagick 和 zbar.也许和我装的 Ubuntu 17.10 的有版本兼容问题吧,总之什么毛病都有,apt 不 ...

  7. Windows平台 python环境配置

    下载python:https://www.python.org/downloads/windows/,一般就下载 executable installer,x86 表示是 32 位机子的,x86-64 ...

  8. windows平台下基于VisualStudio的Clang安装和配置

    LLVM 是一个开源的编译器架构,它已经被成功应用到多个应用领域.Clang是 LLVM 的一个编译器前端,它目前支持 C, C++, Objective-C 以及 Objective-C++ 等编程 ...

  9. cocos2d-x 2.1.4学习笔记01:windows平台搭建cocos2d-x开发环境

    cocos2d-x的大致开发流程是,首先使用win32版进行代码编写并完成游戏,然后将代码迁移到对应的开发环境上进行交叉编译完成游戏打包,如iphone上是mac+xcode,android是ecli ...

随机推荐

  1. Images as x-axis labels

    Open-source software is awesome. If I found that a piece of closed-source software was missing a fea ...

  2. openfire muc 移除成员

    muc添加成员到数据库可参考 将Openfire中的MUC改造成类似QQ群一样的永久群 插件 插件是一位大神参考第一篇文章改进后编写的插件,进测试可以直接使用. ------------------- ...

  3. 你知道现在有一种新的OCR技术叫“移动端车牌识别”吗?

    核心内容:车牌识别.OCR识别技术.移动端车牌识别.手机端车牌识别.安卓车牌识别.Android车牌识别.iOS车牌识别 一.移动端车牌识别OCR技术研发原理 移动端车牌识别是基于OCR识别的一种应用 ...

  4. vue实例的几个概念

    1.构造器 vue应用都是通过vue构造函数创建实例来启动的,在创建vue实例时需要传入一个options对象,该对象可以包含数据.模板.挂在元素.方法.生命周期钩子等选项: var vm = new ...

  5. 点评阿里JAVA手册之MySQL数据库 (建表规约、索引规约、SQL语句、ORM映射)

    下载原版阿里JAVA开发手册  [阿里巴巴Java开发手册v1.2.0] 本文主要是对照阿里开发手册,注释自己在工作中运用情况. 本文内容:MySQL数据库 (建表规约.索引规约.SQL语句.ORM映 ...

  6. 阿里云 Centos7.3安装mysql5.7.18 rpm安装

    卸载MariaDB CentOS7默认安装MariaDB而不是MySQL,而且yum服务器上也移除了MySQL相关的软件包.因为MariaDB和MySQL可能会冲突,故先卸载MariaDB. 1.安装 ...

  7. java小白进阶安卓第一天

  8. 记一次调试串口设备Bug的经历

    最近花了差不多1天的时间在折腾一个Bug,该Bug的表象如下: 这个Bug还特别独特,在开发电脑中无提示,在终端用户那里每次使用软件的时候都报这个.仔细思考了一下最近在源码中新添加的功能,没发现有啥特 ...

  9. 优化tomcat配置(从内存、并发、缓存4个方面)优化

    Tomcat内存优化 Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catalina.sh 中设置 java_OPTS 参数. JAVA_OPTS参数 ...

  10. JAVA web.xml中引用多个XML

    web.xml里加<context-param><param-name>contextConfigLocation</param-name><param-va ...