最近一个项目需要识别二维码,找来找去找到了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. 右击我的电脑,点属性->高级->环境变量。然后:
  2. 1)、在PATH里加入D:\MinGW\bin,记得,如果里面还有其他的变量,记得要加个分号啊,分号得在英文输入模式下输入的。
  3. 2)、新建LIBRARY_PATH变量,如果有的话,在值中加入D:\MinGW\lib,这是标准库的位置。
  4. 3)、新建C_INCLUDEDE_PATH变量,值设为D\MinGW\include
  5. 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文件,内容如下:

  1. [build]
  2. compiler=mingw32

2.2、pip安装(pip install zbar),下面开始逐项排雷。

1、pip install zbar,出现c语言编译错误,没有找到zbar.h头文件(下图中红色字体部分)

  1. C:\Users\zou>pip install zbar
  2. Collecting zbar
  3. Downloading http://mirrors.aliyun.com/pypi/packages/33/54/cc5819efc9ee7e34b60b41e1d2d4753b6dd0c26a41c9a552611f66aa106e/zbar-0.10.tar.bz2
  4. Installing collected packages: zbar
  5. Running setup.py install for zbar ... error
  6. 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:
  7. running install
  8. running build
  9. running build_ext
  10. building 'zbar' extension
  11. creating build
  12. creating build\temp.win32-2.7
  13. creating build\temp.win32-2.7\Release
  14. 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
  15. In file included from zbarmodule.c::
  16. zbarmodule.h:26:18: zbar.h: No such file or directory
  17. In file included from zbarmodule.c:24:
  18. zbarmodule.h:: error: `ZBAR_ERR_NUM' undeclared here (not in a function)
  19. zbarmodule.h:: error: syntax error before "zbar_image_t"
  20. zbarmodule.h:: warning: no semicolon at end of struct or union
  21. zbarmodule.h:: error: syntax error before '}' token
  22. zbarmodule.h:: warning: type defaults to `int' in declaration of `zbarImage'
  23. zbarmodule.h:: warning: data definition has no type or storage class

2、进入https://github.com/ZBar/ZBar/tree/master/python 目录,查看python下的说明,发现如下文字:

  1. To install this module type the following::
  2.  
  3. python setup.py install
  4.  
  5. Dependencies
  6. ------------
  7. This module requires the ZBar Bar Code Reader, which may be obtained
  8. from:
  9.  
  10. * http://zbar.sourceforge.net/
  11.  
  12. Windows users please note: the module *will NOT load* unless the ZBar
  13. library DLL (currently libzbar-0.dll) is available in your Windows system
  14. 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执行程序。

  1. Windows Installer
  2.  
  3. 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.
  4.  
  5. The Windows Installation Guide has detailed instructions for installing and getting started with ZBar on Windows.
  6.  
  7. ZBar 0.10 Windows installer
  8.  
  9. 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.
  10.  
  11. 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目录。

  1. ext_modules = [
  2. Extension('zbar', [
  3. 'zbarmodule.c',
  4. 'enum.c',
  5. 'exception.c',
  6. 'symbol.c',
  7. 'symbolset.c',
  8. 'symboliter.c',
  9. 'image.c',
  10. 'processor.c',
  11. 'imagescanner.c',
  12. 'decoder.c',
  13. 'scanner.c',
  14. ],
             library_dirs = ['C:\Program Files (x86)\ZBar\lib' ] ,
                libraries= ['libzbar-0'],
                include_dirs = ['C:\Program Files (x86)\ZBar\include'],
  15. ),

6、手工执行执行安装。

  1. 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. HourRank 19

    https://www.hackerrank.com/contests/hourrank-19/challenges 第一题略. 第二题是nim博弈,问删掉一个区间的石子,使得先手败的方案有几种,明显 ...

  2. 前端小课堂 js:函数的创建方式及区别

    js 函数的创建大体有这几种方式: -1-函数表达式(函数字面量): 说白了就是把一个函数赋值给了一个变量. var fun1 = function(index){ alert(index); } f ...

  3. 谈一谈Java8的函数式编程 (三) --几道关于流的练习题

    为什么要有练习题?    所谓学而不思则罔,思而不学则殆,在系列第一篇就表明我认为写博客,既是分享,也是自己的巩固,我深信"纸上得来终觉浅,绝知此事要躬行"的道理,因此之后的几篇博 ...

  4. docker安装-centos7

    操作系统要求 要安装Docker,您需要64位版本的CentOS 7.步骤:   卸载旧版本 Docker的旧版本被称为docker或docker-engine . 如果这些已安装,请卸载它们以及关联 ...

  5. kafka 0.10.2 解决java无法生产消息到指定topic问题

    主要是修改server.properties的advertised.listeners advertised.listeners=PLAINTEXT://192.168.59.132:9092

  6. scrapy跟pyspider的杂谈

    最近有一个私人项目要搞,可能最近的博客都会变成爬虫跟数据分析类的了.既然是爬虫,第一反应想到的就是鼎鼎大名的scrapy了,其次想到的pyspider,最后想到的就是自己写. scrapy是封装了tw ...

  7. Python中Swithch Case语法实现

    而python本身没有switch语句,解决方法有以下3种:A.使用dictionaryvalues = { value1: do_some_stuff1, value2: do_some_stuff ...

  8. java中的流程控制语句总结

    程序的结构分类: 顺序结构:按照写代码的顺序 一次执行 选择结构:根据条件的不同有选择的执行不同的代码 循环结构:在一定条件下 反复执行某一片代码 选择结构: 也叫分支结构 根据条件的不同,有选择的执 ...

  9. Centos 环境一键部署脚本(shell脚本)

    谨以此文纪念吊炸天的Centos环境一键部署方案的新鲜出炉 辛苦大半年,产品准备上线了,BOSS亲自体验安装部署,看着超过200+页的安装文档直接崩溃了(需要部署23个基础服务),经历了超过3个小时的 ...

  10. Html5模拟通讯录人员排序(sen.js)

    // JavaScript Document var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_2 = & ...