终极目标:利用freetype开源字体库,实现在图片上书写一行字体大小变化的诗句(比如首字体增大)
目录
(一) 树莓派3b+freetype2.4.101. 下载2. 解压及配置3. 添加字体库4. 一个简单的Demo5.freetype2+opencv3在图片上绘制中文
(二) VS2017+freetype2.9.11. 下载2. 解压及配置3. 一个简单的Demo
(一) 树莓派3b+freetype2.4.10
1. 下载
我是在PC上下载好Linux版本,用teamviewer传到派上的;也可以直接在pi上下载。
官网链接:https://sourceforge.net/projects/freetype/files/freetype2/
注意:Linux系统下需要的版本为:freetype-2.*.*.tar.br2;版本自选。 我选择的是2.4.10,可任选 用teamviewer直接传文件到/home/pi目录下: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181216115340996.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N1bmd1ZGFmYQ==,size_16,color_FFFFFF,t_70)
2. 解压及配置
进入命令模式解压(对应版本):
tar -xjf freetype-2.4.10.tar.bz2
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181212204441690.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N1bmd1ZGFmYQ==,size_16,color_FFFFFF,t_70)
进入安装包:
cd freetype-2.4.10/
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181212204514847.png)
配置:
./configure
配置完成如图: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181212204553838.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N1bmd1ZGFmYQ==,size_16,color_FFFFFF,t_70)
编译:(需要几分钟时间)
make
编译完成如图: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181212204634938.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N1bmd1ZGFmYQ==,size_16,color_FFFFFF,t_70)
直接将库安装到根目录/usr/local/里,所以需要加sudo:
sudo make install
完成如图: ![在这里插入图片描述](https://img-blog.csdnimg.cn/2018121220470670.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N1bmd1ZGFmYQ==,size_16,color_FFFFFF,t_70)
3. 添加字体库
查看本地字体库目录
树莓派:/usr/share/fonts
Windows:C:\Windows\Fonts
经查看,我的pi目录下没有宋体(simhei.ttf)、(simsun.ttc)的字体文件,我从pc上传了一份过去,注意填写的接收地址。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181216115445574.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2N1bmd1ZGFmYQ==,size_16,color_FFFFFF,t_70)
4. 一个简单的Demo
新建文件夹freetype_test将刚才传输的simsun.ttc字体库样式复制到freetype_test文件夹下在freetype_test文件夹下创建 test.cpp编写代码(这里的源码我是参照网上的,先直接拖进去试试水)
#include
#include
#include
#include
#include FT_FREETYPE_H
#define WIDTH 80
#define HEIGHT 80
/* origin is the upper left corner */
unsigned char image[HEIGHT][WIDTH];
/* Replace this function with something useful. */
void
draw_bitmap( FT_Bitmap* bitmap,
FT_Int x,
FT_Int y)
{
FT_Int i, j, p, q;
FT_Int x_max = x + bitmap->width;
FT_Int y_max = y + bitmap->rows;
for ( i = x, p = 0; i width + p];
}
}
}
void
show_image( void )
{
int i, j;
for ( i = 0; i bitmap_top );
/* increment pen position */
pen.x += slot->advance.x;
pen.y += slot->advance.y;
}
show_image();
FT_Done_Face ( face );
FT_Done_FreeType( library );
return 0;
}
保存回到命令行界面编译
gcc -o test test.cpp -I/usr/local/include/freetype2/ -lfreetype -lm
运行
./test ./simsun.ttc cungu
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181212210343837.png)
结果 配置成功!
5.freetype2+opencv3在图片上绘制中文
参考链接:http://lib.csdn.net/article/opencv/53782?tdsourcetag=s_pcqq_aiomsg
新建freetype_test文件夹,把simhei.ttf字体库和图片lena.jpg复制进来,新建freetype_test.cpp程序 运行测试()
g++ -o freetype_test freetype_test.cpp `pkg-config --cflags --libs opencv` -I/usr/local/include/freetype2/ -lfreetype -lm
源码
#include
#include FT_FREETYPE_H
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
class CvText
{
//================================================================
//================================================================
public:
/**
* 装载字库文件
*/
CvText(const char *freeType);
virtual ~CvText();
//================================================================
//================================================================
/**
* 获取字体。目前有些参数尚不支持。
*
* \param font 字体类型, 目前不支持
* \param size 字体大小/空白比例/间隔比例/旋转角度
* \param underline 下画线
* \param diaphaneity 透明度
*
* \sa setFont, restoreFont
*/
void getFont(int *type,
CvScalar *size = NULL, bool *underline = NULL, float *diaphaneity = NULL);
/**
* 设置字体。目前有些参数尚不支持。
*
* \param font 字体类型, 目前不支持
* \param size 字体大小/空白比例/间隔比例/旋转角度
* \param underline 下画线
* \param diaphaneity 透明度
*
* \sa getFont, restoreFont
*/
void setFont(int *type,
CvScalar *size = NULL, bool *underline = NULL, float *diaphaneity = NULL);
/**
* 恢复原始的字体设置。
*
* \sa getFont, setFont
*/
void restoreFont();
//================================================================
//================================================================
/**
* 输出汉字(颜色默认为黑色)。遇到不能输出的字符将停止。
*
* \param img 输出的影象
* \param text 文本内容
* \param pos 文本位置
*
* \return 返回成功输出的字符长度,失败返回-1。
*/
int putText(cv::Mat &frame, const char *text, CvPoint pos);
/**
* 输出汉字(颜色默认为黑色)。遇到不能输出的字符将停止。
*
* \param img 输出的影象
* \param text 文本内容
* \param pos 文本位置
*
* \return 返回成功输出的字符长度,失败返回-1。
*/
int putText(cv::Mat &frame, const wchar_t *text, CvPoint pos);
/**
* 输出汉字。遇到不能输出的字符将停止。
*
* \param img 输出的影象
* \param text 文本内容
* \param pos 文本位置
* \param color 文本颜色
*
* \return 返回成功输出的字符长度,失败返回-1。
*/
int putText(cv::Mat &frame, const char *text, CvPoint pos, CvScalar color);
/**
* 输出汉字。遇到不能输出的字符将停止。
*
* \param img 输出的影象
* \param text 文本内容
* \param pos 文本位置
* \param color 文本颜色
*
* \return 返回成功输出的字符长度,失败返回-1。
*/
int putText(cv::Mat &frame, const wchar_t *text, CvPoint pos, CvScalar color);
//================================================================
//================================================================
private:
// 输出当前字符, 更新m_pos位置
void putWChar(cv::Mat &frame, wchar_t wc, CvPoint &pos, CvScalar color);
//================================================================
//================================================================
private:
FT_Library m_library; // 字库
FT_Face m_face; // 字体
//================================================================
//================================================================
// 默认的字体输出参数
int m_fontType;
CvScalar m_fontSize;
bool m_fontUnderline;
float m_fontDiaphaneity;
//================================================================
//================================================================
};
int main()
{
cv::Mat img = imread("lena.jpg");
CvText text("simhei.ttf");
const char *msg = "黑 体freetype";//字库要求输入中文时中间必须有一个间隔
float p = 0.5;
text.setFont(NULL, NULL, NULL, &p); // 透明处理
text.putText(img, msg, cvPoint(30, 30), CV_RGB(255, 255, 0));
imshow("freetype_test", img);
waitKey(-1);
return 0;
}
CvText::CvText(const char *freeType)
{
assert(freeType != NULL);
// 打开字库文件, 创建一个字体
if (FT_Init_FreeType(&m_library)) throw;
if (FT_New_Face(m_library, freeType, 0, &m_face)) throw;
// 设置字体输出参数
restoreFont();
// 设置C语言的字符集环境
setlocale(LC_ALL, "");
}
// 释放FreeType资源
CvText::~CvText()
{
FT_Done_Face(m_face);
FT_Done_FreeType(m_library);
}
// 设置字体参数:
//
// font - 字体类型, 目前不支持
// size - 字体大小/空白比例/间隔比例/旋转角度
// underline - 下画线
// diaphaneity - 透明度
void CvText::getFont(int *type, CvScalar *size, bool *underline, float *diaphaneity)
{
if (type) *type = m_fontType;
if (size) *size = m_fontSize;
if (underline) *underline = m_fontUnderline;
if (diaphaneity) *diaphaneity = m_fontDiaphaneity;
}
void CvText::setFont(int *type, CvScalar *size, bool *underline, float *diaphaneity)
{
// 参数合法性检查
if (type)
{
if (type >= 0) m_fontType = *type;
}
if (size)
{
m_fontSize.val[0] = fabs(size->val[0]);
m_fontSize.val[1] = fabs(size->val[1]);
m_fontSize.val[2] = fabs(size->val[2]);
m_fontSize.val[3] = fabs(size->val[3]);
}
if (underline)
{
m_fontUnderline = *underline;
}
if (diaphaneity)
{
m_fontDiaphaneity = *diaphaneity;
}
FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
}
// 恢复原始的字体设置
void CvText::restoreFont()
{
m_fontType = 0; // 字体类型(不支持)
m_fontSize.val[0] = 30; // 字体大小
m_fontSize.val[1] = 0.5; // 空白字符大小比例
m_fontSize.val[2] = 0.1; // 间隔大小比例
m_fontSize.val[3] = 0; // 旋转角度(不支持)
m_fontUnderline = false; // 下画线(不支持)
m_fontDiaphaneity = 1.0; // 色彩比例(可产生透明效果)
// 设置字符大小
FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
}
// 输出函数(颜色默认为黑色)
int CvText::putText(cv::Mat &frame, const char *text, CvPoint pos)
{
return putText(frame, text, pos, CV_RGB(255, 255, 255));
}
int CvText::putText(cv::Mat &frame, const wchar_t *text, CvPoint pos)
{
return putText(frame, text, pos, CV_RGB(255, 255, 255));
}
//
int CvText::putText(cv::Mat &frame, const char *text, CvPoint pos, CvScalar color)
{
if (frame.empty()) return -1;
if (text == NULL) return -1;
//
int i;
for (i = 0; text[i] != '\0'; ++i)
{
wchar_t wc = text[i];
// 解析双字节符号
if (!isascii(wc)) mbtowc(&wc, &text[i++], 2);
// 输出当前的字符
putWChar(frame, wc, pos, color);
}
return i;
}
int CvText::putText(cv::Mat &frame, const wchar_t *text, CvPoint pos, CvScalar color)
{
if (frame.empty()) return -1;
if (text == NULL) return -1;
//
int i;
for (i = 0; text[i] != '\0'; ++i)
{
// 输出当前的字符
putWChar(frame, text[i], pos, color);
}
return i;
}
// 输出当前字符, 更新m_pos位置
void CvText::putWChar(cv::Mat &frame, wchar_t wc, CvPoint &pos, CvScalar color)
{
// 根据unicode生成字体的二值位图
IplImage temp=(IplImage) frame;
IplImage* img = &temp;
FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);
FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_MONO);
//
FT_GlyphSlot slot = m_face->glyph;
// 行列数
int rows = slot->bitmap.rows;
int cols = slot->bitmap.width;
//
for (int i = 0; i bitmap.pitch + j / 8;
if (slot->bitmap.buffer[off] & (0xC0 >> (j % 8)))
{
int r = (img->origin == 0) ? pos.y - (rows - 1 - i) : pos.y + i;;
int c = pos.x + j;
if (r >= 0 && r height
&& c >= 0 && c width)
{
CvScalar scalar = cvGet2D(img, r, c);
// 进行色彩融合
float p = m_fontDiaphaneity;
for (int k = 0; k |