在Linux上,如何绘制文本到位图?

假设我已经分配了一个二维数组int bitmap [WIDTH] [HEIGHT]。 我希望让Linux系统在这个位图缓冲区中画一行文本,比如说“Hello World”,就好像Linux在屏幕上打印正常文本一样,这样我就可以提取文本的位图模式,并将其与我的其他位图图像。 那么,请您分享一个适用于Linux的C ++代码片段吗? 非常感谢。

PS:我可以find一些代码来完成这个工作,但是他们在Windows上,而不是在Linux上。 PS2:如果你没有碰到代码,你可以教我如何编写代码,但是请期待Linux C ++编程中很多天真的问题。

您将需要选择一个C或C ++图形库。 Qt和SDL是两个不错的选择。

================================================== =======================

如果您只想绘制图像,另一个选择是libGD (以及前面提到的Qt和SDL )。

杰里就是一个例子:

https://cs.marlboro.edu/code/c/GD_example/GD_example.c

 include <stdio.h> #include <gd.h> // Dimensions of image in pixels #define IMAGE_WIDTH 300 #define IMAGE_HEIGHT 300 // The data to display is in a DATA_SIZE x DATA_SIZE array, // which will have its (left,top)=(x,y) corner at // (DATA_TOP, DATA_LEFT) pixels in from the (0,0)=(left,top) pixel. #define DATA_SIZE 8 #define DATA_LEFT 30 #define DATA_TOP 30 // Position of some blue lines drawn in the image. #define BORDER 10 #define LEFT BORDER #define RIGHT IMAGE_WIDTH - BORDER #define TOP BORDER #define BOTTOM IMAGE_HEIGHT - BORDER // See the bottom of this code for a discussion of some output possibilities. char* filename = "GD_example.png"; // Some values that'll go into the image as shades of gray. // Range is 0 to 255 (ie 8 bits, which is the standard range of intensity). // To do this sort of thing with floating point data or other data, // you'd first scale your numbers to be in this 0 to 255 range. int data[DATA_SIZE][DATA_SIZE] ={ { 2, 10, 10, 10, 10, 10, 10, 2}, { 10, 20, 30, 40, 40, 30, 20, 10}, { 10, 30, 100, 100, 100, 100, 30, 10}, { 10, 40, 100, 200, 200, 100, 40, 10}, { 10, 30, 100, 200, 200, 100, 30, 10}, { 10, 20, 100, 100, 100, 100, 20, 10}, { 10, 10, 20, 30, 40, 30, 10, 10}, { 2, 10, 10, 10, 10, 10, 10, 2} }; int main(){ FILE* outfile; // defined in stdio gdImagePtr image; // a GD image object int white, blue, gray[255]; // some GD colors int i, x, y; // array subscripts printf("=== GD example ===\n"); printf("Creating %i by %i image.\n", IMAGE_WIDTH, IMAGE_HEIGHT); image = gdImageCreate(IMAGE_WIDTH, IMAGE_HEIGHT); // Or image = gdImageCreateTrueColor(IMAGE_WIDTH, IMAGE_HEIGHT); // followed by colors like white=gdTrueColor(255,255,255) that don't // need to refer to any one image's color table. white = gdImageColorAllocate(image, 255,255,255); // 1st is background blue = gdImageColorAllocate(image, 0,0,255); // (red,green,blue) for (i=0; i<255; i++){ gray[i] = gdImageColorAllocate(image, i,i,i); } printf("Drawing some blue lines.\n"); gdImageLine(image, LEFT,TOP, RIGHT,TOP, blue); // draw lines in image gdImageLine(image, RIGHT,TOP, RIGHT,BOTTOM, blue); // +-----------------+ gdImageLine(image, RIGHT,BOTTOM, LEFT,BOTTOM, blue); // |0,0 WIDTH,0| gdImageLine(image, LEFT,BOTTOM, LEFT,TOP, blue); // |0,HEIGHT | // +-----------------+ printf("Filling in some gray pixels.\n"); for (x=0; x<DATA_SIZE; x++){ // fill some grayscale for (y=0; y<DATA_SIZE; y++){ // colors from data. gdImageSetPixel(image, x+DATA_LEFT, y+DATA_TOP, gray[data[x][y]]); } } // Finally, write the image out to a file. printf("Creating output file '%s'.\n", filename); outfile = fopen(filename, "wb"); gdImagePng(image, outfile); fclose(outfile); } 

后来我用OpenImageIO来完成这个任务。 C ++代码如下:

 #include <OpenImageIO/imageio.h> #include <OpenImageIO/imagebuf.h> #include <OpenImageIO/imagebufalgo.h> OIIO_NAMESPACE_USING const int WIDTH = 256, HEIGHT = 32, CHANNELS = 3; unsigned char buffer[WIDTH * HEIGHT * CHANNELS]; int main() { ImageBuf ib(ImageSpec(WIDTH, HEIGHT, CHANNELS), buffer); ImageBufAlgo::render_text(ib, 0 /* x */, 10 /* y */, "Hello, world!" /* text to draw */); for (int j = 0; j < HEIGHT; j++) for (int i = 0; i < WIDTH; i++) cout << buffer[(i+j*WIDTH)*CHANNELS+0] << "," //R << buffer[(i+j*WIDTH)*CHANNELS+1] << "," //G << buffer[(i+j*WIDTH)*CHANNELS+2] << endl; //B } 

位图模式可以如下图所示(使用ib.write("output.tif"); ):

在这里输入图像描述

感谢Larry Gritz帮助我。

PS:它在ubuntu 14.04和g ++ 4.8.4上测试过。 使用的链接选项是-lboost_system -lOpenImageIO

多数情况下,理解一个问题比回答更困难。