使用 Zint 库生成 QR Code 二维码

介绍

QR 码(Quick Response Code)

维基百科

结构

尺寸

​ Version 1 为 21 × 21 矩阵,Version 每增加 1 ,尺寸增加 4 ,公式为:(V - 1) * 4 + 21。Version 最高为40,即矩阵最大尺寸为 177 × 177。

存储

​ Maximum character storage capacity (40-L)

Input mode max. characters bits/char possible characters, default encoding
Numeric only 7,089 3⅓ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Alphanumeric 4,296 0–9, A–Z (upper-case only), space, $, %, *, +, -, ., /, :
Binary/byte 2,953 8 ISO 8859-1
Kanji/kana 1,817 13 Shift JIS X 0208

容错能力

错误修正容量
L水平 7%的字码可被修正
M水平 15%的字码可被修正
Q水平 25%的字码可被修正
H水平 30%的字码可被修正

阅读

二维码的生成细节和原理

Zint

地址

Github-zint

Zint Barcode Generator

编译

编译 zint 之前需先编译 zlib 和 libpng。

  • 到 http://www.zlib.net/ 下载 zlib 源码,到 http://www.libpng.org/pub/png/libpng.html 下载 libpng 源码。

  • 在 D 盘新建 opt 文件夹,在之下新建 include 与 lib 文件夹。

  • 将 zlib 与 libpng 源码解压至 opt 目录下。

  • 编译 libpng,完成后,拷贝

    libpng16.exp、libpng16.lib、zlib.lib

    至 D:\opt\lib,将 zlib 与 libpng 目录下 .h 文件拷贝至 D:\opt\include。

  • 打开 zint 项目,将附加依赖项中的 libpng13.lib 和 zlib1.lib 修改为 libpng16.lib 和 zlib.lib ,在 libzint 项目中的 png.c 添加

    include

  • 删除 libzint 项目中的 dm200.c 和 dm200.h。

  • 右键 libzint 项目,生成。

API

创建和删除 Symbols

#include 
#include 
int main() {
    struct zint_symbol *my_symbol;
    my_symbol = ZBarcode_Create();
    if (my_symbol != NULL) {
        printf("Symbol successfully created!\n");
    }
    ZBarcode_Delete(my_symbol);
    return 0;
}

编码并保存到文件

#include 
#include 
int main(int argc, char **argv) {
    struct zint_symbol *my_symbol;
    my_symbol = ZBarcode_Create();
    ZBarcode_Encode(my_symbol, argv[1], 0);
    ZBarcode_Print(my_symbol, 0);
    ZBarcode_Delete(my_symbol);
    return 0;
}
#include 
#include 
int main(int argc, char **argv) {
    struct zint_symbol *my_symbol;
    my_symbol = ZBarcode_Create();
    ZBarcode_Encode_and_Print(my_symbol, argv[1], 0, 0);
    ZBarcode_Delete(my_symbol);
    return 0;
}

设置选项

Variable Name Type Meaning Default Value
symbology integer Symbology to use. BARCODE_CODE128
height integer Symbol height. 50
whitespace_width integer Whitespace width. 0
boder_width integer Border width. 0
output_options integer Binding or box parameters. (none)
fgcolour character string Foreground (ink) colour as RGB hexadecimal string. Must be 6 characters followed by terminating \0 character. "000000"
bgcolour character string Background (paper) colour as RGB hexadecimal string. Must be 6 characters followed by terminating \0 character. "ffffff"
outfile character string Contains the name of the file to output a resulting barcode symbol to. Must end in .png, .eps or .svg "out.png"
option_1 integer Symbology specific options. (automatic)
option_2 integer Symbology specific options. (automatic)
option_3 integer Symbology specific options. (automatic)
scale float Scale factor for adjusting size of image. 1.0
input_mode integer Set encoding of input data. BINARY_MODE
primary character string Primary message data for more complex symbols. NULL
text unsigned character string Human readable text, which usually consists of the input data plus one or more check digits. Uses UTF-8 formatting. NULL
rows integer Number of rows used by the symbol or, if using barcode stacking, the row to be used by the next symbol. (output only)
width integer Width of the generated symbol. (output only)
encoding_data array of character strings Representation of the encoded data. (output only)
row_height array of integers Representation of the height of a row. (output only)
errtxt character string Error message in the event that an error occurred. (output only)
bitmap pointer to character array Pointer to stored bitmap image. (output only)
bitmap_width integer Width of stored bitmap image (in pixels). (output only)
bitmap_height integer Height of stored bitmap image (in pixels). (output only)

错误处理

Return Value Meaning
WARN_INVALID_OPTION One of the values in zint_struct was set incorrectly but Zint has made a guess at what it should have been and generated a barcode accordingly.
ERROR_TOO_LONG The input data is too long or too short for the selected symbology. No symbol has been generated.
ERROR_INVALID_DATA The data to be encoded includes characters which are not permitted by the selected symbology (e.g. alphabetic characters in an EAN symbol). No symbol has been generated.
ERROR_INVALID_CHECK An ISBN with an incorrect check digit has been entered. No symbol has been generated.
ERROR_INVALID_OPTION One of the values in zint_struct was set incorrectly and Zint was unable to guess what it should have been. No symbol has been generated.
ERROR_ENCODING_PROBLEM A problem has occurred during encoding of the data. This should never happen. Please contact the developer if you encounter this error.
ERROR_FILE_ACCESS Zint was unable to open the requested output file. This is usually a file permissions problem.
ERROR_MEMORY Zint ran out of memory. This should only be a problem with legacy systems.
#include 
#include 
#include 
int main(int argc, char **argv) {
    struct zint_symbol *my_symbol;
    int error = 0;
    my_symbol = ZBarcode_Create();
    strcpy(my_symbol->fgcolour, "nonsense");
    error = ZBarcode_Encode_and_Print(my_symbol, argv[1], 0, 0);
    if (error != 0) {
        /* some error occurred */
        printf("%s\n", my_symbol->errtxt);
    }
    if (error > WARN_INVALID_OPTION) {
        /* stop now */
        ZBarcode_Delete(my_symbol);
        return 1;
    }
    /* otherwise carry on with the rest of the application */
    ZBarcode_Delete(my_symbol);
    return 0;
}

指定 Symbology

Symbology 可以被指定为名称或者是数字。

例如 symbol->symbology = BARCODE_LOGMARS; 与 symbol->symbology = 50; 等价。

QR Code 为:58 BARCODE_QRCODE

设置输入模式

Value Effect
DATA_MODE Uses full ASCII range interpreted as Latin-1 or binary data.
UNICODE_MODE Uses pre-formatted UTF-8 input.
GS1_MODE Encodes GS1 data using FNC1 characters.

参考

https://en.wikipedia.org/wiki/QR_code

http://coolshell.cn/articles/10590.html

https://github.com/downloads/zint/zint/zint_manual_242.pdf

你可能感兴趣的:(使用 Zint 库生成 QR Code 二维码)