首页 > 其他分享 >自定义函数在LCD上显示一张不超过LCD像素大小的色深为 24bit的bmp图片

自定义函数在LCD上显示一张不超过LCD像素大小的色深为 24bit的bmp图片

时间:2024-05-12 20:19:20浏览次数:13  
标签:自定义 int bmp lcd unsigned LCD headinfo

设计程序实现在LCD上任意位置显示一张任意大小的色深为 24bit的bmp图片,要求图像不失真可以在开发板的LCD上显示。

头文件包含

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <strings.h>
//以1字节对齐方式对齐
#pragma pack(1)

// 定义BMP文件头部结构
typedef struct
{
	unsigned short bfType;
	unsigned int bfSize;
	unsigned short bfReserved1;
	unsigned short bfReserved2;
	unsigned int bfOffBits;
} BITMAPFILEHEADER;

typedef struct
{
	unsigned int biSize;
	int biWidth;  // 宽
	int biHeight; // 高
	unsigned short biPlanes;
	unsigned short biBitCount; // 色深
	unsigned int biCompression;
	unsigned int biSizeImage;
	int biXPelsPerMeter;
	int biYPelsPerMeter;
	unsigned int biClrUsed;
	unsigned int biClrImportant;
} BITMAPINFOHEADER;
//取消字节对齐
#pragma pack()
/*******************************************************************
 *
 *	函数名称:	ShowBmp
 *	函数功能:   在LCD上任意位置显示一张任意大小的色深为 24bit的bmp图片
 *	函数参数:
 *				@name 	bmp图像文件名
 *				@x		图像显示的起点x轴坐标
 *				@y		图像显示的起点y轴左边
 *				@lcd_mp	lcd屏内存映射的地址
 *	返回结果:
 * 	注意事项:   None
 * 	函数作者:   [email protected]
 *	创建日期:   2024/05/12
 *	修改历史:
 *	函数版本:	V1.0
 * *****************************************************************/
int ShowBmp(char *name, int x, int y, int *lcd_mp)
{
	// 1.打开待显示的BMP图像  fopen
	FILE *bmp_fp = fopen(name, "rb");
	if (NULL == bmp_fp)
	{
		printf("open FILE is error!\n");
		return -1;
	}
	// 2.读取BMP文件的图像信息,获取BMP的宽和高
	BITMAPINFOHEADER headinfo;
	fseek(bmp_fp, 14, SEEK_SET);
	fread(&headinfo, 1, 40, bmp_fp); // 读取40字节
	// 打开LCD   open
	int lcd_fd = open("/dev/fb0", O_RDWR);
	if (lcd_fd == -1)
	{
		printf("mmap for lcd is error\n");
		return -1;
	}
	// 调用LCD屏的像素
	struct fb_var_screeninfo lcd_vinfo;
	ioctl(lcd_fd, FBIOGET_VSCREENINFO, &lcd_vinfo);
	// 3.读取BMP图片的颜色分量
	char bmp_buf[headinfo.biWidth * headinfo.biHeight * 3];
	bzero(bmp_buf, headinfo.biWidth * headinfo.biHeight * 3);
	fread(bmp_buf, 1, headinfo.biWidth * headinfo.biHeight * 3, bmp_fp);
	printf("bmp width = %d,height = %d\n", headinfo.biWidth, headinfo.biHeight);
	// 4.关闭BMP
	fclose(bmp_fp);
	// 5.循环将bmp图像写入lcd屏指定位置
	int data = 0;
	int i = 0;
	for (int h = (y + headinfo.biHeight - 1); h >= y; h--)
	{
		for (int w = x; w < (x + headinfo.biWidth); w++)
		{
			data |= bmp_buf[i];
			data |= bmp_buf[i + 1] << 8;
			data |= bmp_buf[i + 2] << 16;
			lcd_mp[h * (lcd_vinfo.xres) + w] = data;
			i += 3;
			data = 0;
		}
	}
	// 关闭LCD
	close(lcd_fd);
	munmap(lcd_mp, lcd_vinfo.xres * lcd_vinfo.yres * 4);
	return 0;
}
int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		printf("argument is error!\n");
		return -1;
	}
	if (NULL == argv[1])
	{
		printf("argument 2 is error!\n");
		return -1;
	}
	// 打开LCD   open
	int lcd_fd = open("/dev/fb0", O_RDWR);
	if (lcd_fd == -1)
	{
		printf("mmap for lcd is error\n");
		return -1;
	}
	// 调用LCD屏的像素
	struct fb_var_screeninfo lcd_vinfo;
	ioctl(lcd_fd, FBIOGET_VSCREENINFO, &lcd_vinfo);
	// 对LCD进行内存映射  mmap
	int *lcd_mp = (int *)mmap(NULL,
							  lcd_vinfo.xres * lcd_vinfo.yres * 4,
							  PROT_READ | PROT_WRITE,
							  MAP_SHARED,
							  lcd_fd,
							  0);
	// 键盘输入图片显示的起始位置
	int x, y;
	scanf("%d%d", &x, &y);
	ShowBmp(argv[1], x, y, lcd_mp);
	// 关闭LCD
	munmap(lcd_mp, lcd_vinfo.xres * lcd_vinfo.yres * 4);
	close(lcd_fd);
	return 0;
}

