C语言 如何正确编码数字 QRCode



我正在使用libqrencode。
我想要一个版本 1 (21x21) 和 ECC 级别 H 的二维码。根据 http://www.qrcode.com/en/about/version.html 我可以有 17 个数字。所以我这样做:

QRcode *result;
QRinput *input = QRinput_new2(1, QR_ECLEVEL_H);
unsigned char *data = new unsigned char[17];
for(int i = 0; i < 17; i++) {
    data[i] = 0;
}
QRinput_append(input, QR_MODE_NUM, 17, data);
result = QRcode_encodeInput(input);
int idx = 0;
printf("%dn", result->width);
for(int i = 0; i < result->width; i++) {
    for(int j = 0; j < result->width; j++) {
        if(result->data[idx] & 1) {
            printf("%d", 1);
        } else {
            printf("%d", 0);
        }
        idx++;
    }
    printf("n");
}

但是无论我的数据是什么,我的程序都会返回相同的输出。
我在这里错过了什么?

我为那里的 github 提交了一个问题,很快就得到了答案 https://github.com/fukuchi/libqrencode/issues/33#issuecomment-24997167
问题是我在这里的数据初始化:

unsigned char *data = new unsigned char[17];
for(int i = 0; i < 17; i++) {
    data[i] = 0;
}

应该是:

unsigned char *data = new unsigned char[17];
for(int i = 0; i < 17; i++) {
    data[i] = '0'; //here
}

最新更新