OpenCV搭配Zbar进行二维码及条形码识别
先上代码
#include<Windows.h>
#include <iostream>
#include<zbar.h>
#include<opencv.hpp>
#include<string.h>
#include<opencv2\imgproc\types_c.h>//解决CV_RGB2GRAY:无法解析的标识符
using namespace cv;
using namespace zbar;
using namespace std;//string和cout
int main()
{
ImageScanner scanner;//定义扫描器
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
//加载图像
Mat image = imread("test.png");
//图像转换
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();
//扫描结果打印
for (; symbol != imageZbar.symbol_end(); ++symbol)
{
cout << "类型:" << endl << symbol->get_type_name() << endl << endl;
cout << "条码:" << endl << symbol->get_data() << endl << endl;
}
/*------Line------*/
cv::QRCodeDetector QRdetecter;
std::vector<cv::Point> list;
cv::Mat res;
QRdetecter.detectAndDecode(image, list, res);//res:提取出的二维码;返回解码后的字符串
//画线
for (int i = 0; i < list.size(); i++)
{
if (i == 3)
line(image, list[i], list[0], Scalar(0, 255, 0), 3);
else
line(image, list[i], list[i + 1], Scalar(0, 255, 0), 3);
}
imshow("image", image);
waitKey();
imageZbar.set_data(NULL, 0);//清除缓存
return 0;
}
测试效果
二维码
条形码
倾斜条形码,8行
混合,只出了二维码