如何用C代码生成二维码
当下因微信和支付宝等手机应用广泛使用,而基于二维码/一维条码的移动支付,也借助手机移动端席卷全国,使得越来越多的人知道有“二维码”这么一种东西。
对于普通用户而来,一般只知道将自己的二维码展示给别人,别人使用软件识别这个二维码即可完成一定的功能。比如,扫码二维码付款、扫码二维码加微信好友、扫码二维码访问网页、扫码二维码下载app等等。这些个功能,在日常行为中,已经很常见了,但作为程序猿的我们,我们怎么能不知道二维码是如何生成的呢?或者说,我要自己生成一个二维码,除了去网页上找二维码生成工具来生成,我可以自己编码来实现么?
答案,当然是,必须可以。不然这文章不用写了。
在介绍如何用代码生成二维码之前,就不得不先介绍一个开源库叫zint。这个开源可谓牛叉的很,几乎平时见过的“码”,各式各样的一维条码、各式各样的二维码条码都难不倒它,重要的是,它还是开源的,几乎包含了所有常见“码”的生成。以下是摘自官方用户使用手册的介绍片段。(笔者上一篇博文介绍zint的安装时简单介绍了一下zint库,http://www.cnblogs.com/Recan/p/5967378.html ,它的开源项目网页为https://sourceforge.net/projects/zint/)
The Zint project aims to provide a complete cross-platform open source barcode generating solution. The package currently consists of a Qt based GUI, a command line executable and a library with an API to allow developers access to the capabilities of Zint. It is hoped that Zint provides a solution which is flexible enough for professional users while at the same time takes care of as much of the processing as possible to allow easy translation from input data to barcode image.
-----------------------------------------------------华丽丽的分割线-----------------------------------------------------
言归正传,说回如何使用zint库生成二维码。主要使用到以下几个函数:可以从zint.h中得到api的声明(主要是C语言的接口)。
ZINT_EXTERN struct zint_symbol* ZBarcode_Create(void);
ZINT_EXTERN void ZBarcode_Clear(struct zint_symbol *symbol);
ZINT_EXTERN void ZBarcode_Delete(struct zint_symbol *symbol);
ZINT_EXTERN int ZBarcode_Encode_and_Print(struct zint_symbol *symbol, unsigned char *input, int length, int rotate_angle);
以下是个人封装的生成二维码的自定义接口函数:
/****************************************************************************
Descpribe: Create Qrcode API with C Code by calling zint lib.
Input : pQrCodeData, the qrcode data buf
QrcodeLen, the len of qrcode data, but it can be 0
pQrCodeFile, the output file name of qrcode, it can be NULL
Output : pZintRet, to store the ret code from linzint.
Return : 0 is ok, and other values are fail. See the meanings in enum ZINT_RET_CODE
Notes : pQrCodeFile, Must end in .png, .eps or .svg. when isn,t NULL string.
****************************************************************************/
ZINT_RET_CODE Zint_Create_QrCode(uint8_t *pQrCodeData, int QrcodeLen, char *pQrCodeFile, int *pZintRet);
这个接口定义比较简单,上面也简单说了各个参数的意义,其他中特别需要注意的是,如果传入生成二维码图片名字不使用默认值时(pQrCodeFile != NULL),也务必保证pQrCodeFile必须是以.png, .eps or .svg.结尾的文件名。
以下是zint_code.c 和 zint_code.h的内容,里面将zint中生成二维码的几个函数封装在一块了,使用者只需关注上面定义的Zint_Create_QrCode函数,即可生成漂亮的二维码图片文件。


1 /**************************************************************************** 2 * File : zint_code.c 3 * 4 * Copyright (c) 2011 by Li.Recan < 721317716@qq.com > 5 * 6 * DESCRIPTION: Demo for creating qrcode by C code. 7 * 8 * Modification history 9 * -------------------------------------------------------------------------- 10 * Date Version Author History 11 * -------------------------------------------------------------------------- 12 * 2016-10-15 1.0.0 Li.Recan written 13 ***************************************************************************/ 14 15 // Standard Library 16 #include <string.h> 17 #include <stdio.h> 18 19 // so Library 20 #include "zint.h" 21 22 // Project Header 23 #include "zint_code.h" 24 25 26 /***************************