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"
  • 输出结果
相关推荐
春风解人意3 小时前
从零开始学习嵌入式P32----网络基础之TCP
c语言·网络·学习·tcp/ip
小灰灰搞电子3 小时前
Rust+Slint 实现动态消息提示框源码分享
开发语言·后端·rust
传奇开心果编程5 小时前
【Rust入门知识点学与练】第21课:Trait 进阶 Advanced Traits
开发语言·学习·rust
新时代牛马5 小时前
字符设备驱动完整篇:从 cdev_add、file_operations 到chrdev_open 与排障
开发语言·python
swordbob5 小时前
ReentrantLock 与 AQS 完整学习手册
java·开发语言
是隼人6 小时前
buuctf-pwn inndy_echo(32位fmt)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
白山编程大哥6 小时前
Java OutputStreamWriter 详解:从字符到字节的桥梁
java·开发语言·python
shmily麻瓜小菜鸡7 小时前
JavaScript / TypeScript 易踩坑知识点 —— 异步编程类
开发语言·javascript·typescript
刃神太酷啦7 小时前
Redis 进阶核心:持久化 (RDB/AOF)、事务与主从复制全解析----《Hello Redis!》(5)
linux·c语言·数据库·c++·redis·缓存·bootstrap
七夜zippoe7 小时前
为什么 2026 年每个 Java 团队都该懂 AI Agent
java·开发语言·人工智能