欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用qrencode库生成指定信息二维码图片

程序员文章站 2024-03-19 21:44:16
...

qrencode源码下载地址:https://fukuchi.org/works/qrencode/

windows下编译方式,新建visual studio工程,将源码目录下所有*.c/*.h文件加入工程中(除了qrenc.c,此为测试例子),然后把config.h.in重命名为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 <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1  
/* Define to 1 if using pthread is enabled. */
#undef HAVE_LIBPTHREAD  
/* 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 <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 `strdup' function. */
//#define HAVE_STRDUP 1  
#undef  HAVE_STRDUP
/* 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  
/* 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 inline  
/* Define to 'static' if no test programs will be compiled. */
#define __STATIC static  
/* #undef WITH_TESTS */

编译过程中会提示strdup函数错误,可重命名函数(因为编译时会跟c++中的strdup冲突),预处理宏中添加HAVE_CONFIG_H

编译为静态库,项目中直接引用。

下面是二维码图片生成代码(bmp图片)

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include "qrcode/include/config.h"
#include "qrcode/include/qrencode.h"

using namespace std;

#ifdef _M_IX86
	#pragma comment(lib,"qrcode/lib/libQR32.lib")
#else
	#pragma comment(lib,"qrcode/lib/libQR64.lib")
#endif

int _tmain(int argc, _TCHAR* argv[])
{
	string data("192.168.1.1 aa-bb-cc-dd-ee-ff administrator asfnsdugnerhgernihdmakfa");
	QRcode* qrData = QRcode_encodeString(data.c_str(), 1, QR_ECLEVEL_L, QR_MODE_8, 1);
	if (NULL == qrData)
	{
		cout << "QRcode_encodeString error" << endl;
		return -1;
	}
	unsigned int width = qrData->width;
	cout << "width: " << width << endl;
	unsigned int widthAdjusted = width * 8 * 3;
	if (widthAdjusted % 4)
		widthAdjusted = (widthAdjusted / 4 + 1) * 4;
	unsigned int bytesOfData = widthAdjusted * width * 8;
	unsigned char* pixelData = new unsigned char[bytesOfData];
	memset(pixelData, 0xff, bytesOfData);
	
	BITMAPFILEHEADER fileHeader;
	ZeroMemory(&fileHeader, sizeof(fileHeader));
	fileHeader.bfType = 0x4d42; // BM
	fileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bytesOfData;
	fileHeader.bfReserved1 = 0;
	fileHeader.bfReserved2 = 0;
	fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

	BITMAPINFOHEADER infoHeader;
	ZeroMemory(&infoHeader, sizeof(infoHeader));
	infoHeader.biSize = sizeof(BITMAPINFOHEADER);
	infoHeader.biWidth = width * 8;
	infoHeader.biHeight = -((int)width * 8);
	infoHeader.biPlanes = 1;
	infoHeader.biBitCount = 24;
	infoHeader.biCompression = BI_RGB;
	infoHeader.biSizeImage = 0;
	infoHeader.biXPelsPerMeter = 0;
	infoHeader.biYPelsPerMeter = 0;
	infoHeader.biClrUsed = 0;
	infoHeader.biClrImportant = 0;
	
	unsigned char* source = qrData->data;
	for (int j=0; j<width; ++j)
	{
		unsigned char* dest = pixelData + widthAdjusted * j * 8;
		for (int i=0; i<width; ++i)
		{
			if (*source & 1)
			{
				for (int k=0; k<8; ++k)
					memset(dest + widthAdjusted * k, 0, 24);
			}
			dest += 3 * 8;
			++source;
		}
	}
	QRcode_free(qrData);

	ofstream ofs(L"qrcode.bmp", ios::binary);
	if (ofs.is_open())
	{
		ofs.write(reinterpret_cast<const char*>(&fileHeader), sizeof(fileHeader));
		ofs.write(reinterpret_cast<const char*>(&infoHeader), sizeof(infoHeader));
		ofs.write(reinterpret_cast<const char*>(pixelData), bytesOfData);
		ofs.close();
	}
	delete[] pixelData;

	return 0;
}

参考:https://www.zhaokeli.com/article/8311.html

相关标签: e