├── .gitattributes ├── LICENSE ├── README.md ├── Vcpkg_configure.md ├── images ├── freetypevs001.png ├── freetypevs002.png ├── freetypevs003.png ├── freetypevs004.png ├── freetypevs005.png ├── freetypevs006.png ├── freetypevs007.png ├── freetypevs008.png ├── opencvvs001.png ├── opencvvs002.png ├── opencvvs003.png ├── opencvvs004.png ├── opencvvs005.png ├── opencvvs006.png ├── opencvvs007.png ├── opencvvs008.png ├── opencvvs009.png ├── opencvvs010.png ├── opencvvs011.png ├── opencvvs012.png ├── opencvvs013.png ├── opencvvs014.png ├── opencvvs015.png ├── opencvvs016.png ├── result.png ├── result1.png ├── result2.png ├── result3.png ├── result4.png ├── result5.png ├── table001.png ├── table002.png ├── ubuntu1.png ├── ubuntu2.png ├── ubuntu3.png └── ubuntu4.png ├── opencv_puttext_zh.md ├── opencv_puttext_zh ├── CMakeLists.txt ├── Chinese_test.cpp ├── Chinese_test.h ├── CvxFont.cpp ├── CvxFont.h ├── Japanese_test.cpp ├── Japanese_test.h ├── Symbol_test.h ├── font │ ├── STCAIYUN.TTF │ ├── msgothic.ttc │ └── seguisym.ttf ├── main.cpp └── symbol_test.cpp ├── opencv_visual_studio.md └── usage.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Yang Shun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # freetype_opencv 2 | 3 | ---- 4 | 5 | 更新:2020-11-14 6 | 7 | * 修改了画字形时移动画笔的方法(英文字体有高度位置关系,看起来更合适,但也有一点问题) 8 | * 修改多字节转 wchar 的方法,删除了原来跨平台的部分 9 | 10 | > 修改了画字形的方法后,也引出了另一个问题,根据 freetype 官方的说明,使用修改后的画图方法会使得画的文字位置与你想要的位置有一定的偏差,因为我的代码里是使用字型的 origin 点作为 pos。因此,为了画出你想要的结果,可以在定位时稍微将 pos 的 y 增加一点点。或者在 putWChar 函数中的 gPos 加上一个 0 ~ font size 的值。 11 | 12 | 为了方便直接使用,我创建了两个分支 13 | 14 | * Clion 项目分支 15 | * Visual Studio 项目分支 16 | 17 | 你也可以新建项目,拷贝主分支的代码。 18 | 19 | ---- 20 | 21 | 更新:2020-3-5 22 | 23 | * 重构代码 24 | * 添加下划线 25 | * 测试了其他字符集(包括日语和 Segoe UI) 26 | 27 | ---- 28 | 29 | 更新:2019-11-2 30 | 31 | * 使用 OpenCV 4.1 32 | * 使用 freetype 2.10.1 33 | * 支持 竖向文字、旋转文字 34 | 35 | ---- 36 | 37 | ## [使用说明](https://github.com/busyboxs/freetype_opencv/blob/master/usage.md) 38 | 39 | ## 运行效果 40 | 41 | 中文测试效果 42 | 43 | ![](images/ubuntu1.png) 44 | 45 | 日文测试效果 46 | 47 | ![](images/ubuntu2.png) 48 | 49 | Unicode 字符测试效果(需要使用对应的字体) 50 | 51 | ![](images/ubuntu3.png) 52 | 53 | ![](images/ubuntu4.png) 54 | -------------------------------------------------------------------------------- /Vcpkg_configure.md: -------------------------------------------------------------------------------- 1 | # Vcpkg 配置 freetype 和 OpenCV 2 | 3 | ## 本机环境 4 | 5 | * Windows 10 专业版 6 | * Visual Studio Community 2017 (版本 15.9.7) 7 | 8 | ## Windows 安装 Vcpkg 9 | 10 | Vcpkg 是适用于 Windows,Linux 和 MacOS 的 C++ 库管理器,使用它可以方便地管理 C++ 的依赖库。 11 | 12 | Vcpkg 的下载地址和使用说明: [https://github.com/microsoft/vcpkg](https://github.com/microsoft/vcpkg) 13 | 14 | 首先从 github 克隆源码,然后进入到文件夹。windows shell 执行 `.\bootstrap-vcpkg.bat`,git bash 执行 `./bootstrap-vcpkg.sh`。然后会在文件夹中生成可执行文件 `vcpkg.exe`。 15 | 16 | ```bash 17 | git clone https://github.com/Microsoft/vcpkg.git 18 | 19 | cd vcpkg 20 | 21 | PS> .\bootstrap-vcpkg.bat 22 | Linux:~/$ ./bootstrap-vcpkg.sh 23 | ``` 24 | 25 | vcpkg 安装库也很简单,首先使用 `vcpkg search lib-name` 搜索某个库是否存在以及对应的版本信息,然后使用 `vcpkg install lib-name` 安装指定的库。 26 | 27 | 如果不指定安装的架构,vcpkg 默认把开源库编译成 x86 的 Windows 版本的库。可以使用以下代码查询对应的架构: 28 | 29 | ```bash 30 | $ .\vcpkg.exe help triplet 31 | 32 | Available architecture triplets: 33 | arm-uwp 34 | arm-windows 35 | arm64-uwp 36 | arm64-windows 37 | x64-linux 38 | x64-osx 39 | x64-uwp 40 | x64-windows 41 | x64-windows-static 42 | x86-uwp 43 | x86-windows 44 | x86-windows-static 45 | ``` 46 | 47 | 我这里编译的版本为 x64,因此使用 x64-window。 48 | 49 | 执行以下代码进行安装库,有些库可能会安装附加依赖库,需要你添加 `--recurse`。 50 | 51 | ```bash 52 | > cd d:\vcpkg # 根据你的目录进行修改 53 | > ./vcpkg.exe install opencv:x64-windows 54 | > ./vcpkg.exe install freetype:x64-windows 55 | ``` 56 | 57 | 安装完后可以执行命令 `vcpkg list` 查看库 58 | 59 | ## 配置 Visual Studio 使用 Vcpkg 安装的库 60 | 61 | ### 集成到全局 62 | 63 | Vcpkg 提供了一套机制,可以全自动的适配目录,而开发者不需要关心已安装的库的目录在哪里,也不需要设置。 64 | 65 | ```bash 66 | $ ./vcpkg.exe integrate install 67 | Applied user-wide integration for this vcpkg root. 68 | 69 | All MSBuild C++ projects can now #include any installed libraries. 70 | Linking will be handled automatically. 71 | Installing new libraries will make them instantly available. 72 | 73 | CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=D:/vcpkg/scripts/buildsystems/vcpkg.cmake" 74 | ``` 75 | 76 | 当出现 “Applied user-wide integration for this vcpkg root.” 字样的时候,说明已经集成成功。这时候可以在任意的工程中使用安装好的第三方库。CMake 项目则需要添加后面那句指令。 77 | 78 | 如果不需要集成了,可以移除全局集成,执行下面指令即可 79 | 80 | ```bash 81 | ./vcpkg.exe integrate remove 82 | ``` 83 | 84 | ### 集成到工程 85 | 86 | "集成到工程”需要利用 Visual Studio 中的 nuget 插件来实现。 87 | 88 | #### 生成配置 89 | 90 | 执行命令 91 | 92 | ```bash 93 | $ ./vcpkg.exe integrate project 94 | Created nupkg: D:\vcpkg\scripts\buildsystems\vcpkg.D.vcpkg.1.0.0.nupkg 95 | 96 | With a project open, go to Tools->NuGet Package Manager->Package Manager Console and paste: 97 | Install-Package vcpkg.D.vcpkg -Source "D:\vcpkg\scripts\buildsystems" 98 | 99 | ``` 100 | 101 | 执行命令成功后会在 `“\scripts\buildsystems”` 目录下,生成 nuget 配置文件. 102 | 103 | #### NuGet配置 104 | 105 | 在 Visual Studio 中,点击菜单 “工具->选项”, 选择"NuGet包管理器->程序包源". 106 | 107 | 添加新的可用程序包源, 选择 vcpkg 目录下的 `“scripts\buildsystems”` 目录,然后点击右侧的 “更新” 按钮。 108 | 109 | 点击 “确定” 按钮,关闭对话框。 110 | 111 | 到此,全局性的设置已经完成。 112 | 113 | ![img](https://cdn.jsdelivr.net/gh/busyboxs/CDN@latest/blog/cpp/baiduAI/vcpkg_01.png) 114 | 115 | #### 工程配置 116 | 117 | 用 Visual Studio 打开一个工程或解决方案。右键点击需要设置的工程弹出菜单,选择“管理 NuGet 程序包”。 118 | 119 | ![工程配置](https://cdn.jsdelivr.net/gh/busyboxs/CDN@latest/blog/cpp/baiduAI/vcpkg_02.png) 120 | 121 | 在右上角的 “程序包源” 中选择刚刚设置的 “vcpkg”。这样在 “浏览” 选项卡中就可以看到 “vcpkg.D.vcpkg”。点击最右侧的 “安装”。这样就可以集成到某个工程了。 122 | 123 | ![img](https://cdn.jsdelivr.net/gh/busyboxs/CDN@latest/blog/cpp/baiduAI/vcpkg_03.png) 124 | -------------------------------------------------------------------------------- /images/freetypevs001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/freetypevs001.png -------------------------------------------------------------------------------- /images/freetypevs002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/freetypevs002.png -------------------------------------------------------------------------------- /images/freetypevs003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/freetypevs003.png -------------------------------------------------------------------------------- /images/freetypevs004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/freetypevs004.png -------------------------------------------------------------------------------- /images/freetypevs005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/freetypevs005.png -------------------------------------------------------------------------------- /images/freetypevs006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/freetypevs006.png -------------------------------------------------------------------------------- /images/freetypevs007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/freetypevs007.png -------------------------------------------------------------------------------- /images/freetypevs008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/freetypevs008.png -------------------------------------------------------------------------------- /images/opencvvs001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs001.png -------------------------------------------------------------------------------- /images/opencvvs002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs002.png -------------------------------------------------------------------------------- /images/opencvvs003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs003.png -------------------------------------------------------------------------------- /images/opencvvs004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs004.png -------------------------------------------------------------------------------- /images/opencvvs005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs005.png -------------------------------------------------------------------------------- /images/opencvvs006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs006.png -------------------------------------------------------------------------------- /images/opencvvs007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs007.png -------------------------------------------------------------------------------- /images/opencvvs008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs008.png -------------------------------------------------------------------------------- /images/opencvvs009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs009.png -------------------------------------------------------------------------------- /images/opencvvs010.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs010.png -------------------------------------------------------------------------------- /images/opencvvs011.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs011.png -------------------------------------------------------------------------------- /images/opencvvs012.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs012.png -------------------------------------------------------------------------------- /images/opencvvs013.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs013.png -------------------------------------------------------------------------------- /images/opencvvs014.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs014.png -------------------------------------------------------------------------------- /images/opencvvs015.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs015.png -------------------------------------------------------------------------------- /images/opencvvs016.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/opencvvs016.png -------------------------------------------------------------------------------- /images/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/result.png -------------------------------------------------------------------------------- /images/result1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/result1.png -------------------------------------------------------------------------------- /images/result2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/result2.png -------------------------------------------------------------------------------- /images/result3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/result3.png -------------------------------------------------------------------------------- /images/result4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/result4.png -------------------------------------------------------------------------------- /images/result5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/result5.png -------------------------------------------------------------------------------- /images/table001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/table001.png -------------------------------------------------------------------------------- /images/table002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/table002.png -------------------------------------------------------------------------------- /images/ubuntu1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/ubuntu1.png -------------------------------------------------------------------------------- /images/ubuntu2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/ubuntu2.png -------------------------------------------------------------------------------- /images/ubuntu3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/ubuntu3.png -------------------------------------------------------------------------------- /images/ubuntu4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/images/ubuntu4.png -------------------------------------------------------------------------------- /opencv_puttext_zh.md: -------------------------------------------------------------------------------- 1 | # OpenCV 在图像中显示中文 2 | 3 | ## 本机环境 4 | 5 | * Windows 10 6 | * Visual Studio Community 2017 (version 15.9.17) 7 | * OpenCV 4.1.2 8 | * freetype 2.10.1 9 | 10 | ## Visual Studio 下 OpenCV 环境配置 11 | 12 | Visual Studio 下 OpenCV 环境配置可以参见我的另外一篇博客:[Visual Studio Opencv 环境配置 - 手动配置](https://github.com/busyboxs/freetype_opencv/blob/master/opencv_visual_studio.md) 13 | 14 | ## freetype 环境配置 15 | 16 | 首先下载 freetype 文件。下载地址:[FreeType Downloads](https://www.freetype.org/download.html) 17 | 18 | 然后将文件解压,我这里解压到 D 盘 19 | 20 | ![文件解压](images/freetypevs001.png) 21 | 22 | 然后找到 `~\build\windows\vs2010` 路径,鼠标右键 `freetype.sln`,用 Visual Studio 打开。 23 | 24 | ![](images/freetypevs002.png) 25 | 26 | 然后鼠标右键项目名 `freetype`,选择 “重定向项目”。 27 | 28 | ![](images/freetypevs003.png) 29 | 30 | 然后点击 “确定”。 31 | 32 | ![](images/freetypevs004.png) 33 | 34 | 然后查看项目的属性,可以看到链接文件的输出路径 35 | 36 | ![](images/freetypevs005.png) 37 | 38 | 这里可以选择不同的配置(Debug & Release)和平台(Win32 & x64),然后运行项目就可以生成对应配置和平台的链接文件。 39 | 40 | ![](images/freetypevs006.png) 41 | 42 | 最终我们需要的是 `freetype.lib` 文件,为了方便我把该文件复制到了 `D:\freetype-2.10.1\lib\X64\Debug\` 43 | 44 | ![](images/freetypevs007.png) 45 | 46 | ## Visual Studio 配置 47 | 48 | Visual Studio 中需要配置包含目录,库目录和链接器输入的附加依赖库。 49 | 50 | * 包含目录为: `D:\freetype-2.10.1\include` 51 | * 库目录为: `D:\freetype-2.10.1\lib\X64\Debug` 52 | * 附加依赖库: `freetype.lib` 53 | 54 | 如何配置可以参见我的另外一篇博客:[Visual Studio Opencv 环境配置 - 手动配置](https://github.com/busyboxs/freetype_opencv/blob/master/opencv_visual_studio.md)。 55 | 56 | ## 测试代码 57 | 58 | 源码地址:[freetype_opencv](https://github.com/busyboxs/freetype_opencv) 59 | 60 | 运行结果图 61 | 62 | ![](images/result.png) 63 | 64 | ![](images/result4.png) 65 | -------------------------------------------------------------------------------- /opencv_puttext_zh/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(OpenCVUnicode) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | find_package(OpenCV REQUIRED) 7 | message(STATUS "OpenCV library status:") 8 | message(STATUS " version: ${OpenCV_VERSION}") 9 | message(STATUS " libraries: ${OpenCV_LIBS}") 10 | message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") 11 | include_directories(${OpenCV_INCLUDE_DIRS}) 12 | 13 | find_package(Freetype REQUIRED) 14 | message(STATUS "Freetype library status") 15 | message(STATUS " ${FREETYPE_INCLUDE_DIRS}") 16 | message(STATUS " ${FREETYPE_INCLUDE_DIR_ft2build}") 17 | message(STATUS " ${FREETYPE_INCLUDE_DIR_freetype2}") 18 | include_directories(${FREETYPE_INCLUDE_DIRS}) 19 | 20 | set(SRC 21 | main.cpp 22 | CvxFont.cpp 23 | Chinese_test.cpp 24 | Japanese_test.cpp 25 | Symbol_test.cpp) 26 | 27 | add_executable(OpenCVUnicode ${SRC}) 28 | target_link_libraries(OpenCVUnicode ${OpenCV_LIBS}) 29 | target_link_libraries(OpenCVUnicode ${FREETYPE_LIBRARIES}) -------------------------------------------------------------------------------- /opencv_puttext_zh/Chinese_test.cpp: -------------------------------------------------------------------------------- 1 | #include "CvxFont.h" 2 | using namespace cvx; 3 | 4 | extern cv::Scalar randomColor(cv::RNG& rng); 5 | static cv::RNG rng(0xFFFFFFFF); 6 | 7 | void testLowerAlpha(cvx::CvxFont& font, cv::Mat& img) { 8 | cv::String msg = "abcdefghijklmnopqrstuvwxyz"; 9 | int fontSize = 32; 10 | cvx::putText(img, msg, cv::Point(40, 40), font, fontSize, cv::Scalar(0, 255, 0)); 11 | } 12 | 13 | void testUpperAlpha(cvx::CvxFont& font, cv::Mat& img) { 14 | // cv::RNG rng(0xFFFFFFFF); 15 | cv::String msg1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 16 | int fontSize = 32; 17 | cvx::putText(img, msg1, cv::Point(40, 100), font, fontSize, randomColor(rng)); 18 | } 19 | 20 | void testHorizon(cvx::CvxFont& font, cv::Mat& img) { 21 | // cv::RNG rng(0xFFFFFFFF); 22 | int fontSize = 32; 23 | cv::String msg2 = "这是水平字符abcgf"; 24 | cvx::putText(img, msg2, cv::Point(40, 200), font, fontSize, randomColor(rng)); 25 | } 26 | 27 | void testVertical(cvx::CvxFont& font, cv::Mat& img) { 28 | // cv::RNG rng(0xFFFFFFFF); 29 | int fontSize = 32; 30 | font.setVertical(true); 31 | cv::String msg3 = "这是竖直字符abcij"; 32 | cvx::putText(img, msg3, cv::Point(17, 30), font, fontSize, randomColor(rng)); 33 | } 34 | 35 | void testHorizonRotate(cvx::CvxFont& font, cv::Mat& img) { 36 | // cv::RNG rng(0xFFFFFFFF); 37 | int fontSize = 32; 38 | float angle = 30; 39 | font.setRotateAngle(angle); 40 | cv::String msg4 = "This is a rotate string!"; 41 | cvx::putText(img, msg4, cv::Point(40, 250), font, fontSize, randomColor(rng)); 42 | } 43 | 44 | void testVerticalRotate(cvx::CvxFont& font, cv::Mat& img) { 45 | // cv::RNG rng(0xFFFFFFFF); 46 | int fontSize = 32; 47 | float angle = -30; 48 | font.setVertical(true); 49 | font.setRotateAngle(angle); 50 | cv::String msg5 = "这是旋转字符"; 51 | cvx::putText(img, msg5, cv::Point(700, 100), font, fontSize, randomColor(rng)); 52 | } 53 | 54 | void testTruncate(cvx::CvxFont& font, cv::Mat& img) { 55 | // cv::RNG rng(0xFFFFFFFF); 56 | int fontSize = 32; 57 | cv::String msg6 = "字符可以被截断"; 58 | putText(img, msg6, cv::Point(600, 40), font, fontSize, randomColor(rng)); 59 | } 60 | 61 | void testFontSize(cvx::CvxFont& font, cv::Mat& img) { 62 | // cv::RNG rng(0xFFFFFFFF); 63 | int fontSize = 16; 64 | cv::String msg7 = "这是16号字体"; 65 | putText(img, msg7, cv::Point(40, 350), font, fontSize, randomColor(rng)); 66 | cv::String msg8 = "这是32号字体"; 67 | fontSize = 32; 68 | putText(img, msg8, cv::Point(160, 350), font, fontSize, randomColor(rng)); 69 | } 70 | 71 | void testSpace(cvx::CvxFont& font, cv::Mat& img) { 72 | // cv::RNG rng(0xFFFFFFFF); 73 | int fontSize = 32; 74 | cv::String msg9 = "有空 格时"; 75 | putText(img, msg9, cv::Point(500, 300), font, fontSize, randomColor(rng)); 76 | } 77 | 78 | void testBetweenSpace(cvx::CvxFont& font, cv::Mat& img) { 79 | // cv::RNG rng(0xFFFFFFFF); 80 | int fontSize = 32; 81 | cv::String msg10 = "更大的字间距"; 82 | font.setFontRatio(0.3f); 83 | putText(img, msg10, cv::Point(500, 350), font, fontSize, randomColor(rng)); 84 | } 85 | 86 | void testTransparency(cvx::CvxFont& font, cv::Mat& img) { 87 | // cv::RNG rng(0xFFFFFFFF); 88 | int fontSize = 32; 89 | cv::String msg11 = "设置透明度"; 90 | font.setDiaphaneity(0.3f); 91 | putText(img, msg11, cv::Point(500, 200), font, fontSize, randomColor(rng)); 92 | } 93 | 94 | void testUnderline(cvx::CvxFont& font, cv::Mat& img) { 95 | // cv::RNG rng(0xFFFFFFFF); 96 | int fontSize = 32; 97 | cv::String msg12 = "加下划线"; 98 | font.setUnderline(true); 99 | putText(img, msg12, cv::Point(500, 150), font, fontSize, randomColor(rng)); 100 | } 101 | 102 | void testChinese() { 103 | cv::Mat img(400, 800, CV_8UC3, cv::Scalar(0, 0, 0)); // create a black background 104 | std::string fontPath = "../font/STCAIYUN.TTF"; // be careful to use the font that support Chinese 105 | cvx::CvxFont font(fontPath); 106 | 107 | testLowerAlpha(font, img); 108 | testUpperAlpha(font, img); 109 | testHorizon(font, img); 110 | testVertical(font, img); 111 | testHorizonRotate(font, img); 112 | testVerticalRotate(font, img); 113 | testTruncate(font, img); 114 | testFontSize(font, img); 115 | testSpace(font, img); 116 | testBetweenSpace(font, img); 117 | testTransparency(font, img); 118 | testUnderline(font, img); 119 | 120 | cv::imshow("test", img); 121 | cv::waitKey(); 122 | } -------------------------------------------------------------------------------- /opencv_puttext_zh/Chinese_test.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENCVUNICODE_CHINESE_TEST_H 2 | #define OPENCVUNICODE_CHINESE_TEST_H 3 | 4 | void testChinese(); 5 | 6 | #endif //OPENCVUNICODE_CHINESE_TEST_H 7 | -------------------------------------------------------------------------------- /opencv_puttext_zh/CvxFont.cpp: -------------------------------------------------------------------------------- 1 | #include "CvxFont.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | cvx::CvxFont::CvxFont(const cv::String& fontType) 9 | { 10 | assert(!fontType.empty()); 11 | 12 | m_error = FT_Init_FreeType(&m_library); 13 | if (m_error) 14 | { 15 | std::cerr << "library initial error!" << std::endl; 16 | return; 17 | } 18 | 19 | m_error = FT_New_Face(m_library, fontType.c_str(), 0, &m_face); 20 | if (m_error == FT_Err_Unknown_File_Format) 21 | { 22 | std::cerr << "unsupported font format!" << std::endl; 23 | return; 24 | } 25 | else if (m_error) 26 | { 27 | std::cerr << " can not open font files" << std::endl; 28 | return; 29 | } 30 | 31 | // use default parameters 32 | m_font = new FontProperty; 33 | initFont(); 34 | 35 | setlocale(LC_ALL, ""); 36 | } 37 | 38 | // release freetype resource 39 | cvx::CvxFont::~CvxFont() 40 | { 41 | delete m_font; 42 | FT_Done_Face(m_face); 43 | FT_Done_FreeType(m_library); 44 | } 45 | 46 | void cvx::CvxFont::setFontSize(const int fontSize) 47 | { 48 | m_font->fontSize = fontSize; 49 | FT_Set_Pixel_Sizes(m_face, fontSize, 0); 50 | } 51 | 52 | // initial font 53 | void cvx::CvxFont::initFont() 54 | { 55 | setFontSize(16); 56 | setSpaceRatio(0.5); 57 | setFontRatio(0); 58 | setRotateAngle(0); 59 | setDiaphaneity(1); 60 | setUnderline(false); 61 | setVertical(false); 62 | // set font 63 | FT_Set_Pixel_Sizes(m_face, getFontSize(), 0); 64 | } 65 | 66 | void cvx::CvxFont::rotateFont(double angle) { 67 | angle = (angle / 360) * 3.14159 * 2; 68 | /* set up matrix */ 69 | m_matrix.xx = static_cast(cos(angle) * 0x10000L); 70 | m_matrix.xy = static_cast(-sin(angle) * 0x10000L); 71 | m_matrix.yx = static_cast(sin(angle) * 0x10000L); 72 | m_matrix.yy = static_cast(cos(angle) * 0x10000L); 73 | 74 | FT_Set_Transform(m_face, &m_matrix, nullptr); 75 | } 76 | 77 | void cvx::CvxFont::putTextStr(cv::Mat& img, const cv::String& text, cv::Point pos, const cv::Scalar& color) 78 | { 79 | CV_Assert(!img.empty()); 80 | CV_Assert(!text.empty()); 81 | 82 | int xStart = pos.x; 83 | int yStart = pos.y; 84 | m_maxDiffHeight = 0; 85 | 86 | const char* ptr = text.c_str(); 87 | std::mbtowc(nullptr, nullptr, 0); // reset the conversion state 88 | const char* end = ptr + std::strlen(ptr); 89 | int ret; 90 | for (wchar_t wc; (ret = std::mbtowc(&wc, ptr, end - ptr)) > 0; ptr += ret) { 91 | putWChar(img, (wc & 0xffffffff), pos, color); 92 | } 93 | 94 | int xEnd = pos.x; 95 | int yEnd = pos.y; 96 | if (getUnderline()) { 97 | if (getVertical()) { 98 | cv::line(img, cv::Point(xStart + m_maxDiffHeight, yStart), cv::Point(xStart + m_maxDiffHeight, yEnd), color, 2); 99 | } 100 | else { 101 | cv::line(img, cv::Point(xStart, yStart + m_maxDiffHeight), cv::Point(xEnd, yStart + m_maxDiffHeight), color, 2); 102 | } 103 | } 104 | 105 | } 106 | 107 | void cvx::CvxFont::putWChar(cv::Mat& img, uint32_t wc, cv::Point& pos, const cv::Scalar& color) 108 | { 109 | rotateFont(getAngle()); 110 | const auto vertical = getVertical(); 111 | const auto size = getFontSize(); 112 | 113 | // Converting a Character Code Into a Glyph Index 114 | FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc); 115 | FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT); 116 | FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_MONO); 117 | 118 | FT_GlyphSlot slot = m_face->glyph; 119 | FT_Bitmap bitmap = slot->bitmap; 120 | bool isSpace = wc == ' '; 121 | 122 | // get rows and cols of current wide char 123 | auto rows = bitmap.rows; 124 | auto cols = bitmap.width; 125 | 126 | cv::Point gPos = pos; 127 | //gPos.y += m_font->fontSize; 128 | if (vertical) 129 | { 130 | gPos.x += (slot->metrics.vertBearingX >> 6); 131 | gPos.y += (slot->metrics.vertBearingY >> 6); 132 | m_maxDiffHeight = std::max(m_maxDiffHeight, rows - (slot->metrics.vertBearingY >> 6)); 133 | } 134 | else 135 | { 136 | gPos.x += (slot->metrics.horiBearingX >> 6); 137 | gPos.y -= (slot->metrics.horiBearingY >> 6); 138 | m_maxDiffHeight = std::max(m_maxDiffHeight, rows - (slot->metrics.horiBearingY >> 6)); 139 | } 140 | 141 | // https://stackoverflow.com/questions/52254639/how-to-access-pixels-state-in-monochrome-bitmap-using-freetype2 142 | for (auto i = 0; i < rows; ++i) 143 | { 144 | for (auto j = 0; j < cols; ++j) 145 | { 146 | int off = i * slot->bitmap.pitch + j / 8; 147 | 148 | if (slot->bitmap.buffer[off] & (0x80 >> (j % 8))) 149 | { 150 | const auto r = gPos.y + i; //vertical ? pos.y + i : pos.y + i + (size - rows); // to make align to bottom 151 | const auto c = gPos.x + j; 152 | 153 | if (r >= 0 && r < img.rows && c >= 0 && c < img.cols) 154 | { 155 | cv::Vec3b scalar = img.at(cv::Point(c, r)); 156 | 157 | // merge set color with origin color 158 | double p = getDiaphaneity(); 159 | for (int k = 0; k < 3; ++k) 160 | { 161 | scalar.val[k] = static_cast(scalar.val[k] * (1 - p) + color.val[k] * p); 162 | } 163 | 164 | img.at(cv::Point(c, r)) = cv::Vec3b(scalar[0], scalar[1], scalar[2]); 165 | } 166 | } 167 | } 168 | } 169 | 170 | // modify position to next character 171 | 172 | const auto space = static_cast(size * getSpaceRatio()); 173 | const auto sep = static_cast(size * getFontRatio()); 174 | // vertical string or not, default not vertical 175 | if (vertical) 176 | { 177 | const auto moveX = (static_cast(getAngle()) == 0) ? (slot->metrics.vertAdvance >> 6) : rows + 1; 178 | pos.y += isSpace ? space : moveX + sep; 179 | } 180 | else 181 | { 182 | const auto moveY = (static_cast(getAngle()) == 0) ? (slot->metrics.horiAdvance >> 6) : cols + 1; 183 | pos.x += isSpace ? space : moveY + sep; 184 | } 185 | } 186 | 187 | void cvx::putText(cv::Mat& img, const std::string& text, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color) { 188 | fontFace.setFontSize(fontSize); 189 | fontFace.putTextStr(img, text, std::move(pos), color); 190 | fontFace.initFont(); 191 | } 192 | 193 | void cvx::putOneSymbol(cv::Mat& img, uint32_t symbol, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color) { 194 | fontFace.setFontSize(fontSize); 195 | fontFace.putWChar(img, symbol, pos, color); 196 | fontFace.initFont(); 197 | } 198 | 199 | void cvx::putSymbols(cv::Mat& img, std::vector& symbols, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color) { 200 | fontFace.setFontSize(fontSize); 201 | for (auto& symbol : symbols) { 202 | fontFace.putWChar(img, symbol, pos, color); 203 | } 204 | fontFace.initFont(); 205 | } -------------------------------------------------------------------------------- /opencv_puttext_zh/CvxFont.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENCVUNICODE_CVXFONT_H 2 | #define OPENCVUNICODE_CVXFONT_H 3 | 4 | #include 5 | #include FT_FREETYPE_H 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace cvx { 14 | struct FontProperty { 15 | int fontSize; // font size (pixel) 16 | double spaceRatio; // ratio of distance when meet a space, base on font size 17 | double fontRatio; // ratio of distance between each character, base on font size 18 | double fontRotateAngle; // rotate angle 19 | double fontDiaphaneity; // merge ratio 20 | bool fontIsUnderline; // underline 21 | bool fontIsVertical; // put text in vertical 22 | }; 23 | 24 | class CvxFont 25 | { 26 | public: 27 | 28 | explicit CvxFont(const cv::String& fontType); 29 | virtual ~CvxFont(); 30 | 31 | void setFontSize(int fontSize); 32 | void setSpaceRatio(const double spaceRatio) { m_font->spaceRatio = spaceRatio; } 33 | void setFontRatio(const double fontRatio) { m_font->fontRatio = fontRatio; } 34 | void setRotateAngle(const double angle) { m_font->fontRotateAngle = angle; } 35 | void setUnderline(const bool isUnderline) { m_font->fontIsUnderline = isUnderline; } 36 | void setDiaphaneity(const double diaphaneity) { m_font->fontDiaphaneity = diaphaneity; } 37 | void setVertical(const bool vertical) { m_font->fontIsVertical = vertical; } 38 | 39 | [[nodiscard]] int getFontSize() const { return m_font->fontSize; } 40 | [[nodiscard]] double getSpaceRatio() const { return m_font->spaceRatio; } 41 | [[nodiscard]] double getFontRatio() const { return m_font->fontRatio; } 42 | [[nodiscard]] double getAngle() const { return m_font->fontRotateAngle; } 43 | [[nodiscard]] bool getUnderline() const { return m_font->fontIsUnderline; } 44 | [[nodiscard]] double getDiaphaneity() const { return m_font->fontDiaphaneity; } 45 | [[nodiscard]] bool getVertical() const { return m_font->fontIsVertical; } 46 | 47 | private: 48 | void initFont(); 49 | void rotateFont(double angle); 50 | void putTextStr(cv::Mat& img, const cv::String& text, cv::Point pos, const cv::Scalar& color); 51 | void putWChar(cv::Mat& img, uint32_t wc, cv::Point& pos, const cv::Scalar& color); 52 | 53 | friend void putText(cv::Mat&, const std::string&, cv::Point, cvx::CvxFont&, int, const cv::Scalar&); 54 | friend void putSymbols(cv::Mat&, std::vector&, cv::Point, cvx::CvxFont&, int, const cv::Scalar&); 55 | friend void putOneSymbol(cv::Mat&, uint32_t, cv::Point, cvx::CvxFont&, int, const cv::Scalar&); 56 | 57 | FT_Library m_library{}; // font library 58 | FT_Face m_face{}; // font type 59 | FT_Matrix m_matrix{}; 60 | FT_Vector m_pen{}; 61 | FT_Error m_error; 62 | 63 | FontProperty* m_font; 64 | long m_maxDiffHeight{ 0 }; 65 | 66 | }; 67 | 68 | void putText(cv::Mat& img, const std::string& text, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color); 69 | void putSymbols(cv::Mat& img, std::vector& symbols, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color); 70 | void putOneSymbol(cv::Mat& img, uint32_t symbol, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color); 71 | } 72 | 73 | #endif //OPENCVUNICODE_CVXFONT_H 74 | -------------------------------------------------------------------------------- /opencv_puttext_zh/Japanese_test.cpp: -------------------------------------------------------------------------------- 1 | #include "CvxFont.h" 2 | 3 | extern cv::Scalar randomColor(cv::RNG& rng); 4 | static cv::RNG rng(0xFFFFFFFF); 5 | 6 | // https://zh.wikipedia.org/wiki/%E5%B9%B3%E5%81%87%E5%90%8D 7 | void testJapanese() { 8 | // create a black background 9 | cv::Mat img(400, 800, CV_8UC3, cv::Scalar(0, 0, 0)); 10 | std::string fontPath = "../font/msgothic.ttc"; 11 | 12 | // be careful to use the font that support Chinese 13 | cvx::CvxFont font(fontPath); 14 | 15 | cv::String s = "\u3042\u3044\u3057\u3066\u308b"; 16 | putText(img, s, cv::Point(500, 150), font, 32, randomColor(rng)); 17 | 18 | cv::imshow("test1", img); 19 | cv::waitKey(); 20 | } 21 | 22 | void testOthers() { 23 | // create a black background 24 | cv::Mat img(700, 1200, CV_8UC3, cv::Scalar(0, 0, 0)); 25 | std::string fontPath = "../font/msgothic.ttc"; 26 | 27 | // be careful to use the font that support Chinese 28 | cvx::CvxFont font(fontPath); 29 | int i = 0, page = 0, x = 10, y = 40; 30 | std::vector symbols{}; 31 | for (uint32_t c = 0x3040; c <= 0x309f; ++c) { 32 | symbols.push_back(c); 33 | if ((i + 1) % 16 == 0) { 34 | if ((i + 1) % 32 == 0) { 35 | cvx::putSymbols(img, symbols, cv::Point(x, y), font, 32, randomColor(rng)); 36 | y += 50; 37 | x = 10; 38 | symbols.clear(); 39 | } 40 | else { 41 | cvx::putSymbols(img, symbols, cv::Point(x, y), font, 32, randomColor(rng)); 42 | x += 600; 43 | symbols.clear(); 44 | } 45 | } 46 | if ((i + 1) % (14 * 32) == 0) { 47 | cv::imshow("test2", img); 48 | cv::Mat black(700, 1200, CV_8UC3, cv::Scalar(0, 0, 0)); 49 | img = black; 50 | char k = cv::waitKey(); 51 | x = 10, y = 40; 52 | if (k == 'q') { // press q to quite 53 | return; 54 | } 55 | } 56 | i++; 57 | 58 | } 59 | 60 | cv::imshow("test2", img); 61 | cv::waitKey(); 62 | } -------------------------------------------------------------------------------- /opencv_puttext_zh/Japanese_test.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENCVUNICODE_JAPANESE_TEST_H 2 | #define OPENCVUNICODE_JAPANESE_TEST_H 3 | 4 | void testJapanese(); 5 | void testOthers(); 6 | 7 | #endif //OPENCVUNICODE_JAPANESE_TEST_H 8 | -------------------------------------------------------------------------------- /opencv_puttext_zh/Symbol_test.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENCVUNICODE_SYMBOL_TEST_H 2 | #define OPENCVUNICODE_SYMBOL_TEST_H 3 | 4 | void testSymbols(); 5 | 6 | #endif //OPENCVUNICODE_SYMBOL_TEST_H 7 | -------------------------------------------------------------------------------- /opencv_puttext_zh/font/STCAIYUN.TTF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/opencv_puttext_zh/font/STCAIYUN.TTF -------------------------------------------------------------------------------- /opencv_puttext_zh/font/msgothic.ttc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/opencv_puttext_zh/font/msgothic.ttc -------------------------------------------------------------------------------- /opencv_puttext_zh/font/seguisym.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/busyboxs/freetype_opencv/d8f880584c164f409c228548c7d461cf6564afb9/opencv_puttext_zh/font/seguisym.ttf -------------------------------------------------------------------------------- /opencv_puttext_zh/main.cpp: -------------------------------------------------------------------------------- 1 | #include "CvxFont.h" 2 | 3 | #include "Chinese_test.h" 4 | #include "Japanese_test.h" 5 | #include "Symbol_test.h" 6 | 7 | cv::Scalar randomColor(cv::RNG& rng) 8 | { 9 | auto icolor = (unsigned)rng; 10 | auto randomColor = cv::Scalar(icolor & 255, (icolor >> 8) & 255, (icolor >> 16) & 255); 11 | return randomColor; 12 | } 13 | 14 | std::vector fontText{ 15 | "msjh.ttc", // 0 - 繁体 16 | "Deng.ttf", // 1 - 等线简体 17 | "blodblack.ttf", // 2 - 方正粗黑简体 18 | "FZSTK.TTF", // 3 - 方正舒体 19 | "FZYTK.TTF", // 4 - 方正姚体 20 | "simfang.ttf", // 5 - 仿宋 21 | "simhei.ttf", // 6 - 黑体 22 | "STCAIYUN.TTF", // 7 - 华文彩云 23 | "STFANGSO.TTF", // 8 - 华文仿宋 24 | "STXINGKA.TTF", // 9 - 华文行楷 25 | "STHUPO.TTF", // 10 - 华文琥珀 26 | "STKAITI.TTF", // 11 - 华文楷体 27 | "STLITI.TTF", // 12 - 华文隶书 28 | "STSONG.TTF", // 13 - 华文宋体 29 | "STXIHEI.TTF", // 14 - 华文细黑 30 | "STXINWEI.TTF", // 15 - 华文新魏 31 | "STZHONGS.TTF", // 16 - 华文中宋 32 | "simkai.ttf", // 17 - 楷体 33 | "SIMLI.TTF", // 18 - 隶书 34 | "msyh.ttc", // 19 - 微软雅黑 35 | "SIMYOU.TTF", // 20 - 幼圆 36 | }; 37 | 38 | 39 | 40 | 41 | int main(int argc, char *argv[]) 42 | { 43 | testChinese(); 44 | testJapanese(); 45 | testOthers(); 46 | testSymbols(); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /opencv_puttext_zh/symbol_test.cpp: -------------------------------------------------------------------------------- 1 | #include "CvxFont.h" 2 | 3 | extern cv::Scalar randomColor(cv::RNG& rng); 4 | static cv::RNG rng(0xFFFFFFFF); 5 | 6 | // http://www.kreativekorp.com/charset/font/SegoeUISymbol/ 7 | void testSymbols() { 8 | // create a black background 9 | cv::Mat img(700, 1200, CV_8UC3, cv::Scalar(0, 0, 0)); 10 | std::string fontPath = "../font/seguisym.ttf"; 11 | 12 | // be careful to use the font that support Chinese 13 | cvx::CvxFont font(fontPath); 14 | int i = 0, page = 0, x = 10, y = 40; 15 | std::vector symbols{}; 16 | for (uint32_t c = 0x1f300; c <= 0x1f6cf; ++c) { 17 | symbols.push_back(c); 18 | if ((i + 1) % 16 == 0) { 19 | if ((i + 1) % 32 == 0) { 20 | cvx::putSymbols(img, symbols, cv::Point(x, y), font, 32, randomColor(rng)); 21 | y += 50; 22 | x = 10; 23 | symbols.clear(); 24 | } 25 | else { 26 | cvx::putSymbols(img, symbols, cv::Point(x, y), font, 32, randomColor(rng)); 27 | x += 600; 28 | symbols.clear(); 29 | } 30 | } 31 | if ((i + 1) % (14 * 32) == 0) { 32 | cv::imshow("test2", img); 33 | cv::Mat black(700, 1200, CV_8UC3, cv::Scalar(0, 0, 0)); 34 | img = black; 35 | char k = cv::waitKey(); 36 | x = 10, y = 40; 37 | if (k == 'q') { // press q to quite 38 | return; 39 | } 40 | } 41 | i++; 42 | } 43 | 44 | cv::imshow("test2", img); 45 | cv::waitKey(); 46 | } -------------------------------------------------------------------------------- /opencv_visual_studio.md: -------------------------------------------------------------------------------- 1 | # Visual Studio Opencv 环境配置 - 手动配置 2 | 3 | ## 本机环境 4 | 5 | * Windows 10 6 | * Visual Studio Community 2017 (version 15.9.17) 7 | * OpenCV 4.1.2 8 | 9 | ## OpenCV环境配置 10 | 11 | 首先下载 OpenCV,下载地址: [opencv-win](https://sourceforge.net/projects/opencvlibrary/files/opencv-win/) 12 | 13 | ![OpenCV 下载图](images/opencvvs001.png) 14 | 15 | 然后运行下载的文件,选择提取文件的路径,并提取文件。 16 | 17 | ![提取文件](images/opencvvs002.png) 18 | 19 | 提取完成后添加 PATH 环境。首先新建一个环境变量 20 | 21 | > 变量名: `OPENCV_DIR` 22 | > 23 | > 变量值: `E:\opencv4.1\opencv\build` 24 | 25 | ![添加环境变量](images/opencvvs003.png) 26 | 27 | 然后在 PATH 中新建变量。并添加以下路径(根据自己系统类型选择对应的文件夹,确保路径下有文件) 28 | 29 | ```text 30 | %OPENCV_DIR%\x64\vc15\bin 31 | ``` 32 | 33 | ![](images/opencvvs004.png) 34 | 35 | ## Visual Studio 配置属性 36 | 37 | 首先打开 Visual Studio 并新建一个空应用。 38 | 39 | ![](images/opencvvs005.png) 40 | 41 | 然后新建一个原文件 `main.cpp`。 42 | 43 | ![](images/opencvvs006.png) 44 | 45 | 然后打开属性管理器,进行配置。这里只以 `Debug|x64` 为例,其他的配置相似。 46 | 47 | ![](images/opencvvs007.png) 48 | 49 | 鼠标右键 `Debug|x64`,选择 “添加新项目属性表”。文件名为 `OpenCVDebug`。 50 | 51 | ![](images/opencvvs008.png) 52 | 53 | ![](images/opencvvs009.png) 54 | 55 | 然后鼠标右键 `OpenCVDebug`,选择 “属性”。 56 | 57 | ![](images/opencvvs010.png) 58 | 59 | 这里需要配置的地方有 3 个,分别是 “包含目录”、 “库目录” 和 链接器输入的 “附加依赖项”。 60 | 61 | ![](images/opencvvs011.png) 62 | 63 | ![](images/opencvvs012.png) 64 | 65 | 包含目录里需要添加两个字段 66 | 67 | ```text 68 | $(OPENCV_DIR)\include\opencv2 69 | $(OPENCV_DIR)\include\ 70 | ``` 71 | 72 | ![](images/opencvvs013.png) 73 | 74 | 如上图所示,当我们添加了这两个字段后,会得到计算值,必须保证这个计算值的路径是正确的,能够访问到文件。 75 | 76 | 库目录里需要添加一个字段。由于下载的 OpenCV 只编译了 x64,因此这里只能配置 x64 的编译环境,如需要使用 win32 的编译环境,需要自己编译 OpenCV。 77 | 78 | ```text 79 | $(OPENCV_DIR)\x64\vc15\lib 80 | ``` 81 | 82 | ![](images/opencvvs014.png) 83 | 84 | 同样需要保证计算的值是正确的。 85 | 86 | 附加依赖项需要添加 lib 文件名。也就是库目录下的 lib 文件的名字。其中数字后面带 `d` 的是表示 Debug 模式编译的,不带 `d` 的表示 Release 模式编译的。 87 | 88 | ```text 89 | opencv_world412d.lib 90 | ``` 91 | 92 | ![](images/opencvvs015.png) 93 | 94 | 至此,Visual Studio OpenCV环境已经配置好了。 95 | 96 | ## OpenCV 测试 97 | 98 | 在 `main.cpp` 文件中输入以下代码。注意根据你的 OpenCV 的提取目录修改图像文件路径。 99 | 100 | ```cpp 101 | #include 102 | #include 103 | 104 | int main() 105 | { 106 | cv::Mat img = cv::imread("E:\\opencv4.1\\opencv\\sources\\samples\\data\\lena.jpg"); 107 | if (img.empty()) { 108 | std::cout << "Can not read image" << std::endl; 109 | return -1; 110 | } 111 | cv::imshow("image", img); 112 | cv::waitKey(); 113 | return 0; 114 | } 115 | ``` 116 | 117 | 如果能够显示图像则表示环境配置成功。 118 | 119 | ![](images/opencvvs016.png) -------------------------------------------------------------------------------- /usage.md: -------------------------------------------------------------------------------- 1 | # 该项目使用说明文档 2 | 3 | ## 环境配置 4 | 5 | ### 1. Visual Studio 手动配置 6 | 7 | * 手动配置: [OpenCV 在图像中显示中文](https://github.com/busyboxs/freetype_opencv/blob/master/opencv_puttext_zh.md) 8 | 9 | ### 2. Visual Studio Vcpkg 配置 10 | 11 | * Vcpkg 配置:[Vcpkg 配置 freetype 和 OpenCV](https://github.com/busyboxs/freetype_opencv/blob/master/opencv_visual_studio.md) 12 | 13 | ### 3. Clion 配置 14 | 15 | * 配置文件参考:[CMakeLists](https://github.com/busyboxs/freetype_opencv/blob/master/opencv_puttext_zh/CMakeLists.txt) 16 | 17 | ---- 18 | 19 | ## 项目说明 20 | 21 | 分别使用对应的 IDE 创建项目,然后把代码添加或者拷贝进去即可 22 | 23 | 项目主要代码文件如下: 24 | 25 | * `CvxText.cpp` 26 | * `CvxText.h` 27 | 28 | 测试代码如下: 29 | 30 | * `Chinese_test.cpp` 31 | * `Japanese_test.cpp` 32 | * `symbol_test.cpp` 33 | * `main.cpp` 34 | 35 | 字体可以直接从 windows 系统(`c:\windows\fonts`)里复制过来,然后放到 font 文件夹下。 36 | 37 | ---- 38 | 39 | ## 使用说明 40 | 41 | 代码中定义了一个字体类,字体属性包含以下 42 | 43 | ```cpp 44 | int fontSize; // 基于像素的字体大小 45 | float spaceRatio; // 空格字符与文字字符的占比(相对于字符) 46 | float fontRatio; // 字符与字符之间空间的占比(相对于字符) 47 | float fontRotateAngle; // 字符旋转的角度 48 | float fontDiaphaneity; // 字符透明度(与背景融合占比) 49 | bool fontIsUnderline; // 下划线标志 50 | bool fontIsVertical; // 竖直方向字符标志 51 | ``` 52 | 53 | 这些属性都可以通过类的 `get*-` 和 `set*-` 设置和获取对应的属性。默认情况下(初始化) 54 | 55 | ```cpp 56 | fontSize = 32; 57 | spaceRatio = 0.5f; 58 | fontRatio = 0.1f; 59 | fontRotateAngle = 0; 60 | fontDiaphaneity = 1; 61 | fontIsUnderline = false; 62 | fontIsVertical = false; 63 | ``` 64 | 65 | 主要的画字符的函数为 66 | 67 | * putText: 画文字字符 68 | * putSymbols: 画多个符号字符 69 | * putOneSymbol: 画单个符号字符 70 | 71 | ```cpp 72 | void putText(cv::Mat& img, const std::string& text, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color); 73 | void putSymbols(cv::Mat& img, std::vector& symbols, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color); 74 | void putOneSymbol(cv::Mat& img, uint32_t symbol, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar &color); 75 | ``` 76 | 77 | 其参数和 OpenCV 自带的 `putText` 相似,为了区分,我将其放到了 `cvx` 命名空间中,因此调用时需要加上命名空间,例如: 78 | 79 | ```cpp 80 | cvx::putText(img, msg, cv::Point(40, 20), font, fontSize, cv::Scalar(0, 255, 0)); 81 | ``` 82 | 83 | 当需要画下划线、旋转字符或者竖直字符时,需要在调用 `putText` 函数之前设置对应的属性,这些属性在 `putText` 执行完之后会自动重新初始化,因此每次都需要单独设置,因为这几个属性相对来说用的很少,所以基本上不怎么设置。例如画竖直旋转字符时 84 | 85 | ```cpp 86 | void testVerticalRotate(cvx::CvxFont& font, cv::Mat& img) { 87 | // cv::RNG rng(0xFFFFFFFF); 88 | int fontSize = 32; 89 | float angle = -30; 90 | font.setVertical(true); 91 | font.setRotateAngle(angle); 92 | cv::String msg5 = "这是旋转字符"; 93 | cvx::putText(img, msg5, cv::Point(700, 100), font, fontSize, randomColor(rng)); 94 | } 95 | ``` 96 | 97 | ---- 98 | 99 | ## 字符集说明 100 | 101 | 对于中文文字直接输入文字即可,对于符号字符,需要查对应的 unicode 字符集得到对应的 unicode 编码。 102 | 103 | * [SegoeUISymbol 字符集](http://www.kreativekorp.com/charset/font/SegoeUISymbol/) : 这个 windows 系统下有对应的字体,可以直接拷贝过来用,该项目的字体中也有提供 104 | * [Unicode 12.1 Character Code Charts](http://www.unicode.org/charts/) : 这个里面包含各种各样的字符集 105 | 106 | Windows 上自带字符映射表。直接在开始菜单搜索 “字符映射表” 就能找到。 107 | 108 | ![](images/table001.png) 109 | 110 | ![](images/table002.png) 111 | 112 | 该应用可以查到对应字体中符号的 unicode 编码,这个编码在画图的时候会用到。 113 | --------------------------------------------------------------------------------