树莓派 二维码(QR)识别 c语言篇

                     树莓派 二维码(QR)识别 c语言篇

 先看一下运行结果:

识别图片二维码,源程序:


#include "zbar.h"      

#include "cv.h"      

#include "highgui.h"      

#include <iostream>      



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("helloword.jpg");  

	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-二维码检测--摄像头检测

源程序:


#include "zbar.h"      

#include "cv.h"      

#include "highgui.h"      

#include <iostream>      



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("helloword.jpg");  

	Mat image;
	VideoCapture cpt;
	cpt.open(0);
	char key=0;
	key = waitKey(50);

	while (key != 27)
	{
		key = waitKey(50);                    //获取用户和按键的交互信息,并延迟50
		cpt >> image;                         //获取每一帧 ,显示到  Mat image 上
	//	imshow("视频", 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;

		}

		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(30);  ///////////////////////////////////////////  //要有一定延迟才能显示视频

		imageZbar.set_data(NULL, 0);

	

	}
	return 0;

}

希望对你有帮助。

猜你喜欢

转载自blog.csdn.net/qq_41204464/article/details/84933311