使用Visual Studio 2022调用libqrencode生成二维码

新建一个VS控制台新项目qrtest

建立项目后建立主程序文件,拷贝qrencode.h到工程目录,配置依赖qrencode.lib。

代码

void qr_code(std::string _src_str, std::string _dst_bmp_path)
{
	const char* szSourceSring = _src_str.c_str();
	unsigned int unWidth, x, y, l, n, unWidthAdjusted, unDataBytes;
	unsigned char* pRGBData, * pSourceData, * pDestData;
	QRcode* pQRC;
	FILE* f;

	if (pQRC = QRcode_encodeString(szSourceSring, 0, QR_ECLEVEL_H, QR_MODE_8, 1))
	{
		unWidth = pQRC->width;
		unWidthAdjusted = unWidth * 8 * 3;
		if (unWidthAdjusted % 4) {
			unWidthAdjusted = (unWidthAdjusted / 4 + 1) * 4;
		}
		unDataBytes = unWidthAdjusted * unWidth * 8;
		if (!(pRGBData = (unsigned char*)malloc(unDataBytes))) {
			std::cout << "malloc err" << std::endl;
			exit(-1);
		}

		//Preset to White
		memset(pRGBData, 0xff, unDataBytes);

		//Prepare bmp headers
		BITMAPFILEHEADER kFileHeader;
		kFileHeader.bfType = 0x4d42;	//"BM"
		kFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + unDataBytes;
		kFileHeader.bfReserved1 = 0;
		kFileHeader.bfReserved2 = 0;
		kFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
		BITMAPINFOHEADER kInfoHeader;
		kInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
		kInfoHeader.biWidth = unWidth * 8;
		kInfoHeader.biHeight = ((int)unWidth * 8);
		kInfoHeader.biPlanes = 1;
		kInfoHeader.biBitCount = 24;
		kInfoHeader.biCompression = BI_RGB;
		kInfoHeader.biSizeImage = 0;
		kInfoHeader.biXPelsPerMeter = 0;
		kInfoHeader.biYPelsPerMeter = 0;
		kInfoHeader.biClrUsed = 0;
		kInfoHeader.biClrImportant = 0;

		//Convert QrCode bits to bmp pixels
		pSourceData = pQRC->data;
		for (y = 0; y < unWidth; y++)
		{
			pDestData = pRGBData + unWidthAdjusted * y * 8;
			for (x = 0; x < unWidth; x++)
			{
				if (*pSourceData & 1)
				{
					for (l = 0; l < 8; l++)
					{
						for (n = 0; n < 8; n++)
						{
							//以下三行是设置三基色 三基色设置0x00 则二维码颜色就是黑色的
							*(pDestData + n * 3 + unWidthAdjusted * l) = 0xff;
							*(pDestData + 1 + n * 3 + unWidthAdjusted * l) = 0x00;
							*(pDestData + 2 + n * 3 + unWidthAdjusted * l) = 0x00;
						}
					}
				}
				pDestData += 3 * 8;
				pSourceData++;
			}
		}

		//output the bmp file
		if (!(fopen_s(&f, _dst_bmp_path.c_str(), "wb")))
		{
			fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER), 1, f);
			fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER), 1, f);
			fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f);
			std::cout << "qrcode has generated in " << _dst_bmp_path.c_str() << std::endl;
			fclose(f);
		}
		else
		{
			std::cout << "unable to open file" << std::endl;
			exit(-1);
		}
		//Free data
		free(pRGBData);
		QRcode_free(pQRC);

	}
	else
	{
		std::cout << "NULL returned" << std::endl;
		exit(-1);
	}
}

调用

// TODO: 在此添加控件通知处理程序代码
	std::string _src_str = "http://www.baidu.com";
	std::string _dst_bmp_path = "2.png";
	qr_code(_src_str, _dst_bmp_path);

运行结果:

赞(0)
未经允许不得转载:程序员中文网 » 使用Visual Studio 2022调用libqrencode生成二维码
分享到: 更多 (0)
关于我们 免责申明 意见反馈 隐私政策
程序员中文网:公益在线网站,帮助学习者快速成长!
关注微信 技术交流
推荐文章
每天精选资源文章推送
推荐文章
随时随地碎片化学习
推荐文章
发现有趣的