Git地址:https://github.com/fukuchi/libqrencode
官网:https://fukuchi.org/works/qrencode/index.html.en
平台:
Win10+VS2010+MFC 对话框
步骤:
- VS2010里新建一个项目QRTest,对话框模式,选择在共享DLL中使用MFC
- 在解决方案资源管理器里右键解决方案,添加–新建项目–Win32项目,取名libQR–选择静态库,同时取消预编译,结果如图
- 选出libqrencode里所有.h .c文件添加到libQR工程里去,注意排除掉qrenc.c这个文件,这个文件是用来说明怎么调用这个库的
- 右键libQR工程c++–预处理器–预处理器定义里添加HAVE_CONFIG_H,常规–目标文件名改为$(ProjectName)d,这样生成的debug的lib文件名为libQRd.lib,用来区分release下的lib
- 自带的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 /* 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 4 /* Micro version number */ #define MICRO_VERSION 0 /* Minor version number */ #define MINOR_VERSION 0 /* 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 4.0.0" /* 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 "4.0.0" /* Define to 1 if you have the ANSI C header files. */ #define STDC_HEADERS 1 /* Version number of package */ #define VERSION "4.0.0" #define inline /* Define to 'static' if no test programs will be compiled. */ #define STATIC_IN_RELEASE static /* #undef WITH_TESTS */
-
到此libqrencode就完成编译设置了,F7之后便能看到Debug文件夹里便有libQRd.lib文件
-
测试,到QRTest工程中,添加引入头文件,引用lib文件
-
#pragma comment(lib, "..\\Debug\\libQRd.lib") #else #pragma comment(lib, "..\\Release\\libQR.lib") #endif #include "..\libQR\qrencode.h"
-
生成二维码按钮
char* szSourceSring = "zhangsan";
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;
// 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;
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++)
{
*(pDestData + n * 3 + unWidthAdjusted * l) = 0xff;
*(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, "D:\\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);
}