一、需求
按照数据生成png格式的二维码图片。
二、方案
整合libqrencode、libpng的两个方法,从而实现,详见文:
[linux]使用libqrencode库生成二维码数据https://evenurs.blog.csdn.net/article/details/135655176?spm=1001.2014.3001.5502[linux]使用libpng库将数据生成为png图像文件https://evenurs.blog.csdn.net/article/details/135667030?spm=1001.2014.3001.5502
三、源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <png.h>
#include <qrencode.h>void write_png_file(char* filename, int width, int height, png_bytep* row_pointers) {FILE* fp = fopen(filename, "wb");if (!fp) {printf("Error opening file %s for writing\n", filename);return;}png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);if (!png_ptr) {printf("Error creating PNG write structure\n");fclose(fp);return;}png_infop info_ptr = png_create_info_struct(png_ptr);if (!info_ptr) {printf("Error creating PNG info structure\n");png_destroy_write_struct(&png_ptr, NULL);fclose(fp);return;}if (setjmp(png_jmpbuf(png_ptr))) {printf("Error during PNG writing\n");png_destroy_write_struct(&png_ptr, &info_ptr);fclose(fp);return;}png_init_io(png_ptr, fp);png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);png_write_info(png_ptr, info_ptr);png_write_image(png_ptr, row_pointers);png_write_end(png_ptr, NULL);png_destroy_write_struct(&png_ptr, &info_ptr);fclose(fp);
}int QR_CreatePng(QRcode *res,char *fileName) {// int width = 256;// int height = 256;png_bytep* row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * res->width);for (int y = 0; y < res->width; y++) {row_pointers[y] = (png_byte*)malloc(sizeof(png_byte) * res->width * 4);for (int x = 0; x < res->width; x++) {png_byte* pixel = &(row_pointers[y][x * 4]);if (res->data[y * res->width + x] & 0x01) {char pixBuf[4]={0,0,0,0xff};memcpy(pixel,pixBuf,4);} else {char pixBuf[4]={0xff,0xff,0xff,0xff};memcpy(pixel,pixBuf,4);}}}char buf[1024]={0};char*pngFile=".png";strcat(buf,fileName);strcat(buf,pngFile);printf("file name is %s\n",buf);write_png_file(buf, res->width, res->width, row_pointers);for (int y = 0; y < res->width; y++) {free(row_pointers[y]);}free(row_pointers);return 0;
}QRcode *QR_CreateData(QRcode *qrcode,char*rawData){// unsigned char *data;int version = 1;// int width = 256;// int margin = 2;// 生成二维码数据qrcode = QRcode_encodeString(rawData, version, QR_ECLEVEL_L, QR_MODE_8, 1);if (qrcode == NULL) {fprintf(stderr, "Failed to encode string.\n");return NULL;}return qrcode;
}
int main(int argc, char **argv) {if (argc < 2){printf("Usage:\n");printf("app argv[1] argv[2]\n");printf(" argv[1]: user data.\n");printf(" argv[2]: file name. \n");printf("for example:app www.abc.com abc \n");return -1;}QRcode *qrcode;QRcode *res=QR_CreateData(qrcode,argv[1]);if(res==NULL){goto exit;}else{QR_CreatePng(res,argv[2]);printf("data to %s.png finished.\n",argv[2]);}exit:// 释放内存QRcode_free(qrcode);return 1;
}
四、gcc编译
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/libpng-1.2.56/ -I /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/zlib-1.2.8/ -I /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/qrencode-3.4.4/ data2png_app.c -o data2png_app -L /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/libpng-1.2.56/.libs/ -L /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/zlib-1.2.8/ -L /home/Evenurs/f1c100s/dev/f1c100s/tina/out/c200s-F1C200s/compile_dir/target/qrencode-3.4.4/.libs -lqrencode -lpng -lz
arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
arm-openwrt-linux-muslgnueabi-gcc.bin: warning: environment variable 'STAGING_DIR' not defined
编译通过
五、通过adb push烧录
详细烧录步骤参考下文:
【adb】adb push命令 向设备传输文件https://evenurs.blog.csdn.net/article/details/128940198?spm=1001.2014.3001.5502
六、程序运行
root@TinaLinux:/# chmod -R 777 ./data2png_app
root@TinaLinux:/# ./data2png_app www.baidu.com baidu
file name is baidu.png
data to baidu.png finished.
七、图片查看
八、扫码查看
九、结论
本文主要将前两天的方法进行了整合,如某些环节看不懂可参考前文,了解库函数作用即可编写本应用,接下来会实现图片尺寸的平铺效果。