#include <wchar.h>
#include <assert.h>
#include <locale.h>
#include <ctype.h>
#include "CvxText.h"
//====================================================================
//====================================================================
// 打开字库
CvxText::CvxText(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资源
CvxText::~CvxText()
{
FT_Done_Face (m_face);
FT_Done_FreeType(m_library);
}
// 设置字体参数:
//
// font - 字体类型, 目前不支持
// size - 字体大小/空白比例/间隔比例/旋转角度
// underline - 下画线
// diaphaneity - 透明度
void CvxText::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 CvxText::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;
}
}
// 恢复原始的字体设置
void CvxText::restoreFont()
{
m_fontType = 0; // 字体类型(不支持)
m_fontSize.val[0] = 20; // 字体大小
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 CvxText::putText(IplImage *img, const char *text, CvPoint pos)
{
return putText(img, text, pos, CV_RGB(255,255,255));
}
int CvxText::putText(IplImage *img, const wchar_t *text, CvPoint pos)
{
return putText(img, text, pos, CV_RGB(255,255,255));
}
//
int CvxText::putText(IplImage *img, const char *text, CvPoint pos, CvScalar color)
{
if(img == NULL) 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(img, wc, pos, color);
}
return i;
}
int CvxText::putText(IplImage *img, const wchar_t *text, CvPoint pos, CvScalar color)
{
if(img == NULL) return -1;
if(text == NULL) return -1;
//
int i;
for(i = 0; text[i] != '\0'; ++i)
{
// 输出当前的字符
putWChar(img, text[i], pos, color);
}
return i;
}
// 输出当前字符, 更新m_pos位置
void CvxText::putWChar(IplImage *img, wchar_t wc, CvPoint &pos, CvScalar color)
{
// 根据unicode生成字体的二值位图
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 < rows; ++i)
{
for(int j = 0; j < cols; ++j)
{
int off = ((img->origin==0)? i: (rows-1-i))
* slot->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 < img->height
&& c >= 0 && c < img->width)
{
CvScalar scalar = cvGet2D(img, r, c);
// 进行色彩融合
float p = m_fontDiaphaneity;
for(int k = 0; k < 4; ++k)
{
scalar.val[k] = scalar.val[k]*(1-p) + color.val[k]*p;
}
cvSet2D(img, r, c, scalar);
}
}
} // end for
} // end for
// 修改下一个字的输出位置
double space = m_fontSize.val[0]*m_fontSize.val[1];
double sep = m_fontSize.val[0]*m_fontSize.val[2];
pos.x += (int)((cols? cols: space) + sep);
}
没有合适的资源?快使用搜索试试~ 我知道了~
opencv库中将中文字写入图像并保存,对使用opencv做图像处理又需要在图像上写中文信息很有用处
共108个文件
h:81个
dll:7个
user:2个
5星 · 超过95%的资源 需积分: 33 46 下载量 113 浏览量
2017-07-06
10:21:25
上传
评论 4
收藏 11.95MB RAR 举报
温馨提示
opencv库中将中文字写入图像并保存,对使用opencv做图像处理又需要在图像上写中文信息很有用处
资源推荐
资源详情
资源评论
收起资源包目录
opencv库中将中文字写入图像并保存,对使用opencv做图像处理又需要在图像上写中文信息很有用处 (108个子文件)
CvxText.cpp 4KB
main.cpp 640B
mt.dep 67B
opencv_ffmpeg200.dll 4.96MB
cxcore200.dll 1.97MB
cv200.dll 1.57MB
cvaux200.dll 930KB
highgui200.dll 756KB
ml200.dll 421KB
cxts200.dll 196KB
CvxTextOut_demo.dsp 5KB
CvxTextOut_demo.dsw 536B
CvxTextOut_demo.exe 335KB
freetype.h 206KB
tttypes.h 85KB
ftimage.h 76KB
ftcache.h 58KB
ttnameid.h 54KB
sfnt.h 45KB
ftoption.h 41KB
ftobjs.h 40KB
tttables.h 39KB
ftglyph.h 35KB
psaux.h 35KB
fttypes.h 34KB
ftoutln.h 31KB
ftheader.h 23KB
ftstream.h 23KB
ftmm.h 22KB
ftmodapi.h 21KB
pshints.h 21KB
ftstroke.h 19KB
ftmac.h 17KB
t1tables.h 16KB
ftlist.h 16KB
ftmemory.h 14KB
ftserv.h 14KB
ftconfig.h 13KB
autohint.h 12KB
ftbitmap.h 12KB
ftgxval.h 12KB
fterrdef.h 12KB
ftrfork.h 11KB
ftdriver.h 11KB
ftdebug.h 11KB
ftrender.h 11KB
ftincrem.h 10KB
ftsystem.h 10KB
ftsnames.h 10KB
ftwinfnt.h 9KB
t1types.h 9KB
ftsizes.h 9KB
fterrors.h 9KB
fttrigon.h 8KB
ftstdlib.h 8KB
ftcalc.h 8KB
ftmoderr.h 7KB
ftvalid.h 7KB
ftchapters.h 7KB
ftotval.h 7KB
ftgloadr.h 6KB
ftpfr.h 6KB
ftbdf.h 6KB
ftlcdfil.h 6KB
CvxText.h 5KB
fttrace.h 5KB
ftbbox.h 5KB
tttags.h 4KB
ftxf86.h 4KB
ftgzip.h 4KB
ftlzw.h 4KB
svpscmap.h 4KB
ftgasp.h 4KB
svttcmap.h 3KB
ftsynth.h 3KB
internal.h 3KB
svgxval.h 3KB
svmm.h 3KB
svsfnt.h 2KB
svpfr.h 2KB
ttunpat.h 2KB
ft2build.h 2KB
svgldict.h 2KB
svxf86nm.h 2KB
svpsinfo.h 2KB
svpostnm.h 2KB
svbdf.h 2KB
svotval.h 2KB
svkern.h 2KB
svwinfnt.h 2KB
svtteng.h 2KB
svttglyf.h 2KB
pcftypes.h 2KB
ftmodule.h 1022B
BuildLog.htm 7KB
vc90.idb 467KB
lena.jpg 90KB
freetype235.lib 835KB
CvxTextOut_demo.exe.intermediate.manifest 851B
CvxTextOut_demo.ncb 10.92MB
共 108 条
- 1
- 2
资源评论
- mailzst12019-07-08好东西,值得学习。
zhangpingy123
- 粉丝: 1
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功