MFC二维码生成,libqrencode——小白级

直接使用:下载我编译好的库-链接-,直接跳转到使用库那一步

两个工程打包链接 ——下载

先上最终效果:

 

二维码生成,参考: 跳转链接

一、 libqrencode库编译

libqrencode是一个日本人写的的库,库在这里下载。

1、新建一个config.h文件

文件中的内容为:

/* config.h.  Generated from config.h.in by configure.  */
/* config.h.in.  Generated from configure.ac by autoheader.  */
 
/* Define to 1 if you have the <dlfcn.h> header file. */
#define HAVE_DLFCN_H 1
 
/* Define if you have the iconv() function and it works. */
/* #undef HAVE_ICONV */
 
/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1
 
/* Define to 1 if using pthread is enabled. */
//#define HAVE_LIBPTHREAD 1
 
/* Define to 1 if you have the <memory.h> header file. */
#define HAVE_MEMORY_H 1
 
/* Define to 1 if you have the <stdint.h> header file. */
#define HAVE_STDINT_H 1
 
/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1
 
/* Define to 1 if you have the `strdup' function. */
#define HAVE_STRDUP 1
 
/* Define to 1 if you have the <strings.h> header file. */
#define HAVE_STRINGS_H 1
 
/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1
 
/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1
 
/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1
 
/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1
 
/* Define to the sub-directory in which libtool stores uninstalled libraries.
   */
#define LT_OBJDIR ".libs/"
 
/* Major version number */
#define MAJOR_VERSION 3
 
/* Micro version number */
#define MICRO_VERSION 4
 
/* Minor version number */
#define MINOR_VERSION 4
 
/* Name of package */
#define PACKAGE "qrencode"
 
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT ""
 
/* Define to the full name of this package. */
#define PACKAGE_NAME "QRencode"
 
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "QRencode 3.4.4"
 
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "qrencode"
 
/* Define to the home page for this package. */
#define PACKAGE_URL ""
 
/* Define to the version of this package. */
#define PACKAGE_VERSION "3.4.4"
 
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1
 
/* Version number of package */
#define VERSION "3.4.4"
 
/* Define to empty if `const' does not conform to ANSI C. */
/* #undef const */
 
/* Define to `__inline__' or `__inline' if that's what the C compiler
   calls it, or to nothing if 'inline' is not supported under any name.  */
#ifndef __cplusplus
/* #undef inline */
#endif
 
/* Define to 'static' if no test programs will be compiled. */
#define STATIC_IN_RELEASE static
/* #undef WITH_TESTS */

2、编译libqrencode

1)增删文件
解压libqrencode库之后,将config.h添加进去,将除了.c和.h的文件全部删除,还要删除qrenc.c,如果保留此文件,编译静态库时会发生错误。
处理完之后还剩下如下文件:

 2)编译
①在VS中创建一个win32项目,选择生成静态库,不使用预编译头。

 ②添加现有项,把所有文件添加进去

 ③然后选中项目,点击右键,属性->配置属性->C/C++ ->预处理器,在“预处理器”定义中添加HAVE_CONFIG_H.

④点击生成->生成解决方案

 二、使用库

1、使用静态库
将生成的lib文件和源码中的qrencode.h两个文件拷贝到项目中。
然后配置属性->C/C++->常规->附加包含目录,加入qrencode.h的所在路径。
配置属性->链接器属性->链接器->常规->附加库目录,加入ErWeiMaLib.lib所在路径。
配置属性->链接器属性->输入->附加依赖项,加入ErWeiMaLib.lib。

2、生成二维码保存到本地项目路径下

