使用C语言自定义创建bmp图片

定义文件头和调色板

typedef struct {
    uint16_t type; //文件类型,必须是0x4D42,即字符串“BM”,也就是说所有.bmp文件的头两个字节都是“BM”
    uint32_t size; //bmp文件所占的空间
    uint16_t reserved1; //保留字1
    uint16_t reserved2; //保留字2
    uint32_t offset; //从文件头到实际数据处的偏移量
    uint32_t dib_header_size; //本结构体所使用的字节数为40字节
    int32_t  width; //bmp宽度
    int32_t  height; //bmp高度
    uint16_t planes; //目标设备级别,必须为1
    uint16_t bits_per_pixel;  //表示颜色时每个像素要用到的位数,常用的值为1(黑白二色图), 4(16色图), 8(256色), 24(真彩色图)
    uint32_t compression; //图片是否压缩,其类型是 0(BI_RGB不压缩),  1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)
    uint32_t image_size; //实际的bmp图片所占用的字节
    int32_t  x_pixels_per_meter; //水平像素
    int32_t  y_pixels_per_meter; //垂直像素
    uint32_t colors_used; //有多少颜色使用 0=2的(bits_per_pixel)次方种颜色
    uint32_t important_colors; //图像内的重要颜色数,0=所有都是重要的
} BMPHeader;

typedef struct {
    uint8_t blue;
    uint8_t green;
    uint8_t red;
} BMPColor;

fwrite函数使用格式:fwrite(buffer, size, count, fp);
{
buffer是存放数据的存储空间的起始地址;

size是数据块的大小(字节数);

count是读多少个块;

fp是文件类型指针
}

创建100×100黑白图像

void Creat_BlackWhite_BMP(const char* filename)
{
	//给文件头内的数据赋值
		BMPHeader header = 
	{
		.type = 0x4D42,
		.size = sizeof(BMPHeader) + 256 * 256 * sizeof(BMPColor), //使用sizeof函数获得BMPHeader所需要的字节数再加上调色盘×256次方的字节
		.reserved1 = 0,
        .reserved2 = 0,
        .offset = sizeof(BMPHeader),
        .dib_header_size = 40,
        .width = 100,
        .height = 100,
        .planes = 1,
        .bits_per_pixel = 24, //这里可以改为1,只有两种颜色
        .compression = 0,
        .image_size = 0,
        .x_pixels_per_meter = 0,
        .y_pixels_per_meter = 0,
        .colors_used = 0,
        .important_colors = 0
	};
		FILE* fp = fopen(filename, "wb"); //定义文件类型的指针fp,以”wb“打开模式打开文件名为filename的文件
		fwrite(&header, sizeof(BMPHeader), 1, fp); //将header的内容放入fp指向的文件内,所需要的内存通过sizeof函数自动计算好
		BMPColor pixel = {0, 0, 0}; //定义调色盘
		
		//写入黑白数据,一个像素一个像素的写入
		for(int x = 0;x < header.height; x++)
		{
			for(int y = 0;y < header.width; y++)
			{
				if (x < 50)
				{
					BMPColor pixel = {0, 0, 0};
					fwrite(&pixel, sizeof(BMPColor), 1, fp); //向内存里写入调色板颜色
				}
				else
				{
					BMPColor pixel = {255, 255, 255};
					fwrite(&pixel, sizeof(BMPColor), 1, fp); //向内存里写入调色板颜色
				}
			}
		}
		fclose(fp); //关闭文件

实现的100×100黑白图效果:请添加图片描述

创建100×100的256色图像

		//写入数据,一个像素一个像素的写入
		for(int x = 0;x < header.height; x++)
		{
			for(int y = 0;y < header.width; y++)
			{
				pixel.red = y; //可以用别的色,用红色就是以红色为基底的渐变,由于上限只有100,所以整体会偏黑
			
				fwrite(&pixel, sizeof(BMPColor), 1, fp); //向内存里写入调色板颜色
			}
		}

实现的256色图像效果:请添加图片描述

创建100×100的乱码图像

	srand(time(NULL)); //使用srand根据系统时间为rand生成不同的种子值
	
	//生成乱码图像
	for (int y = 0; y < header.height; y++) 
	{
    	for (int x = 0; x < header.width; x++) 
		{
			for (int i = 0; i < 256; i++) 
			{
				pixel.blue = rand() % 256;
    			pixel.green = rand() % 256;
    			pixel.red = rand() % 256;
    			
    			fwrite(&pixel, sizeof(BMPColor), 1, fp);
			}
    	}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在嵌入式Linux下使用C语言生成二维码bmp图片,可以通过以下步骤实现: 1. 安装libqrencode库 在嵌入式Linux系统,可以使用包管理器或手动编译安装libqrencode库,该库提供了生成二维码的API接口。 2. 编写代码生成二维码数据 使用libqrencode库提供的API接口,可以生成二维码数据。以下是一个简单的示例: ``` #include <stdio.h> #include <string.h> #include <qrencode.h> int main(int argc, char **argv) { QRcode *qrcode = QRcode_encodeString("hello world", 0, QR_ECLEVEL_L, QR_MODE_8, 1); if (qrcode) { for (int y = 0; y < qrcode->width; y++) { for (int x = 0; x < qrcode->width; x++) { if (qrcode->data[y*qrcode->width + x] & 1) { printf("*"); } else { printf(" "); } } printf("\n"); } QRcode_free(qrcode); } return 0; } ``` 上面的代码会将字符串"hello world"转换成二维码数据,并输出到终端。 3. 将二维码数据转换成bmp图片 将二维码数据转换成bmp图片可以使用图形库,如libgd。以下是一个简单的示例: ``` #include <stdio.h> #include <string.h> #include <qrencode.h> #include <gd.h> int main(int argc, char **argv) { QRcode *qrcode = QRcode_encodeString("hello world", 0, QR_ECLEVEL_L, QR_MODE_8, 1); if (qrcode) { int width = qrcode->width; gdImagePtr image = gdImageCreate(width, width); int black = gdImageColorAllocate(image, 0, 0, 0); int white = gdImageColorAllocate(image, 255, 255, 255); for (int y = 0; y < width; y++) { for (int x = 0; x < width; x++) { if (qrcode->data[y*width + x] & 1) { gdImageSetPixel(image, x, y, black); } else { gdImageSetPixel(image, x, y, white); } } } FILE *fp = fopen("qrcode.bmp", "wb"); gdImageBmp(image, fp, 0); fclose(fp); gdImageDestroy(image); QRcode_free(qrcode); } return 0; } ``` 上面的代码会将二维码数据转换成bmp图片,并保存到文件"qrcode.bmp"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值