当前位置: 首页 > 工具软件 > Magick++ > 使用案例 >

Magick++绘制中文

逑衡
2023-12-01
//============================================================================
// Name        : imagick.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <Magick++.h>
using namespace std;

#define SE_MAGICK_STD
int main(int argc, char ** argv) {
	try {
		Magick::InitializeMagick(*argv);
#if defined(SE_MAGICK_STD)
		std::list<Magick::Drawable> draws;
		draws.push_back(Magick::DrawableFont("/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf")); /*设置字体*/
		draws.push_back(Magick::DrawablePointSize(72)); /*设置字体大小*/
		draws.push_back(Magick::DrawableStrokeColor(Magick::ColorRGB (255,0,0)));/*边框颜色*/
		draws.push_back(Magick::DrawableFillColor(Magick::Color("blue")));/*字体颜色*/

		std::list<Magick::Image> imgs;
		Magick::readImages(&imgs, "/home/km_black/soft/xxx.gif");
		Magick::coalesceImages(&imgs, imgs.begin(), imgs.end());

		for (std::list<Magick::Image>::iterator it = imgs.begin(); it != imgs.end(); ++it) {
			draws.push_back(Magick::DrawableText(0, 72, "ImageMagick中文测试"));
			it->draw(draws);
		}
		Magick::writeImages(imgs.begin(), imgs.end(), "/home/km_black/soft/1.gif");
#else
		std::string txt("ImageMagick中文测试");
		std::list<Magick::Drawable> draws;
		Magick::TypeMetric metric;

		Magick::Image img;
		img.read( "/home/km_black/soft/a.tif" );
		Magick::Geometry size =  img.size();/*获取图像大小*/
		img.font("/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf");  /*设置字体*/
		img.fontPointsize(24);/*设置字体大小*/
		img.fillColor(Magick::Color("red"));/*字体颜色*/
		img.fontTypeMetrics(txt,&metric);/*获取字体大小*/
		double x = size.width() - metric.textWidth();
		double y = size.height() ;
		size.height( size.height() - metric.textHeight() );
		draws.push_back(Magick::DrawableText(x,y,txt));
		img.draw(draws);
		img.magick("png");
		img.write("/home/km_black/soft/1.png");
#endif
	} catch (Magick::Exception &exp) {
		cout << exp.what() << endl; // prints !!!Hello World!!!
	}
	return 0;
}

注意:在window下绘制的字符串必须先转换成UTF-8格式,然后再绘制文字


linux下字体查看命令    identify -list font

查看支持中文的字体     fc-list :lang=zh-cn



中文字体安装
sudo apt-get  install fonts-droid
sudo apt-get  install fonts-arphic-ukai
sudo apt-get  install fonts-arphic-uming

 类似资料: