承接上文 opencv 通过cmake编译生成动态链接库
配置包含:
VC++目录 包含目录

vc++ 库目录
链接器 输入
配置属性->调试->工作目录
下面是识别barcode的代码
image\10.png图片放在了E:\vs2015_sketchtown\opencv_vs2015\bin\Release 里
// QR_test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "zbar.h"
/*
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat image(2, 2, CV_8UC3, Scalar(0, 0, 255));
//cout << "M = " << endl << " " << image << endl << endl;
//if (argc != 2)
//{
// printf("usage: DisplayImage.out <Image_Path>\n");
// return -1;
//}
image = imread("E:\\1.jpg", IMREAD_COLOR);
if (!image.data)
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
*/
using namespace std;
using namespace zbar; //添加zbar名称空间
using namespace cv;
int main(int argc, char*argv[])
{
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
Mat image = imread("image\\10.png");
if (!image.data)
{
cout << "请确认图片" << endl;
system("pause");
return 0;
}
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())
{
cout << "查询条码失败,请检查图片!" << endl;
}
for (; symbol != imageZbar.symbol_end(); ++symbol)
{
cout << "类型:" << endl << symbol->get_type_name() << endl << endl;
cout << "条码:" << endl << symbol->get_data() << endl << endl;
}
imshow("Source Image", image);
waitKey();
imageZbar.set_data(NULL, 0);
return 0;
}
下面附上运行程序的代码图:
将图片替换成二维码也同样可以识别成功
注意:还要讲opencv和zbar中bin文件路径配置到系统环境变量上。如果编译报出缺少相应有关zbar的dll文件,将zbar中bin文件中的lib文件考到之前配置的vs工作目录中即可。