之前在做跟华为合作的项目,需要根据字串来生成特定的二维码,特在此记录,以便查看。
二维码图样有很多种,根据项目要求,我选择了使用libqrcode库来实现,这是个开源库,使用十分方便,首先下载libqrcode源码,根据平台选择编译条件并编译成动态库so。在自己编写程序包含相应的头文件,编译链接动态库即可。
以下是我部分code,仅供参考。
qrcode.h
#ifndef _QRCODE_H_
#define _QRCODE_H_
#ifdef __cplusplus
extern "C"{
#endif
#include "qrencode.h"
enum imageType {
PNG_TYPE,
PNG32_TYPE,
EPS_TYPE,
SVG_TYPE,
XPM_TYPE,
ANSI_TYPE,
ANSI256_TYPE,
ASCII_TYPE,
ASCIIi_TYPE,
UTF8_TYPE,
ANSIUTF8_TYPE,
UTF8i_TYPE,
ANSIUTF8i_TYPE
};
int makeQrcode(const char *SourceSring, const char *path);
#ifdef __cplusplus
}
#endif
#endif
qrcode.cpp
#include <string.h>
#include <errno.h>
//#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <wchar.h>
#include <png.h>
#include "qrcode.h"
#pragma pack(push, 2)
#pragma pack(pop)
#define INCHES_PER_METER (100.0/2.54)
static int margin = 1;
static int size = 8; //qrcode size
static unsigned char fg_color[4] = {0, 0, 0, 255};
static unsigned char bg_color[4] = {201, 201, 201, 255};
static int dpi = 72;
static void fillRow(unsigned char *row, int num, const unsigne