关于二维码

Version
二维码一共有40个尺寸。官方叫版本Version。Version 1是21 x 21的矩阵,Version 2是 25 x 25的矩阵,Version 3是29的尺寸,每增加一个version,就会增加4的尺寸,公式是:(V-1)*4 + 21(V是版本号) 最高Version 40,(40-1)*4+21 = 177,所以最高是177 x 177 的正方形。

// 21:Version=1,最大字符=17(8.5个汉字) // 25:Version=2,最大字符=32(16个汉字) //
29:Version=3,最大字符=49(24.5个汉字) // 33:Version=4,最大字符=78(39个汉字) //
37:Version=5,最大字符=106(53个汉字) // 41:Version=6,最大字符=134(67个汉字) //
45:Version=7,最大字符=154(77个汉字) // 49:Version=8,最大字符=192(96个汉字)

Error Correction Code Level

二维码中有四种级别的纠错,这就是为什么二维码有残缺还能扫出来,也就是为什么有人在二维码的中心位置加入图标。
二维码有4种纠错等级,可恢复的码字比例为:

#define QR_LEVEL_L 0 //7%的字码可被修正
#define QR_LEVEL_M 1 //15%的字码可被修正
#define QR_LEVEL_Q 2 //25%的字码可被修正
#define QR_LEVEL_H 3 //30%的字码可被修正

源码地址

​https://fukuchi.org/works/qrencode/​​​ 或者
https://github.com/fukuchi/libqrencode
下载源码

添加文件

添加根目录下的c文件和h文件,其中qrenc.c是一些使用例子,不用添加,把所有c文件中的

#if HAVE_CONFIG_H
# include "config.h"
#endif

改成

//#if HAVE_CONFIG_H
#include "config.h"
//#endif

config.h,就是配置文件,其中如下宏要注意

/* Define to 1 if you have the `strdup' function. */
#define HAVE_STRDUP 0

如果没有strdup,就改成0

还有如果内存分配释放不是malloc和free,比如FreeRTOS是pvPortMalloc和 vPortFree,则需对应替换,还有,如果calloc和realloc目标平台没有也需要找到相应位置改写。

测试例程

#include <string.h>
#include <stdio.h>
#include <stdint.h>

#include "qrencode.h"
#include "qrspec.h"
#include "mqrspec.h"
#include "bitstream.h"
#include "qrinput.h"
#include "rsecc.h"
#include "split.h"
#include "mask.h"
#include "mmask.h"

int main(void)
{
QRcode*qrcode; //最后结果
qrcode= QRcode_encodeString("测试",2, QR_ECLEVEL_L, QR_MODE_8,0);

printf("====version=%d========\r\n",qrcode->version);

for(int y=0;y<qrcode->width;y++)
{
for(int x=0;x<qrcode->width;x++)
{
if(qrcode->data[y*qrcode->width+x]&0x01)
{
printf("1");
// LCD_SetPixel(x,y,BLACK);
}
else
{
printf(" ");
// LCD_SetPixel(x,y,WHITE);
}
}
printf("\n");
}
QRcode_free(qrcode);
printf("\r\n");

return 0;
}

利用libqrencode生成二维码_#define
利用libqrencode生成二维码_#define_02