Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2609037
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5921
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: iOS平台

2015-11-16 15:34:43

ImageMagick--往图片上写文字
http://blog.csdn.net/txgc0/article/details/14231283

为了在文件上写文字,话费了一天的时间,都快崩溃了,其实可以有两种写法:一种是自己实现,对指定的点进行修改像素值,一种是直接用库中的函数进行写文字;当然所有人都希望用库中的函数,既方便又好用,但是事情的发展总是不如人意,我以为我直接写一个image.annotate("abcok",Geometry("100x100"));就能实现功能,谁知道总是运行错误,调了大半天,处于崩溃边缘,于是,在Magick群里问了一下,果然有牛人给我解答,不过刚开始说的我不懂,什么是字体路径?还有就是一大串代码,一大堆英文文档。。。。好了,吐槽结束:

说重点:最重要的一个地方就是程序中:image.font("./font/n021003l.pfb");这句话。就是当时我不懂的指定字体路径。据我理解,这算是一个文字库,指定后,会把要写的文字在这里边找到对应的编码,然后显示到图片上,一般在下载的源码中,会有一个文件夹fonts(font),里边就是字体的模板,,我是把它拷贝到我的运行目录下,把这个路径指向里边的一个字体.pfb的文件。然后就可以对往图片上写文字了。



  1. // magick_1.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. //#pragma comment(lib,"ws2_32.lib")  
  10. using namespace std;   
  11. using namespace Magick;   
  12. #include   
  13. #include   
  14.   
  15. int main(int argc,char **argv)   
  16. {   
  17.   InitializeMagick(*argv);  
  18.   Image image( "test.jpg" );   
  19.   Blob blob;  
  20.   image.strokeColor("red"); // Outline color   
  21.     image.strokeWidth(1);//设置画线的线宽  
  1.   image.fontPointsize(20);//设置字体的大小  
  2. image.font("./font/n021003l.pfb");//指定字体的路径  
  3. image.annotate("abcok",Geometry("100x100+100+100"));  
  4. image.annotate("SouthEastGravity",SouthEastGravity);  
  5. image.annotate("CenterGravity",CenterGravity);  
  6. image.write("test_result.jpg");  
  7.   
  8. return 0;   
  1.   

下边代码中实现的那个函数还没搞明白呢。

  1. // magick_1.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. //#pragma comment(lib,"ws2_32.lib")  
  10. using namespace std;   
  11. using namespace Magick;   
  12. #include   
  13. #include   
  14.   
  15. int main(int argc,char **argv)   
  16. {   
  17.   InitializeMagick(*argv);  
  18.   Image image( "test.jpg" );   
  19.   Blob blob;  
  20.   image.strokeColor("red"); // Outline color   
  21.     image.strokeWidth(1);//设置画线的线宽  
  22.     image.fontPointsize(20);//设置字体的大小  
  23.   image.font("./font/n022024l.pfb");//指定字体的路径  
  24.     image.annotate("abcok",Geometry("100x100+100+100"));  
  25.   
  26.   TypeMetric *ty;  
  27.   ty=new TypeMetric;  
  28.   
  29.   image.fontTypeMetrics("abcok",ty);  
  30.   printf("ascent:%f\n",ty->ascent());  
  31.   printf("descent:%f\n",ty->descent());  
  32.   printf("maxHorizontalAdvance:%f\n",ty->maxHorizontalAdvance());  
  33.   printf("textHeight:%f\n",ty->textHeight());  
  34.   printf("textWidth:%f\n",ty->textWidth());  
  35.   char buf[100];  
  36.   gets(buf);  
  37.   //image.annotate("abcok",Geometry("100x100+100+100"));  
  38.   //image.annotate("SouthEastGravity",SouthEastGravity);  
  39.   //image.annotate("CenterGravity",CenterGravity);  
  40.     
  41.   image.write("test_result.jpg");  
  42.   
  43.   delete ty;  
  44.   ty=NULL;  
  45.   
  46.   return 0;   
  47. }  



阅读(1699) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~