#include ansi C中的图像

jsj*_*jsj 1 c image-file c-preprocessor

我正在编写一个创建图像文件的程序.我希望在程序的二进制文件中对"原始"图像进行硬编码.

我刚在想...

char image[] = {
#include"image.jpg"
}
Run Code Online (Sandbox Code Playgroud)

但是我需要以某种方式将图像转换为可以作为char数组#included到ac文件中的格式?

Ian*_*ton 7

好的,使用unix工具xxd创建给定二进制文件的ac char数组导入,如下所示:

$ xxd -i imgfile.dat > imgfile.h
Run Code Online (Sandbox Code Playgroud)

这应该产生如下输出:

unsigned char imgfile[] = {
  0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0a
};
unsigned int imgfile_len = 12;
Run Code Online (Sandbox Code Playgroud)

另请参阅:"#include"C程序中的文本文件作为char []