test code

#ifndef FT_EXPORT

#ifdef __cplusplus
#define FT_EXPORT( x )  extern "C"  x
#else
#define FT_EXPORT( x )  extern  x
#endif
</pre><pre code_snippet_id="1956391" snippet_file_name="blog_20161030_1_7856010" name="code" class="cpp">
#ifdef DLL_EXPORT
#undef DLL_EXPORT
#define DLL_EXPORT  __declspec(dllexport)
#else
#define DLL_EXPORT  __declspec(dllimport)
#endif /* !DLL_EXPORT */




#ifndef FT_EXPORT


#ifdef __cplusplus
#define FT_EXPORT( x )  extern "C"  DLL_EXPORT  x
#else
#define FT_EXPORT( x )  extern  DLL_EXPORT  x
#endif


#endif /* !FT_EXPORT */
</pre><pre code_snippet_id="1956391" snippet_file_name="blog_20161030_1_7856010" name="code" class="cpp">//====================================================================
//====================================================================
//
// 文件: CvxText.h
//
// 说明: OpenCV汉字输出
//
// 时间: 
//
// 作者: chaishushan#gmail.com
//
//====================================================================
//====================================================================


#ifndef OPENCV_CVX_TEXT_2007_08_31_H
#define OPENCV_CVX_TEXT_2007_08_31_H


/**
* \file CvxText.h
* \brief OpenCV汉字输出接口
*
* 实现了汉字输出功能。
*/


#include <ft2build.h>
#include FT_FREETYPE_H


#include<opencv2/opencv.hpp>


/**
* \class CvxText
* \brief OpenCV中输出汉字
*
* OpenCV中输出汉字。字库提取采用了开源的FreeFype库。由于FreeFype是
* GPL版权发布的库,和OpenCV版权并不一致,因此目前还没有合并到OpenCV
* 扩展库中。
*
* 显示汉字的时候需要一个汉字字库文件,字库文件系统一般都自带了。
* 这里采用的是一个开源的字库:“文泉驿正黑体”。
*
* 关于"OpenCV扩展库"的细节请访问
* http://code.google.com/p/opencv-extension-library/
*
* 关于FreeType的细节请访问
* http://www.freetype.org/
*/




