基本信息
源码名称:opencv+zbar识别二维码
源码大小:1.72KB
文件格式:.cpp
开发语言:C/C++
更新时间:2019-04-30
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


#include "zbar.h"
#include "opencv.hpp" 
#include "opencv2/highgui.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/types_c.h"
#include "opencv2/imgproc.hpp"
#include <iostream> 
#include "fstream"

using namespace std;
using namespace zbar;  //添加zbar名称空间      
using namespace cv;


int main(int argc, char*argv[])
{

	VideoCapture VC(0);
	while (true)
	{
		ImageScanner scanner;
		scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

		Mat image;
		VC.read(image);
		imshow("Source Image", image);
		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;
		}
		else
		{
			for (; symbol != imageZbar.symbol_end();   symbol)
			{
				cout << "类型:" << endl << symbol->get_type_name() << endl << endl;
				cout << "条码:" << endl << symbol->get_data() << endl << endl;
				//将检测的结果写到result.txt中方便查阅,追加方式写入的,
				ofstream fout("resule.txt", ios::app);
				fout << "类型:" << symbol->get_type_name() << endl << "条码:" << symbol->get_data() << endl << endl;
				fout.close();
				int key = cvWaitKey();
				if (key == 27) return 0;
			}
		}

		imageZbar.set_data(NULL, 0);
		int key = cvWaitKey(100);
		if (key == 27) return 0;
	}
	waitKey();
	return 0;
}