文字转图片

来源:互联网 发布:java violate关键字 编辑:程序博客网 时间:2024/06/04 18:51

安装

第一步下载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格式。

0 0
原创粉丝点击