测试结果

image
image

标签:自定义,int,bmp,lcd,unsigned,LCD,headinfo
From: https://www.cnblogs.com/lzlwyh/p/18188121

相关文章

  • 在 LCD 上任意位置显示一张任意大小
    /***************************************************************************************filename:1.c*author: [email protected]*date:2024/05/12*function: 设计程序,实现在LCD上任意位置显示一张任意大小*note :none*......
  • 在lcd屏幕上的任意位置显示任意大小的图片
    /***************************************************filename:ShowBmp2.c*author:[email protected]*date:2024/05/12*brief:在lcd屏幕上的任意位置显示任意大小的图片*note:None**CopyRight(c)[email protected]......
  • 自定义各类基础排序算法
    接口函数基础信息/********************************************************************* 文件名称: 数据结构中对于无序数列的排序算法* 文件作者:[email protected]* 创建日期:2024/05/11* 文件功能:对无序数列进行排序* 注意事项:None** C......
  • Blazor WebAssembly使用 AuthenticationStateProvider 自定义身份认证
    本文章以客户端基础,实现类似后台系统,进入后台控制台页面需要经过登录身份验证才可访问情况简单来时就是实现前后端分离,前端通过token和用户信息进行身份认证,或者在 AuthenticationStateProvider 实现方法 GetAuthenticationStateAsync 中调用后台接口进行身份验证安装依......
  • dotnet 使用自定义特性
    namespaceTETTD.Common{///<summary>///导入excel特性标记字段映射的列///</summary>[AttributeUsage(AttributeTargets.Property|AttributeTargets.Field,AllowMultiple=false)]publicclassReadAttribute:Attribute{p......
  • C语言实现获取BMP文件信息
    通过命令行传递文件路径参数,输出BMP的width、height、size1、从BMP的结构信息可知,文件大小、图片宽度、图片高度均占据4个字节,所以只需要打开文件读取对应位置的信息并打印即可。2、为了提高可移植性,可以定义结构体,保持和BMP文件结构一致,这样可以一次性读取保存,方便后续调用。但......
  • 系统IO下查看bmp照片信息
    IO编程用系统IO实现查看bmp照片信息设计程序,利用系统IO读取磁盘上指定BMP图片的宽和高,以及BMP图片的大小,并输出到终端,要求图片名称通过命令行传递。/******************************************************************************filename:2024-05-11_GetBmpInfo.c......
  • GETbmpinfo
    /*************************************************/***@filename: GETbmpinfo*@brief实现对BMP图片位图信息的获取*@[email protected]*@date2024/05/11*@version1.0:在下坂本,有何贵干*@property:none*@notenone*CopyRigh......
  • BMP图片内部结构
    BMP图片内部结构​ BMP文件的数据按照从文件头开始的先后顺序分为四个部分:分别是位图文件头、位图信息头、调色板(24bit位图是没有的)、位图数据(RGB)。(1)位图文件头(Bitmap-FileHeader)包含了图像类型、图像大小、两个保留字以及位图数据存放地址。(2)位图信息头(Bitmap-InformationH......
  • bmp位图文件信息结构体
    /***************************************************filename:BmpInfoStruct.c*author:[email protected]*date:2024/05/11*brief:构造一个结构体用于存储bmp图片的文件信息*note:None**CopyRight(c)2024momolyl@126.......