为了完成大飞哥老师布置的作业,调查了一下开源的二维码识别库。我只查了Zxing、Zbar,其中Zxing在我的机器上编译调试通过,但效果并不理想,所以又换了Zbar使用一下。(我的小伙伴找到了Zxing移动端的开源库,并调试成功,我过几天再补上)
环境:VS2010、OpenCV2.4.9、Zbar环境配置
环境说明:
1. OpenCV是一个机器视觉开源类库,广泛用于图像处理、模式识别等领域,它是Zbar项目的核心。这里我使用的是OpenCV2.4.9。(不建议使用OpenCV1版本)
2. 集成开发工具Visual Studio2010。(不建议使用Visual C++ 6.0,因其仅支持OpenCV1.0的版本)
3. Zbar开发环境
Zbar官网:http://zbar.sourceforge.net/download.html
OpenCV官网:http://opencv.org/
环境配置参考:http://blog.csdn.net/dcrmg/article/details/52108258
使用说明:
1. 安装OpenCV、Zbar,并进行环境配置
2. 新建C++控制台应用程序,并设置相应的工程属性,添加包含目录、附加链接等。
3. 测试程序如下:
#include "zbar.h"
#include "cv.h"
#include "highgui.h"
#include <iostream>
using namespace std;
using namespace zbar;
using namespace cv;
int main(void)
{
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
char file[256];
cin>>file;
Mat img = imread(file,0);
Mat imgout;
cvtColor(img,imgout,CV_GRAY2RGB);
int width = img.cols;
int height = img.rows;
uchar *raw = (uchar *)img.data;
Image image(width, height, "Y800", raw, width * height);
int n = scanner.scan(image);
for(Image::SymbolIterator symbol = image.symbol_begin();symbol != image.symbol_end();symbol++)
{
vector<Point> vp;
cout<<"Decoded:êo"<<endl<<symbol->get_type_name()<<endl<<endl;
cout<<"Symbol:êo"<<endl<<symbol->get_data()<<endl<<endl;
int n = symbol->get_location_size();
for(int i=0;i<n;i++)
{
vp.push_back(Point(symbol->get_location_x(i),symbol->get_location_y(i)));
}
RotatedRect r = minAreaRect(vp);
Point2f pts[4];
r.points(pts);
Point textPoint(pts[1]);
string codeNum=symbol->get_data();
for(int i=0;i<4;i++)
{
line(imgout,pts[i],pts[(i+1)%4],Scalar(255,0,0),3);
textPoint.x>=pts[i].x?textPoint.x=pts[i].x:textPoint.x=textPoint.x;
textPoint.y>=pts[i].y?textPoint.y=pts[i].y:textPoint.y=textPoint.y;
putText(imgout,codeNum,textPoint,FONT_HERSHEY_COMPLEX,1,Scalar(0,0,255),1,8,false);
}
cout<<"Angle: "<<r.angle<<endl;
}
imshow("imgout.jpg",imgout);
waitKey();
}
4. 先生成一个嵌入文本信息的二维码
运行测试程序,如下图:
换个网址再试试: