当前位置:   article > 正文

[linux]使用libqrencode库生成二维码数据

[linux]使用libqrencode库生成二维码数据

一、需求

要将一段数据生成为二维码

二、方案

使用linux标准库,通过libqrencode将需要写入的信息转为二维码图片数据。

三、实现

3.1编写c文件

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <qrencode.h>
  4. int main() {
  5. QRcode *qrcode;
  6. unsigned char *data;
  7. int version = 1;
  8. int width = 256;
  9. int margin = 2;
  10. // 生成二维码数据
  11. qrcode = QRcode_encodeString("Hello, world!", version, QR_ECLEVEL_L, QR_MODE_8, 1);
  12. if (qrcode == NULL) {
  13. fprintf(stderr, "Failed to encode string.\n");
  14. return -1;
  15. }
  16. // 创建图像数据
  17. data = (unsigned char *)malloc(qrcode->width * qrcode->width * 3);
  18. if (data == NULL) {
  19. fprintf(stderr, "Failed to allocate memory.\n");
  20. QRcode_free(qrcode);
  21. return -1;
  22. }
  23. // 将二维码数据转换为图像数据
  24. for (int y = 0; y < qrcode->width; y++) {
  25. for (int x = 0; x < qrcode->width; x++) {
  26. int offset = (y * qrcode->width + x) * 3;
  27. if (qrcode->data[y * qrcode->width + x] & 0x01) {
  28. data[offset] = 0; // R
  29. data[offset + 1] = 0; // G
  30. data[offset + 2] = 0; // B
  31. } else {
  32. data[offset] = 255; // R
  33. data[offset + 1] = 255; // G
  34. data[offset + 2] = 255; // B
  35. }
  36. }
  37. }
  38. // 保存图像数据为PNG文件
  39. FILE *fp = fopen("qrcode.png", "wb");
  40. if (fp == NULL) {
  41. fprintf(stderr, "Failed to open file.\n"); QRcode_free(qrcode);
  42. free(data);
  43. return -1;
  44. }
  45. fwrite(data, 1, qrcode->width * qrcode->width * 3, fp);
  46. fclose(fp);
  47. // 释放内存
  48. QRcode_free(qrcode);
  49. free(data);
  50. printf("QR code generated and saved as qrcode.png.\n");
  51. return 0;
  52. }

3.2编译c文件为可执行文件

3.2.1第一次编译

Evenurs@admin-PowerEdge-S350-166:~/f1c100s/dev/f1c100s/tina$ /home/Evenurs/f1c100s/dev/f1c100s/tina/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-arm9-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-gcc qrencode.c -o qrencode

报错:

  1. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
  2. qrencode.c:3:22: fatal error: qrencode.h: No such file or directory
  3. ^
  4. compilation terminated.

分析:找不到qrencode.h头文件。

解决方法:编译时依赖库文件。

3.2.2第二次编译

Evenurs@admin-PowerEdge-S350-166:~/f1c100s/dev/f1c100s/tina$ /home/Evenurs/f1c100s/dev/f1c100s/tina/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-arm9-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-gcc qrencode.c -o qrencode -lqrencode

报错:

  1. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
  2. qrencode.c:3:22: fatal error: qrencode.h: No such file or directory
  3. ^
  4. compilation terminated.

 分析:找不到库文件。

解决方法:需要指定头文件的路径。

3.2.3第三次编译

Evenurs@admin-PowerEdge-S350-166:~/f1c100s/dev/f1c100s/tina$ /home/Evenurs/f1c100s/dev/f1c100s/tina/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-arm9-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-gcc -I /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/qrencode-3.4.4/ qrencode.c -o qrencode

报错:

  1. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
  2. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
  3. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
  4. /tmp/ccEgRQio.o: In function `main':
  5. qrencode.c:(.text+0x3c): undefined reference to `QRcode_encodeString'
  6. qrencode.c:(.text+0xc8): undefined reference to `QRcode_free'
  7. qrencode.c:(.text+0x24c): undefined reference to `QRcode_free'
  8. qrencode.c:(.text+0x2a0): undefined reference to `QRcode_free'
  9. collect2: error: ld returned 1 exit status

分析:头文件已找到,库文件仍需要依赖。

解决方法:依赖库文件。

3.2.4第四次编译

Evenurs@admin-PowerEdge-S350-166:~/f1c100s/dev/f1c100s/tina$ /home/Evenurs/f1c100s/dev/f1c100s/tina/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-arm9-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-gcc -I /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/qrencode-3.4.4/ qrencode.c -o qrencode -lqrencode

报错:

  1. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
  2. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
  3. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
  4. /home/Evenurs/f1c100s/dev/f1c100s/tina/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-arm9-musl/toolchain/bin/../lib/gcc/arm-openwrt-linux-muslgnueabi/6.4.1/../../../../arm-openwrt-linux-muslgnueabi/bin/ld: cannot find -lqrencode
  5. collect2: error: ld returned 1 exit status

分析:依然找不到库文件路径。

解决方法:在编译时指定寻找查找库文件的路径。

3.2.5第五次编译

Evenurs@admin-PowerEdge-S350-166:~/f1c100s/dev/f1c100s/tina$ /home/Evenurs/f1c100s/dev/f1c100s/tina/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-arm9-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-gcc -I /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/qrencode-3.4.4/ qrencode.c -o qrencode -L /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/qrencode-3.4.4/.libs -lqrencode 

编译通过:

  1. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
  2. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
  3. arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined

3.3通过adb烧录

通过adb push的方法烧录,详细步骤可参考:

【adb】adb push命令 向设备传输文件icon-default.png?t=N7T8https://evenurs.blog.csdn.net/article/details/128940198?spm=1001.2014.3001.5502

3.4运行

  1. root@TinaLinux:/# chmod -R 777 ./qrencode
  2. root@TinaLinux:/# ./qrencode
  3. QR code generated and saved as qrcode.png.
  4. root@TinaLinux:/# ls
  5. bin lib pseudo_init rom tmp
  6. dev mnt qrcode.png root usr
  7. etc overlay qrencode sbin var
  8. hello proc rdinit sys www
  9. root@TinaLinux:/#

3.5将qrcode.png通过adb pull导出到电脑

查看数据:

确认是像素颜色数据,只是由于没有文件配置的头数据,会导致文件无法被打开与识别。

四、结论

通过libqrencode库,的确可以将数据转为二维码图片数据,图片数据转成图片则需要借助其他工具实现。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/41773?site
推荐阅读
相关标签
  

闽ICP备14008679号