OpenCV + zbar realizes QR code scanning

surroundings

visual studio 2019 + opencv3.3.1 + zbar

Configuration

Configure OpenCV

It is the same as the zbar configuration method below, so I won't repeat it here, you can check the related blog configuration by yourself.

Place zbar

You can go to zbar official website to download: http://zbar.sourceforge.net/download.html
zabar's github library: https://github.com/marmalade/zbar
Insert picture description here
but the compiled zbar provided by the official website seems to be only x86 version, configuration It's been a long time, and it always goes wrong.
The x64-bit version is provided here:
Link: https://pan.baidu.com/s/1EsGbBhkwv_yLVSn1IQoWvw Extraction code: After
downloading and decompressing iibk , there are several folders as follows: The
Insert picture description here
configuration method is the same as that of OpenCV, create a new one in the project Configuration:

Insert picture description here
Then double-click to edit, and fill
in the
executable file path in
Insert picture description here
the VC++ directory according to your decompression location. This can also be added to the PATH in the environment variable. After adding, if the program reports an error, generally restart the computer and it will be OK. Or directly copy the files in bin to C:\Windows\System32. (Not recommended)
Include directory:
Insert picture description here
library directory:
Insert picture description here
then linker -> input -> additional dependencies : the
Insert picture description here
configuration is now complete.

Code display

Barcode scanning recognition:

Input: Barcode on the back of Gonzalez Image Processing
Insert picture description here
Code:

#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;
}

result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44456692/article/details/109776706