class CvxText
{
<span style="white-space:pre">	</span>// 禁止copy


<span style="white-space:pre">	</span>CvxText& operator=(const CvxText&);


<span style="white-space:pre">	</span>//================================================================
<span style="white-space:pre">	</span>//================================================================


public:


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span>* 装载字库文件
<span style="white-space:pre">	</span>*/


<span style="white-space:pre">	</span>CvxText(const char *freeType);
<span style="white-space:pre">	</span>virtual ~CvxText();


<span style="white-space:pre">	</span>//================================================================
<span style="white-space:pre">	</span>//================================================================


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span>* 获取字体。目前有些参数尚不支持。
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \param font        字体类型, 目前不支持
<span style="white-space:pre">	</span>* \param size        字体大小/空白比例/间隔比例/旋转角度
<span style="white-space:pre">	</span>* \param underline   下画线
<span style="white-space:pre">	</span>* \param diaphaneity 透明度
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \sa setFont, restoreFont
<span style="white-space:pre">	</span>*/


<span style="white-space:pre">	</span>void getFont(int *type,
<span style="white-space:pre">		</span>CvScalar *size = NULL, bool *underline = NULL, float *diaphaneity = NULL);


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span>* 设置字体。目前有些参数尚不支持。
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \param font        字体类型, 目前不支持
<span style="white-space:pre">	</span>* \param size        字体大小/空白比例/间隔比例/旋转角度
<span style="white-space:pre">	</span>* \param underline   下画线
<span style="white-space:pre">	</span>* \param diaphaneity 透明度
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \sa getFont, restoreFont
<span style="white-space:pre">	</span>*/


<span style="white-space:pre">	</span>void setFont(int *type,
<span style="white-space:pre">		</span>CvScalar *size = NULL, bool *underline = NULL, float *diaphaneity = NULL);


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span>* 恢复原始的字体设置。
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \sa getFont, setFont
<span style="white-space:pre">	</span>*/


<span style="white-space:pre">	</span>void restoreFont();


<span style="white-space:pre">	</span>//================================================================
<span style="white-space:pre">	</span>//================================================================


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span>* 输出汉字(颜色默认为黑色)。遇到不能输出的字符将停止。
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \param img  输出的影象
<span style="white-space:pre">	</span>* \param text 文本内容
<span style="white-space:pre">	</span>* \param pos  文本位置
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \return 返回成功输出的字符长度,失败返回-1。
<span style="white-space:pre">	</span>*/


<span style="white-space:pre">	</span>int putText(IplImage *img, const char    *text, CvPoint pos);


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span>* 输出汉字(颜色默认为黑色)。遇到不能输出的字符将停止。
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \param img  输出的影象
<span style="white-space:pre">	</span>* \param text 文本内容
<span style="white-space:pre">	</span>* \param pos  文本位置
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \return 返回成功输出的字符长度,失败返回-1。
<span style="white-space:pre">	</span>*/


<span style="white-space:pre">	</span>int putText(IplImage *img, const wchar_t *text, CvPoint pos);


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span>* 输出汉字。遇到不能输出的字符将停止。
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \param img   输出的影象
<span style="white-space:pre">	</span>* \param text  文本内容
<span style="white-space:pre">	</span>* \param pos   文本位置
<span style="white-space:pre">	</span>* \param color 文本颜色
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \return 返回成功输出的字符长度,失败返回-1。
<span style="white-space:pre">	</span>*/


<span style="white-space:pre">	</span>int putText(IplImage *img, const char    *text, CvPoint pos, CvScalar color);


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span>* 输出汉字。遇到不能输出的字符将停止。
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \param img   输出的影象
<span style="white-space:pre">	</span>* \param text  文本内容
<span style="white-space:pre">	</span>* \param pos   文本位置
<span style="white-space:pre">	</span>* \param color 文本颜色
<span style="white-space:pre">	</span>*
<span style="white-space:pre">	</span>* \return 返回成功输出的字符长度,失败返回-1。
<span style="white-space:pre">	</span>*/
<span style="white-space:pre">	</span>int putText(IplImage *img, const wchar_t *text, CvPoint pos, CvScalar color);


<span style="white-space:pre">	</span>//================================================================
<span style="white-space:pre">	</span>//================================================================


private:


<span style="white-space:pre">	</span>// 输出当前字符, 更新m_pos位置


<span style="white-space:pre">	</span>void putWChar(IplImage *img, wchar_t wc, CvPoint &pos, CvScalar color);


<span style="white-space:pre">	</span>//================================================================
<span style="white-space:pre">	</span>//================================================================


private:


<span style="white-space:pre">	</span>FT_Library   m_library;   // 字库
<span style="white-space:pre">	</span>FT_Face      m_face;      // 字体


<span style="white-space:pre">	</span>//================================================================
<span style="white-space:pre">	</span>//================================================================


<span style="white-space:pre">	</span>// 默认的字体输出参数


<span style="white-space:pre">	</span>int         m_fontType;
<span style="white-space:pre">	</span>CvScalar   m_fontSize;
<span style="white-space:pre">	</span>bool      m_fontUnderline;
<span style="white-space:pre">	</span>float      m_fontDiaphaneity;


<span style="white-space:pre">	</span>//================================================================
<span style="white-space:pre">	</span>//================================================================
};


#endif // OPENCV_CVX_TEXT_2007_08_31_H
</pre><pre code_snippet_id="1956391" snippet_file_name="blog_20161030_1_7856010" name="code" class="cpp">#include <wchar.h>
#include <assert.h>
#include <locale.h>
#include <ctype.h>


#include "CvxText.h"


//====================================================================
//====================================================================


// 打开字库


CvxText::CvxText(const char *freeType)
{
<span style="white-space:pre">	</span>assert(freeType != NULL);


<span style="white-space:pre">	</span>// 打开字库文件, 创建一个字体


<span style="white-space:pre">	</span>if (FT_Init_FreeType(&m_library)) throw;
<span style="white-space:pre">	</span>if (FT_New_Face(m_library, freeType, 0, &m_face)) throw;


<span style="white-space:pre">	</span>// 设置字体输出参数


<span style="white-space:pre">	</span>restoreFont();


<span style="white-space:pre">	</span>// 设置C语言的字符集环境


<span style="white-space:pre">	</span>setlocale(LC_ALL, "");
}


