基于GEC 6818开发板的BMP图片播放器


一、现象:

基于6818开发板的BMP图片播放器




二、直接上代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/mman.h>
#include <linux/input.h>





int *plcd,*lcd_p;
int lcd_fd;

unsigned char bmppath[128][128]={
	"./pictures/1.bmp",
	"./pictures/2.bmp",
	"./pictures/3.bmp",
	"./pictures/4.bmp",
	"./pictures/5.bmp",
	"./pictures/6.bmp",
	"./pictures/7.bmp",
	"./pictures/8.bmp",
};


void  *lcd_init(){
	 lcd_fd=open("/dev/fb0",O_RDWR);
	
	if(lcd_fd==-1){
		perror("open lcd_file error\n");
		return MAP_FAILED;
	}
	
	plcd=(int *)mmap(NULL,
			 800*480*4,
			 PROT_READ|PROT_WRITE,
			 MAP_SHARED,
			 lcd_fd,
			 0
	);
	
	
	return plcd;
	
}


int uninit_lcd(){
	close(lcd_fd);
	
	if(munmap(lcd_p,800*480*4)==-1){
		return -1;
	}
	return 0;
}


void lcd_draw_point(int x,int y,int color){
	
	*(plcd+y*800+x) = color;
	
}


// 绘图(BMP格式)
// 下面的代码几乎是一套模板,照着抄就行
// 培训老师给的代码 ^_^
void LCD_DrawBMP(int x,int y,const char *bmpname){
	unsigned char buf[4]={0};
	
	int fd = open(bmpname,O_RDONLY);
	
	if(fd == -1){
		perror("open bmp file failed:");
		
		return;
	}
	
	lseek(fd,0,SEEK_SET);
	int ret=read(fd,buf,2);
	if(ret!=2){
		perror("LCD_DrawBMP-read bmp picture-error");
		return;
	}
	
    // 首先判断它是不是一个BMP图片
	if(buf[0]!=0x42 || buf[1]!=0x4D){
		printf("This image format is not bmp!\n");
		return;
		
	}
	
    // 读位图宽度
	int bmp_w=0;
	lseek(fd,0x12,SEEK_SET);
	ret=read(fd,&bmp_w,4);
	
	if(ret!=4){
		perror("read picture width error");
		return ;
	}
	
    // 读位图高度
	int bmp_h=0;
	lseek(fd,0x16,SEEK_SET);
	ret=read(fd,&bmp_h,4);
	
	if(ret!=4){
		perror("read picture length error");
		return;
	}
	

    // 读位图色深
	int bmp_colordepth=0;
	lseek(fd,0x1C,SEEK_SET);
	ret=read(fd,&bmp_colordepth,2);
	
	if(ret!=2){
		perror("read picture colordepth error");
		return;
	}
	
	//printf("bmp:%ld*%ld*%ld\n",bmp_w,bmp_h,bmp_colordepth);
	
	// 下面的代码用来读像素数组内容,并通过画点函数画出来
	lseek(fd,54,SEEK_SET);// 把光标偏移到像素数组位置
	
	int i,j;
	
	for(i=0;i<bmp_h;i++){
		for(j=0;j<bmp_w;j++){
			int color=0;
			read(fd,&color,bmp_colordepth/8);
			lcd_draw_point(x+j,y+(bmp_h>0?(bmp_h-1-i):i),color);//位图高度为正数时,会上下颠倒存放数据
		}
		lseek(fd,(4-bmp_colordepth/8*bmp_w%4)%4,SEEK_CUR);//跳过无用数据
	}
		
	
	 close(fd);
	
}


// 获取点击屏幕的坐标
int get_coordinate(int *x,int *y){
	struct input_event et;
	int fd = open("/dev/input/event0",O_RDONLY);

	if(fd == -1){
		perror("open even0 failed");
		return -1;
	}

	while(1){
		int r = read(fd,&et,sizeof et);
		if(r == sizeof et){
			if(et.type==EV_ABS && et.code==ABS_X){
				*x = et.value; // 保存x坐标
			}
			if(et.type==EV_ABS && et.code==ABS_Y){

				*y=et.value;  // 保存y坐标

			}
			if(et.type==EV_KEY&&et.code==BTN_TOUCH&&et.value==0){
					close(fd);
					return 0;
			}

		}
		else {
			perror(" get_coordinate-read-error");
			return -1;
		}

	}


	return 0;
}




int main()
{
	
	
	lcd_p=lcd_init();
	if(lcd_p == MAP_FAILED){
		perror(" lcd_init error\n");
		return -1;
	}
	else printf("lcd init succuss!\n");



	LCD_DrawBMP(0,0,"./pictures/-1.bmp");

	int i=-1;
	while(1){
		int x,y;

        // 得到点击屏幕处的坐标
		get_coordinate(&x,&y);
		
		printf("x=%d y=%d\n",x,y);


        // x和y的范围可以自己试出来,不一定要和我的一样

		if(x>=0&&x<=280&&y>=515&&y<=600){
			usleep(5000);
			i--;
			if(i<0) i=7;
			printf("use picture %d\n\n",i);
			LCD_DrawBMP(0,0,bmppath[i]);

		}
		else if(x>=745&&x<=1030&&y>=515&&y<=600){
			usleep(5000);
			i++;
			if(i>7) i=0;
			printf("use picture %d\n\n",i);
			LCD_DrawBMP(0,0,bmppath[i]);
		}


	}


    if(uninit_lcd()==-1){
		perror("munmap error!\n");
		return -1;
		
	};
	
	

	return 0;
}




三、打包文件:

请点我

// 使用方式
先在终端输入命令:chmod 777 kezhixingwenjian
再输入:./kezhixingwenjian
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gec6818开发板图片显示黑白重叠可能是由于以下原因之一导致的: 1. 图片格式不支持:开发板可能只支持特定的图片格式,如BMP格式。如果您尝试显示图片是其他格式,如PNG格式,可能会导致显示异常。\[2\] 2. 图片显示设置错误:开发板显示设置可能需要进行正确配置,以确保图片能够正确显示。您可以检查开发板的文档或参考相关的开发指南,了解如何正确设置图片显示参数。 3. 驱动问题:开发板的驱动程序可能存在问题,导致图片显示异常。您可以尝试更新或重新安装驱动程序,或者联系开发板的制造商获取技术支持。 请您根据具体情况检查以上可能的原因,并采取相应的解决措施来解决图片显示黑白重叠的问题。 #### 引用[.reference_title] - *1* *3* [GEC6818开发板使用和配置](https://blog.csdn.net/m0_45463480/article/details/124673151)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [GEC6818开发板显示png图片效果](https://blog.csdn.net/weixin_50722786/article/details/127183021)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值