liunx下用C++使用freetype库在opencv上打中文字

1、/visualizer.cpp:11:10: fatal error: ft2build.h: 没有那个文件或目录
   11 | #include <ft2build.h>

freetype安装问题,要把文件拉到根目录,不然找不到文件

2、编译失败找不到定义

/usr/bin/ld: CMakeFiles/interactive_face_detection_demo.dir/visualizer.cpp.o: in function `putChineseText(cv::Mat&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, cv::Point_<int>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int, cv::Scalar_<double>&)':
visualizer.cpp:(.text+0x5ec): undefined reference to `FT_Init_FreeType'
/usr/bin/ld: visualizer.cpp:(.text+0x610): undefined reference to `FT_New_Face'
/usr/bin/ld: visualizer.cpp:(.text+0x631): undefined reference to `FT_Set_Pixel_Sizes'
/usr/bin/ld: visualizer.cpp:(.text+0x928): undefined reference to `FT_Done_Face'
/usr/bin/ld: visualizer.cpp:(.text+0x934): undefined reference to `FT_Done_FreeType'
/usr/bin/ld: visualizer.cpp:(.text+0xa49): undefined reference to `FT_Load_Char'
collect2: error: ld returned 1 exit status

添加链接库:

3、编译通过

4、贴一下文字代码(GPT写的,挺好用)

int putChineseText(cv::Mat& img,  std::string& text,  cv::Point& position,  std::string& fontFile, int fontSize,  cv::Scalar& color)
{
  // 加载字体库
    FT_Library ft;
    if (FT_Init_FreeType(&ft)) {
        fprintf(stderr, "无法初始化FreeType库\n");
        return -1;
    }

    // 加载字体文件
    FT_Face face;
    if (FT_New_Face(ft, "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc", 0, &face)) {
        fprintf(stderr, "无法加载字体文件\n");
        return -1;
    }

    // 设置字体大小
    FT_Set_Pixel_Sizes(face, 0, 24);

    // 创建OpenCV图像
    //cv::Mat image(400, 600, CV_8UC3, cv::Scalar(255, 255, 255));

    // 创建OpenCV图像的画布
    cv::Mat canvas = img.clone();

    // 设置文本参数
    cv::Point pos(position.x, position.y); 
    int lineThickness = 2;
    
     // 创建一个转换器
    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
    // 将std::string转换为std::wstring
    std::wstring texta = converter.from_bytes(text);
     
    for (wchar_t c : texta) {
        FT_Load_Char(face, c, FT_LOAD_RENDER);

        FT_GlyphSlot glyph = face->glyph;

        // 将字形位图绘制到OpenCV图像上
        for (int row = 0; row < glyph->bitmap.rows; ++row) {
            for (int col = 0; col < glyph->bitmap.width; ++col) {
                int x = pos.x + glyph->bitmap_left + col;
                int y = pos.y - glyph->bitmap_top + row;

                if (x >= 0 && x < img.cols && y >= 0 && y < img.rows) {
                    img.at<cv::Vec3b>(y, x) = cv::Vec3b(glyph->bitmap.buffer[row * glyph->bitmap.pitch + col], glyph->bitmap.buffer[row * glyph->bitmap.pitch + col], glyph->bitmap.buffer[row * glyph->bitmap.pitch + col]);
                }
            }
        }

        // 更新绘制位置
        pos.x += glyph->advance.x >> 6;
    }
 

    // 释放资源
    FT_Done_Face(face);
    FT_Done_FreeType(ft);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_陈陆亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值