文字转图片

本文介绍libjpeg库的下载与安装步骤,并通过示例代码演示如何使用GD库在图片上绘制UTF8格式的中文字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装

第一步下载libjpeg库

下载地址http://www.ijg.org/

下载jpegsrc.v9a.tar.gz这个文件

第二步

解压这个文件

第三步

打开终端,切换到解压的目录

执行

./configure --prefix=/usr/local/--enable-shared --enable-static

也可以根据需要添加其他库,如下:

 --with-zlib[=DIR]       Supportzlib (optionally in DIR)

 --with-png[=DIR]        Supportpng (optionally in DIR)

 --with-freetype[=DIR]   Supportfreetype (optionally in DIR)

 --with-fontconfig[=DIR] Support fontconfig (optionally in DIR)

 --with-jpeg[=DIR]       Supportjpeg (optionally in DIR)

 --with-liq[=DIR]        Supportliq (optionally in DIR)

 --with-xpm[=DIR]        Supportxpm (optionally in DIR)

 --with-tiff[=DIR]       Supporttiff (optionally in DIR)

 --with-webp[=DIR]       Supportwebp (optionally in DIR)

第四步

执行

make

可能需要几分钟。

执行

make install

 

代码:

#include "gd.h"

#include <string.h>

int main(int argc, char **argv)

{

   gdImagePtr im;

   int black;

   int white;

   int brect[8];

   int x, y;

   char *err;

   FILE *fpPng = NULL;

 

   char *s = "中国人"; /* String to draw. */

   double sz = 100.;

   char *f = "/usr/share/fonts/wqy-microhei.ttc";   //根据需要下载

   /* obtain brect so that we can size the image */

   err = gdImageStringFT(NULL,&brect[0],0,f,sz,0.,0,0,s);

   if (err) {

       fprintf(stderr, "%s", err);

       return 1;

    }

 

   /* create an image big enough for the string plus a little whitespace */

    x= brect[2]-brect[6] + 6;

    y= brect[3]-brect[7] + 6;

   im = gdImageCreate(x,y);

 

   /* Background color (first allocated) */

   white = gdImageColorResolve(im, 255, 255, 255);

   black = gdImageColorResolve(im, 0, 0, 0);

 

   /* render the string, offset origin to center string*/

   /* note that we use top-left coordinate for adjustment

    *since gd origin is in top-left with y increasing downwards. */

    x= 3 - brect[6];

    y= 3 - brect[7];

   err = gdImageStringFT(im,&brect[0],black,f,sz,0.0,x,y,s);

   if (err) {

       fprintf(stderr, "%s",err);

       return 1;

    }

   fpPng = fopen("ft.png", "wb");

   

   /* Write img to fpPng */

   gdImagePng(im, fpPng);

   fclose(fpPng);

   /* Destroy it */

   gdImageDestroy(im);

   return 0;

}

问题:

1中文乱码。

中文要先改成UTF8格式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山西茄子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值