void GenerateQRcode(char * msg){

	unsigned int    unWidth, x, y, l, n, unWidthAdjusted, unDataBytes;
	unsigned char* pRGBData, * pSourceData, * pDestData;
	QRcode* pQRC;
	FILE* f;

	if (pQRC = QRcode_encodeString(msg, 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;

		// Allocate pixels buffer

		if (!(pRGBData = (unsigned char*)malloc(unDataBytes)))
		{
			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;// 位图文件保留字,必须为0
		kFileHeader.bfReserved2 = 0;// 位图文件保留字,必须为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;// 目标设备的级别,必须为1
		kInfoHeader.biBitCount = 24; // 每个像素所需的位数,必须是1(双色)、
									//4(16色)、8(256色)或24(真彩色)之一
		kInfoHeader.biCompression = BI_RGB;// 位图压缩格式,必须是0,不压缩
		kInfoHeader.biSizeImage = 0; // 位图的大小,以字节为单位
		kInfoHeader.biXPelsPerMeter = 0; // 位图水平分辨率
		kInfoHeader.biYPelsPerMeter = 0; // 位图垂直分辨率
		kInfoHeader.biClrUsed = 0; // 位图实际使用的颜色表中的颜色数
		kInfoHeader.biClrImportant = 0; // 位图显示过程中重要的颜色数,0表示所有的颜色都重要

		// Convert QrCode bits to bmp pixels

		pSourceData = pQRC->data;
		for(int y=unWidth-1;y>=0;y--)
		{
			pDestData = pRGBData + unWidthAdjusted * y * 8;
			// y
			
			for (x = 0; x < unWidth; x++)
			{
				if (*pSourceData & 1)
				{

					for (int l = 0; l < 8; l++)
					{
						for (int n=0; n < 8; n++)
						{
							*(pDestData + n * 3 + unWidthAdjusted * l) = 0;
							*(pDestData + 1 + n * 3 + unWidthAdjusted * l) = 0;
							*(pDestData + 2 + n * 3 + unWidthAdjusted * l) = 0;
						}
					}
				}
				pDestData += 3 * 8;
				pSourceData++;
			}
		}
		// Output the bmp file
		if (!(fopen_s(&f, "temp.bmp", "wb")))
		{
			fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER), 1, f);
			fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER), 1, f);
			fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f);
			fclose(f);
		}
		else
		{
			printf("Unable to open file");
			exit(-1);
		}
		// Free data
		free(pRGBData);
		QRcode_free(pQRC);
	}
	else
	{
		printf("NULL returned");
		exit(-1);
	}
}

3、使用方式  。WideCharToMultiByte里面  填写CP_UTF8就能使用中文了

    CString str;
	GetDlgItemText(IDC_WENZI ,str);

	int len = WideCharToMultiByte(CP_UTF8, 0, str, str.GetLength(), NULL, 0, NULL, NULL);
	char* szSourceSring = new char[len + 1];
	WideCharToMultiByte(CP_UTF8, 0, str, str.GetLength(), szSourceSring, len, NULL, NULL);
	szSourceSring[len] = '\0';

	GenerateQRcode(szSourceSring);

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
libqrencode是一个C语言编写的库,用于生成二维条形码。生成的二维码可以通过手机的CCD摄像机轻松扫描出来。该库具有很强的鲁棒性,可以生成容量多达7000个数字或4000个字符的二维码。\[1\] 要使用libqrencode生成二维码,你需要以下几个步骤: 1. 下载稳定版本的libqrencode源代码包,可以从http://fukuchi.org/works/qrencode/下载。 2. 解压下载的源代码包,将其中的.h和.c文件拷贝到一个文件夹中,例如命名为libqrencode。 3. 创建一个工程,选择适合你的平台(如OSX或iOS)的应用程序模板。 4. 将libqrencode文件夹中的源代码添加到你的工程中。 5. 根据你的需求,调用libqrencode提供的函数来生成二维码。 这样,你就可以使用libqrencode库来生成二维码了。请注意,具体的实现细节可能因你的开发环境和需求而有所不同。\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [基于libqrencode二维码生成](https://blog.csdn.net/SkyNullCode/article/details/49934699)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [使用libqrencode生成二维码,支持telphone,短信,url等](https://blog.csdn.net/u013295518/article/details/89680310)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值