- 调试信息:开发者可以在终端生成二维码,包含调试信息或日志数据,便于移动设备扫描和查看。
 - 设备配置:物联网设备配置时,通过终端生成配置二维码,扫描后进行设备配置。
 
 
 
sudo apt-get update
sudo apt-get install libqrencode-dev
 
 
brew install qrencode
 
 
sudo chown -R 用户名 /usr/local/include /usr/local/lib
chmod u+w /usr/local/include /usr/local/lib
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ifaddrs.h>
#include <arpa/inet.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");
    }
}
char *get_local_ip_address() {
    struct ifaddrs *ifaddr, *ifa;
    char *ip = NULL;
    int family;
    
    if (getifaddrs(&ifaddr) == -1) {
        perror("getifaddrs");
        return NULL;
    }
    for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
        if (ifa->ifa_addr == NULL) continue;
        family = ifa->ifa_addr->sa_family;
        if (family == AF_INET) { 
            char addr[INET_ADDRSTRLEN];
            if (inet_ntop(family, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, addr, sizeof(addr)) != NULL) {
                
                if (strcmp(addr, "127.0.0.1") != 0 && strncmp(addr, "169.254", 7) != 0) {
                    ip = strdup(addr);
                    break;
                }
            }
        }
    }
    freeifaddrs(ifaddr);
    return ip;
}
int main() {
    char *ip = get_local_ip_address();
    if (ip == NULL) {
        fprintf(stderr, "Failed to get local IP address\n");
        return 1;
    }
    
    char url[256];
    snprintf(url, sizeof(url), "http://%s", ip);
    printf("URL: %s\n", url);
    QRcode *qrcode = QRcode_encodeString(url, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
    if (qrcode == NULL) {
        fprintf(stderr, "Failed to generate QR code\n");
        free(ip);
        return 1;
    }
    print_qr_code(qrcode);
    QRcode_free(qrcode);
    free(ip);
    return 0;
}
 
 
gcc -o demo demo.c -L/usr/local/opt/qrencode/lib -lqrencode -I/usr/local/opt/qrencode/include
./demo
 
 
