Zbar的识别能力让我出乎意料。能够在一张较为复杂的图中,十分迅速且准确的定位并识别二维码。
条形码识别能力也很好,但是摄像头在采集条形码的时候,由于竖线间距小,很容易糊掉,二维码完全没问题。
1.下载
官网 http://zbar.sourceforge.net/download.html 下载最新版
或
wget http://downloads.sourceforge.net/project/zbar/zbar/0.10/zbar-0.10.tar.gz
如果你从sourceforge上下载请直接跳到第2步。
理论上可以从github上下载
git clone https://github.com/ZBar/ZBar.git
从github下载后需要自己 aclocal autoconf autoheader automake --add-missing
但不知为何,我一直在automake上出现各种警告无法生成Makefile,研究了很久也不知其所以,很是郁闷,待研究。
2.config
由于使用OpenCV作为图像采集的媒介,我们不需要zbar支持很多功能,在配置的时候可以disable许多捆绑选项。
./configure --without-python --disable-video --without-imagemagick --without-jpeg --without-qt --without-gtk --without-x
我是将所有选项都disable了,你也可以根据自己需要配置,但是需要一些库和软件的支持,根据提示apt-get即可。
如果你将zbar当作OpenCV的补充,那么尚可全都without。
3.make && make install
4.例程:
#include <iostream> //iostream需放在zbar.h之前,因为zbar兼容C,iostram中define了_cplusplus,zbar检测到会自动extern "C"
#include "zbar.h"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace zbar;
using namespace cv;
int main(int argc,char*argv[])
{
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
Mat image;
VideoCapture cam(0); //打开摄像头
int i =0;
while(1)
{
cam.read(image);
imshow("image",image);
Mat imageGray;
cvtColor(image,imageGray,CV_RGB2GRAY);
int width = imageGray.cols;
int height = imageGray.rows;
uchar *raw = (uchar *)imageGray.data;
Image imageZbar(width, height, "Y800", raw, width * height);
scanner.scan(imageZbar);
Image::SymbolIterator symbol = imageZbar.symbol_begin();
if(imageZbar.symbol_begin()!=imageZbar.symbol_end())
{
for(;symbol != imageZbar.symbol_end();++symbol)
{
cout<<"类型:"<<endl<<symbol->get_type_name()<<endl<<endl;
cout<<"条码:"<<endl<<symbol->get_data()<<endl<<endl;
}
}
imageZbar.set_data(NULL,0);
char key = waitKey(100);
if(key == 27) break;
}
return 0;
}