// 释放FreeType资源


CvxText::~CvxText()
{
<span style="white-space:pre">	</span>FT_Done_Face(m_face);
<span style="white-space:pre">	</span>FT_Done_FreeType(m_library);
}


// 设置字体参数:
//
// font         - 字体类型, 目前不支持
// size         - 字体大小/空白比例/间隔比例/旋转角度
// underline   - 下画线
// diaphaneity   - 透明度


void CvxText::getFont(int *type, CvScalar *size, bool *underline, float *diaphaneity)
{
<span style="white-space:pre">	</span>if (type) *type = m_fontType;
<span style="white-space:pre">	</span>if (size) *size = m_fontSize;
<span style="white-space:pre">	</span>if (underline) *underline = m_fontUnderline;
<span style="white-space:pre">	</span>if (diaphaneity) *diaphaneity = m_fontDiaphaneity;
}


void CvxText::setFont(int *type, CvScalar *size, bool *underline, float *diaphaneity)
{
<span style="white-space:pre">	</span>// 参数合法性检查


<span style="white-space:pre">	</span>if (type)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>if (type >= 0) m_fontType = *type;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>if (size)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>m_fontSize.val[0] = fabs(size->val[0]);
<span style="white-space:pre">		</span>m_fontSize.val[1] = fabs(size->val[1]);
<span style="white-space:pre">		</span>m_fontSize.val[2] = fabs(size->val[2]);
<span style="white-space:pre">		</span>m_fontSize.val[3] = fabs(size->val[3]);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>if (underline)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>m_fontUnderline = *underline;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>if (diaphaneity)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>m_fontDiaphaneity = *diaphaneity;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>//FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
}


// 恢复原始的字体设置


void CvxText::restoreFont()
{
<span style="white-space:pre">	</span>m_fontType = 0;            // 字体类型(不支持)


<span style="white-space:pre">	</span>m_fontSize.val[0] = 20;      // 字体大小
<span style="white-space:pre">	</span>m_fontSize.val[1] = 0.5;   // 空白字符大小比例
<span style="white-space:pre">	</span>m_fontSize.val[2] = 0.1;   // 间隔大小比例
<span style="white-space:pre">	</span>m_fontSize.val[3] = 0;      // 旋转角度(不支持)


<span style="white-space:pre">	</span>m_fontUnderline = false;   // 下画线(不支持)


<span style="white-space:pre">	</span>m_fontDiaphaneity = 1.0;   // 色彩比例(可产生透明效果)


<span style="white-space:pre">	</span>// 设置字符大小


<span style="white-space:pre">	</span>FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
}


// 输出函数(颜色默认为黑色)


int CvxText::putText(IplImage *img, const char    *text, CvPoint pos)
{
<span style="white-space:pre">	</span>return putText(img, text, pos, CV_RGB(255, 255, 255));
}
int CvxText::putText(IplImage *img, const wchar_t *text, CvPoint pos)
{
<span style="white-space:pre">	</span>return putText(img, text, pos, CV_RGB(255, 255, 255));
}


//


int CvxText::putText(IplImage *img, const char    *text, CvPoint pos, CvScalar color)
{
<span style="white-space:pre">	</span>if (img == NULL) return -1;
<span style="white-space:pre">	</span>if (text == NULL) return -1;


<span style="white-space:pre">	</span>//


<span style="white-space:pre">	</span>int i;
<span style="white-space:pre">	</span>for (i = 0; text[i] != '\0'; ++i)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>wchar_t wc = text[i];


<span style="white-space:pre">		</span>// 解析双字节符号


<span style="white-space:pre">		</span>if (!isascii(wc)) mbtowc(&wc, &text[i++], 2);


<span style="white-space:pre">		</span>// 输出当前的字符


<span style="white-space:pre">		</span>putWChar(img, wc, pos, color);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return i;
}
int CvxText::putText(IplImage *img, const wchar_t *text, CvPoint pos, CvScalar color)
{
<span style="white-space:pre">	</span>if (img == NULL) return -1;
<span style="white-space:pre">	</span>if (text == NULL) return -1;


<span style="white-space:pre">	</span>//


<span style="white-space:pre">	</span>int i;
<span style="white-space:pre">	</span>for (i = 0; text[i] != '\0'; ++i)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>// 输出当前的字符


<span style="white-space:pre">		</span>putWChar(img, text[i], pos, color);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return i;
}


