c++ - 如何在 BGR 图像的像素上绘制文本字符串,以便在显示时文本可读,c++?

标签 c++ image draw

有没有一种简单的方法可以修改 72x72 像素 BGR 图像的像素,使其包含显示图像时可读的文本字符串。

本质上,我需要在下面创建的图像缓冲区 img 上绘制 str 中的文本,以允许在显示图像时读取它的方式。

unsigned char img[72*72*3]; // 72*72*3 BGR image buffer
unsigned char B = 0x00; 
unsigned char G = 0x00;
unsigned char R = 0x00;
std::string str = "Test Text";

// Create BGR image
for (int i = 0; i < (72*72*3); i += 3)
{
    img[i + 0] = B;
    img[i + 1] = G;
    img[i + 2] = R;
}

// Draw str on BGR image buffer?

最佳答案

我会建议 CImg像这样:

#include <iostream>
#include <cstdlib>
#define cimg_display 0
#include "CImg.h"

using namespace cimg_library;
using namespace std;

int main() {
   // Create 72x72 RGB image
   CImg<unsigned char> image(72,72,1,3);

   // Fill with magenta
   cimg_forXY(image,x,y) {
      image(x,y,0,0)=255;
      image(x,y,0,1)=0;
      image(x,y,0,2)=255;
   }

   // Make some colours
   unsigned char cyan[]    = {0,   255, 255 };
   unsigned char black[]   = {0,   0,   0   };

   // Draw black text on cyan
   image.draw_text(3,20,"Test text",black,cyan,1,16);

   // Save result image as NetPBM PNM - no libraries required
   image.save_pnm("result.pnm");
}

enter image description here

它体积小、速度快、功能全面、现代 C++ 和“仅 header ”,这意味着您也不需要链接任何东西。

关于c++ - 如何在 BGR 图像的像素上绘制文本字符串,以便在显示时文本可读,c++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56446351/

相关文章:

c++ - 二维点的排列

c++ - Clang 的 libc++:在 std::chrono::duration 的定义中使用 long long

java - 通过 java 中的 url 检索单个图像

java - 绘制数千个粒子的更高效方法(Java/Android)

ios - 在 imageView 顶部绘制背景清晰的矩形

c++ - 在C++中获取嵌套的JSON数组和矩阵

c++ - 最大缓冲区数

html - 标题内带有 url 图片的表格

security - 安全地加载 Facebook 个人资料图片

swift - 将一条线分成 n 等份