编程思路:
1、打开bmp图片获得fd文件描述符(open()函数)
2、偏移信息头54字节,lseek()函数
3、读取bmp图片BGR(与RGB位置相反)信息,采用二维数组bmp_buf[480][800*3]
4、关闭文件fd
5、打开屏幕设备文件/dev/fb0(open(),读写操作)
6、BGR转ARGB,3转4操作,上下颠倒操作
7、将ARGB显示数据写到fb0 注意:ubantu字符界面可视分辨率800480,实际分辨率不是800480,要进行偏移
8、关闭屏幕
ubantu字符界面和图形界面的转换
Alt+Crtl+F1:图形界面—>字符界面(要输入用户名、密码)
Alt+Crtl+F7:字符界面—>图形界面
Alt+Crtl :释放光标
查看ubantu屏幕分辨率
#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>
int main()
{
struct fb_var_screeninfo info;
//打开LCD设备文件
int fd_lcd = open("/dev/fb0", O_RDWR );
if (-1 == fd_lcd){
// printf(“open LCD error \n”);
perror(“open LCD error “);
return -1 ;
}
ioctl(fd_lcd, FBIOGET_VSCREENINFO, &info);
printf(”%s\n”, “hello world!”);
printf(“可视分辨率:%d %d \n”,info.xres,info.yres);
printf(“实际分辨率:%d %d \n”,info.xres_virtual,info.yres_virtual);
}
我的运行结果是:
可视分辨率:800 x 600
实际分辨率:1176 x 885
所以步骤7的每一行的偏移量是:(1176-800)*4个字节
测试图片:分辨率800*480,bmp格式
实现代码
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
int main(int argv,char *argc[])
{
unsigned char bmp_buf[480][800*3]={0};
int lcd_buf[480][800]={0};
/* open bmp to get fd. */
printf("%s\n",argc[1] );
int bmp_fd = open(argc[1],O_RDWR);
/* jump bmp head info size of 54 Bytes. */
lseek(bmp_fd,54,SEEK_SET);
/* read bmp RGB info to bmp_buf and print read_numBytes. */
read(bmp_fd,bmp_buf,800*480*3);
close(bmp_fd);
/* open screen file. */
int fb0_fd = open("/dev/fb0",O_WRONLY);
/* BGR to ARGB and Image rollovers. */
for(int Bmp_H=0;Bmp_H<480;Bmp_H++){
for (int Bmp_W = 0; Bmp_W<800; Bmp_W++)
lcd_buf[479-Bmp_W ][Bmp_W]=0x00<<24|bmp_buf[Bmp_H][Bmp_W*3]|bmp_buf[Bmp_H][Bmp_W*3+1]<<8|bmp_buf[Bmp_H][Bmp_W*3+2]<<16;
}
/* write ARGB info to fb0_fd.*/
for(int j=0;j<480;j++){
for(int i=0;i<800;i++)
write(fb0_fd,&lcd_buf[j][i],4);
/* screen offset. */
lseek(fb0_fd, 376*4, SEEK_CUR);
}
/*close fd. */
close(fb0_fd);
}
注:
1、运行代码的时候要加上图片的路径和名字(如/home/1.bmp)。
2、unsigned char bmp_buf[480][8003]不能设置为char bmp_buf[480][8003]有符号字符型数组,因为这会导致read()读出来的BGR数据有一部分丢失,导致显示出来的颜色不对。
3、用write()函数不能对屏幕进行整屏覆盖,导致有些画面漏点或者刷新速度慢。