// 输出当前字符, 更新m_pos位置


void CvxText::putWChar(IplImage *img, wchar_t wc, CvPoint &pos, CvScalar color)
{
<span style="white-space:pre">	</span>// 根据unicode生成字体的二值位图


<span style="white-space:pre">	</span>FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);
<span style="white-space:pre">	</span>FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
<span style="white-space:pre">	</span>FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_MONO);


<span style="white-space:pre">	</span>//


<span style="white-space:pre">	</span>FT_GlyphSlot slot = m_face->glyph;


<span style="white-space:pre">	</span>// 行列数


<span style="white-space:pre">	</span>int rows = slot->bitmap.rows;
<span style="white-space:pre">	</span>int cols = slot->bitmap.width;


<span style="white-space:pre">	</span>//


<span style="white-space:pre">	</span>for (int i = 0; i < rows; ++i)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>for (int j = 0; j < cols; ++j)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>int off = ((img->origin == 0) ? i : (rows - 1 - i))
<span style="white-space:pre">				</span>* slot->bitmap.pitch + j / 8;


<span style="white-space:pre">			</span>if (slot->bitmap.buffer[off] & (0xC0 >> (j % 8)))
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>int r = (img->origin == 0) ? pos.y - (rows - 1 - i) : pos.y + i;;
<span style="white-space:pre">				</span>int c = pos.x + j;


<span style="white-space:pre">				</span>if (r >= 0 && r < img->height
<span style="white-space:pre">					</span>&& c >= 0 && c < img->width)
<span style="white-space:pre">				</span>{
<span style="white-space:pre">					</span>CvScalar scalar = cvGet2D(img, r, c);


<span style="white-space:pre">					</span>// 进行色彩融合


<span style="white-space:pre">					</span>float p = m_fontDiaphaneity;
<span style="white-space:pre">					</span>for (int k = 0; k < 4; ++k)
<span style="white-space:pre">					</span>{
<span style="white-space:pre">						</span>scalar.val[k] = scalar.val[k] * (1 - p) + color.val[k] * p;
<span style="white-space:pre">					</span>}


<span style="white-space:pre">					</span>cvSet2D(img, r, c, scalar);
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>} // end for
<span style="white-space:pre">	</span>} // end for


<span style="white-space:pre">	</span>// 修改下一个字的输出位置


<span style="white-space:pre">	</span>double space = m_fontSize.val[0] * m_fontSize.val[1];
<span style="white-space:pre">	</span>double sep = m_fontSize.val[0] * m_fontSize.val[2];


<span style="white-space:pre">	</span>pos.x += (int)((cols ? cols : space) + sep);
}
#include <opencv/highgui.h>
#include <assert.h>
#include "CvxText.h"
using namespace cv;


int main(int argc, char *argv[])
{
<span style="white-space:pre">	</span>// 打开一幅
<span style="white-space:pre">	</span>IplImage *img = cvLoadImage("lena.jpg");
<span style="white-space:pre">	</span>// 输出汉字
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>CvxText text("C:\\Windows\\Fonts\\simhei.ttf"); // "zenhei.ttf"为黑体常规
<span style="white-space:pre">		</span>//CvxText text("simhei.ttf"); // "zenhei.ttf"为黑体常规
<span style="white-space:pre">		</span>const char *msg = "在OpenCV中输出汉字!";
<span style="white-space:pre">		</span>float p = 0.5;
<span style="white-space:pre">		</span>text.setFont(NULL, NULL, NULL, &p);   // 透明处理
<span style="white-space:pre">		</span>text.putText(img, msg, cvPoint(100, 150), CV_RGB(255, 0, 0));
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>// 定义窗口,并显示影象
<span style="white-space:pre">	</span>cvShowImage("test", img); cvWaitKey(-1);
<span style="white-space:pre">	</span>cvReleaseImage(&img);
<span style="white-space:pre">	</span>return 0;
}
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值