linux bmp添加字符,linux 将数字符号画到BMP图片上

首先我本先了解一下BMP图片的格式。

BMP文件主要分为如下4个部分: 块名称

对应Windows结构体定义                                                    大小(Byte)

文件信息头 BITMAPFILEHEADER                  14

位图信息头 BITMAPINFOHEADER                  40

颜色表(调色板)RGBQUAD                   (可选)

位图数据(RGB颜色阵列) BYTE* 由图像长宽尺寸决定 第1~2位:424dh = 'BM',表示这是Windows支持的位图格式。

第3~5位:代表文件大小。如00010436h = 66614 B = 65.05 kB

第6~9位:保留段,为0。

第A~D位:即从文件头到位图数据需偏移字节数。

共14字节。

0E~11:00000028h = 40,这个位图信息头的大小为40个字节。

12~15:图片宽度。(像素)

16~19:图片高度。(像素)

1A~1B:0001h。

1C~1D:0008h,代表每个像素占8比特。

1E~21:00000000h,代表图像不压缩。

22-25:00000000h。

26-29:00000000h。

2A-2D:00000000h。

2E-31:00000100h=256,说明本位图实际使用的颜色索引数为256。

32-35:00000100h=256,说明本位图重要的颜色索引数为256。

位图数据(RGB颜色阵列)就是我们给入的图像阵列,本程序是从ASC16点阵字库中取得。

注意:linux汉字使用的是utf-8,如果从HZK16点阵字库中取得汉字阵列需要转化成GB2321码,本文不做讲解。

注:

int GenBmpFile(char *pData, char bitCountPerPix, int width, int height, const char *filename) 生成BMP图片函数以及对应的结构体是从

http://www.cnblogs.com/shengansong/archive/2011/09/23/2186409.html摘抄而来,具体可转此连接查看。

PutBMP(FILE *fp16,char *str) 函数是将数组中的数字的ASCII码乘以16获取数字在字库中的区位码,获取改数字的阵列,赋值给RGB结构体,最后通过GenBmpFile()函数生成对应数字的bmp图片。

#include

#include

#include

#pragma pack(push, 1)

typedef struct tagBITMAPFILEHEADER

{

short bfType;

int bfSize;

short bfReserved1;

short bfReserved2;

int bfOffBits;

} BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER

{

int biSize;

int biWidth;

int biHeight;

short biPlanes;

short biBitCount;

int biCompression;

int biSizeImage;

int biXPelsPerMeter;

int biYPelsPerMeter;

int biClrUsed;

int biClrImportant;

} BITMAPINFOHEADER;

typedef struct tagRGBQUAD

{

char rgbBlue;

char rgbGreen;

char rgbRed;

char rgbReserved;

} RGBQUAD;

typedef struct tagBITMAPINFO

{

BITMAPINFOHEADER bmiHeader;

RGBQUAD bmiColors[1];

} BITMAPINFO;

typedef struct tagBITMAP

{

BITMAPFILEHEADER bfHeader;

BITMAPINFO biInfo;

}BITMAPFILE;

#pragma pack(pop)

int GenBmpFile(char *pData, char bitCountPerPix, int width, int height, const char *filename)

{

FILE *fp = fopen(filename, "wb");

if(!fp)

{

printf("fopen failed : %s, %d\n", __FILE__, __LINE__);

return 0;

}

int bmppitch = ((width*bitCountPerPix + 31) >> 5) << 2;

int filesize = bmppitch*height;

BITMAPFILE bmpfile;

bmpfile.bfHeader.bfType = 0x4D42;

bmpfile.bfHeader.bfSize = filesize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

bmpfile.bfHeader.bfReserved1 = 0;

bmpfile.bfHeader.bfReserved2 = 0;

bmpfile.bfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

bmpfile.biInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

bmpfile.biInfo.bmiHeader.biWidth = width;

bmpfile.biInfo.bmiHeader.biHeight = height;

bmpfile.biInfo.bmiHeader.biPlanes = 1;

bmpfile.biInfo.bmiHeader.biBitCount = bitCountPerPix;

bmpfile.biInfo.bmiHeader.biCompression = 0;

bmpfile.biInfo.bmiHeader.biSizeImage = 0;

bmpfile.biInfo.bmiHeader.biXPelsPerMeter = 0;

bmpfile.biInfo.bmiHeader.biYPelsPerMeter = 0;

bmpfile.biInfo.bmiHeader.biClrUsed = 0;

bmpfile.biInfo.bmiHeader.biClrImportant = 0;

fwrite(&(bmpfile.bfHeader), sizeof(BITMAPFILEHEADER), 1, fp);

fwrite(&(bmpfile.biInfo.bmiHeader), sizeof(BITMAPINFOHEADER), 1, fp);

char *pEachLinBuf = (char*)malloc(bmppitch);

memset(pEachLinBuf, 0, bmppitch);

char BytePerPix = bitCountPerPix >> 3;

int pitch = width * BytePerPix;

if(pEachLinBuf)

{

int h,w;

for(h=height-1; h>=0; h--)

{

for(w=0; w

{

//copy by a pixel

pEachLinBuf[w*BytePerPix+0] = pData[h*pitch + w*BytePerPix + 0];

pEachLinBuf[w*BytePerPix+1] = pData[h*pitch + w*BytePerPix + 1];

pEachLinBuf[w*BytePerPix+2] = pData[h*pitch + w*BytePerPix + 2];

}

fwrite(pEachLinBuf, bmppitch, 1, fp);

}

free(pEachLinBuf);

}

fclose(fp);

return 1;

}

typedef struct _LI_RGB

{

char b;

char g;

char r;

}LI_RGB;

int PutBMP(FILE *fp16,char *str)

{

const unsigned char bit[8]={128,64,32,16,8,4,2,1};

int x,y,wid;

int WIDTH=strlen(str)*8,HEIGHT=16;

long len;

char buf[32];

LI_RGB pRGB[HEIGHT][WIDTH]; // 定义位图数据

memset(pRGB, 0XFF, sizeof(pRGB)); // 设置背景为白色

for(wid=0;wid

{

len=str[wid]*16;

fseek(fp16,len,SEEK_SET);

fread(buf,1,16,fp16);

for (y=0;y<16;y++)

{

for (x=0;x<8;x++)

{

if (buf[y+x/8]&bit[x%8])

{

pRGB[y][wid*8+x].b = 0x00;

pRGB[y][wid*8+x].g = 0x00;

pRGB[y][wid*8+x].r = 0x00;

}

}

}

}

GenBmpFile((char*)pRGB, 24, WIDTH, HEIGHT, "out.bmp");//生成BMP文件

return 1;

}

int main(char argc, char *argv[])

{

char buf[30]={"2014/10/15 17:06"};

FILE *fd = fopen("ASC16", "rb");

if(!fd)

{

printf("fopen failed : %s, %d\n", __FILE__, __LINE__);

return 0;

}

PutBMP(fd,buf);

fclose(fd);

return 1;

}

0818b9ca8b590ca3270a3433284dd417.png

这是最终程序生成的out.bmp图片。

——————————海鱼

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值