OpenCV + zbar实现二维码扫描

环境

visual studio 2019 + opencv3.3.1 + zbar

配置

配置OpenCV

和下边的zbar配置方法相同,在此不再赘述,可自行查相关的博客配置。

配置zbar

可以去zbar官网下载:http://zbar.sourceforge.net/download.html
zabar的github库:https://github.com/marmalade/zbar
在这里插入图片描述
但是官网提供的编译好的zbar好像只有x86版本的,配置了很久,总是出错。
这里提供x64位的版本:
链接: https://pan.baidu.com/s/1EsGbBhkwv_yLVSn1IQoWvw 提取码: iibk
下载解压后,有如下几个文件夹:
在这里插入图片描述
配置方法和OpenCV的方法相同,在项目中新建一个配置:

在这里插入图片描述
然后双击编辑,按自己的解压位置填
VC++目录里边的
可执行文件路径
在这里插入图片描述
这个也可以在环境变量里边在PATH添加,添加完后如果程序报错,一般重启一下电脑就OK了。或者直接把bin里边的文件复制到C:\Windows\System32下面。(不推荐)
包含目录:
在这里插入图片描述
库目录:
在这里插入图片描述
然后链接器->输入->附加依赖项
在这里插入图片描述
至此配置完成。

代码展示

条形码扫描识别:

输入:冈萨雷斯图像处理背面的条形码
在这里插入图片描述
代码:

#include <iostream>
#include <opencv2\opencv.hpp>
#include <zbar.h>

using namespace std;
using namespace cv;
using namespace zbar;

//主函数
int main()
{
    
    
	Mat src = imread("pictures/test1.jpg");
	if (src.empty())
	{
    
    
		cout << "reading images fails" << endl;
	}

	Mat gray;
	cvtColor(src, gray, COLOR_BGR2GRAY);
	namedWindow("select ROI", WINDOW_NORMAL);
	Rect box = selectROI("select ROI", gray, false, false);
	destroyWindow("select ROI");
	Mat result_img = gray(box);

	ImageScanner scanner;
	scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
	int width = result_img.step;
	int height = result_img.rows;
	uchar* raw = (uchar*)result_img.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;
	}
	imageZbar.set_data(NULL, 0);
	waitKey(0);
	return 0;
}

结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44456692/article/details/109776706
今日推荐