C 语言实现在终端里输出二维码

Mac 环境安装二维码库

bash 复制代码
brew install qrencode
  • 安装过程报权限问题执行以下命令
bash 复制代码
sudo chown -R 用户名 /usr/local/include /usr/local/lib
chmod u+w /usr/local/include /usr/local/lib
c 复制代码
#include <stdio.h>
#include <qrencode.h>

void print_qr_code(QRcode *qrcode) {
    int x, y;
    for (y = 0; y < qrcode->width; y++) {
        for (x = 0; x < qrcode->width; x++) {
            // 打印二维码模块,使用 '██' 表示黑色模块,使用 '  ' 表示白色模块
            if (qrcode->data[y * qrcode->width + x] & 1) {
                printf("██");
            } else {
                printf("  ");
            }
        }
        printf("\n");
    }
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <text>\n", argv[0]);
        return 1;
    }

    const char *text = argv[1];
    QRcode *qrcode = QRcode_encodeString(text, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
    if (qrcode == NULL) {
        fprintf(stderr, "Failed to generate QR code\n");
        return 1;
    }

    print_qr_code(qrcode);
    QRcode_free(qrcode);

    return 0;
}
  • 编译执行
bash 复制代码
# 编译
gcc -o demo demo.c -L/usr/local/opt/qrencode/lib -lqrencode -I/usr/local/opt/qrencode/include
# 执行
./demo "https://blog.csdn.net/weixin_42607526"
  • 输出结果
相关推荐
祈安_4 小时前
C语言内存函数
c语言·后端
郑州光合科技余经理2 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1232 天前
matlab画图工具
开发语言·matlab
dustcell.2 天前
haproxy七层代理
java·开发语言·前端
norlan_jame2 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054962 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
czy87874752 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
遥遥江上月2 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237172 天前
C语言-数组练习进阶
c语言·开发语言·算法