C语言字符串生成图片(libgd)学习2

        参考内容博客地址:http://elkpi.com/topics/libgd-notes.html

        libgd官网网址:https://libgd.github.io/

        今天接着学习相关如何将中文字符串转成图片。

#include <stdio.h>
#include <string.h>
#include "gd.h"

int main(int argc, char **argv)
{
    gdImagePtr stImg;             //声明图像结构体,具体内容gd.h文件
    FILE *pfJpeg = NULL;          //声明输出文件格式
    int black;                    //声明颜色索引
    int white;
    char *pcMsg = "hello, 你好";    //转换字符串内容
    double sz = 100.0;            //字体高度
    char *err = NULL;
    int x;                        //存储图片生成高度
    int y;                        //存储图片生成宽度
    //linux中文字体为文泉驿微米黑,系统路径为/usr/share/fonts/wqy-microhei.ttc
    char *pcFont = "/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc";
    
    //计算单个字体宽度&#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在嵌入式Linux下使用C语言生成二维码bmp图片,可以通过以下步骤实现: 1. 安装libqrencode库 在嵌入式Linux系统中,可以使用包管理器或手动编译安装libqrencode库,该库提供了生成二维码的API接口。 2. 编写代码生成二维码数据 使用libqrencode库提供的API接口,可以生成二维码数据。以下是一个简单的示例: ``` #include <stdio.h> #include <string.h> #include <qrencode.h> int main(int argc, char **argv) { QRcode *qrcode = QRcode_encodeString("hello world", 0, QR_ECLEVEL_L, QR_MODE_8, 1); if (qrcode) { for (int y = 0; y < qrcode->width; y++) { for (int x = 0; x < qrcode->width; x++) { if (qrcode->data[y*qrcode->width + x] & 1) { printf("*"); } else { printf(" "); } } printf("\n"); } QRcode_free(qrcode); } return 0; } ``` 上面的代码会将字符串"hello world"转换成二维码数据,并输出到终端。 3. 将二维码数据转换成bmp图片 将二维码数据转换成bmp图片可以使用图形库,如libgd。以下是一个简单的示例: ``` #include <stdio.h> #include <string.h> #include <qrencode.h> #include <gd.h> int main(int argc, char **argv) { QRcode *qrcode = QRcode_encodeString("hello world", 0, QR_ECLEVEL_L, QR_MODE_8, 1); if (qrcode) { int width = qrcode->width; gdImagePtr image = gdImageCreate(width, width); int black = gdImageColorAllocate(image, 0, 0, 0); int white = gdImageColorAllocate(image, 255, 255, 255); for (int y = 0; y < width; y++) { for (int x = 0; x < width; x++) { if (qrcode->data[y*width + x] & 1) { gdImageSetPixel(image, x, y, black); } else { gdImageSetPixel(image, x, y, white); } } } FILE *fp = fopen("qrcode.bmp", "wb"); gdImageBmp(image, fp, 0); fclose(fp); gdImageDestroy(image); QRcode_free(qrcode); } return 0; } ``` 上面的代码会将二维码数据转换成bmp图片,并保存到文件"qrcode.bmp"中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值