Freetypeコンパイル画像に漢字を表示

17457 ワード

1:freetypeソースコードをダウンロード:http://download.savannah.gnu.org/releases/freetype/おすすめダウンロード2.6以上のバージョンFreetype 编译 图片上显示汉字_第1张图片
2:解凍後C:Users95Desktopfreetype-2.7buildswindowsvc 2010パスでfreetypeを開く.slnはVS 2013を使用してコンパイルFreetype 编译 图片上显示汉字_第2张图片 3を開くことができる:release x 64バージョンのコンパイルが安く完了すると、C:Users95Desktopfreetype-2.7objsvc 2010x 64パスの下でfreetype 27を生成する.libフォルダを新規作成しincludeとfreetype 27をlibが過去をコピーすると、プロパティファイル(Opencvと同様)Freetype 编译 图片上显示汉字_第3张图片を構成してプロジェクトfreetype_を新規作成できます.test.slnはCvxTextを追加する.hとCvxText.cppテスト関数を追加すればいい
CvxText.h
// CvxText.h  
#pragma once  

#ifndef OPENCV_CVX_TEXT_2007_08_31_H  
#define OPENCV_CVX_TEXT_2007_08_31_H  

#include   
#include FT_FREETYPE_H  

#include  

class CvxText
{
    CvxText& operator=(const CvxText&);

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

public:

    CvxText(const char *freeType);
    virtual ~CvxText();


    void getFont(int *type,
        CvScalar *size = NULL, bool *underline = NULL, float *diaphaneity = NULL);

    void setFont(int *type,
        CvScalar *size = NULL, bool *underline = NULL, float *diaphaneity = NULL);


    void restoreFont();

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

    int putText(IplImage *img, const char    *text, CvPoint pos);

    int putText(IplImage *img, const wchar_t *text, CvPoint pos);

    int putText(IplImage *img, const char    *text, CvPoint pos, CvScalar color);

    int putText(IplImage *img, const wchar_t *text, CvPoint pos, CvScalar color);
private:

    void putWChar(IplImage *img, wchar_t wc, CvPoint &pos, CvScalar color);

private:

    FT_Library   m_library;   //  Á? a  
    FT_Face      m_face;      //  Á? ¬?  


    int         m_fontType;
    CvScalar   m_fontSize;
    bool      m_fontUnderline;
    float      m_fontDiaphaneity;

    //================================================================  
    //================================================================  
};

#endif // OPENCV_CVX_TEXT_2007_08_31_H  

CvxText.cpp
#include "stdafx.h"
#include   
#include   
#include   
#include   

#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;
    }
    FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0);
}

//            

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);
}

main関数
    system("color 6F");
    Mat image = imread("1.jpg");
    //    
    imshow("  ", image);
    cv::waitKey(1);
    IplImage *img = cvLoadImage("1.jpg");
    {
        CvxText text("STZHONGS.TTF");
        const char *msg = "     !";
        float p = 0.5;
        text.setFont(NULL, NULL, NULL, &p);
        text.putText(img, msg, cvPoint(100, 150), CV_RGB(255, 0, 0));
    }
    cvShowImage("test", img); 
    cvWaitKey(0);
    cvReleaseImage(&img);
    return 0;