摘自:http://blog.csdn.net/luxiaoxun/article/details/7622988
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
//14byte檔案頭
typedef struct
{
char cfType[2];//檔案類型,"BM"(0x4D42)
long cfSize;//檔案大小(位元組)
long cfReserved;//保留,值為0
long cfoffBits;//資料區相對于檔案頭的偏移量(位元組)
}__attribute__((packed)) BITMAPFILEHEADER;
//__attribute__((packed))的作用是告訴編譯器取消結構在編譯過程中的優化對齊
//40byte資訊頭
typedef struct
{
char ciSize[4];//BITMAPFILEHEADER所占的位元組數
long ciWidth;//寬度
long ciHeight;//高度
char ciPlanes[2];//目标裝置的位平面數,值為1
int ciBitCount;//每個像素的位數
char ciCompress[4];//壓縮說明
char ciSizeImage[4];//用位元組表示的圖像大小,該資料必須是4的倍數
char ciXPelsPerMeter[4];//目标裝置的水準像素數/米
char ciYPelsPerMeter[4];//目标裝置的垂直像素數/米
char ciClrUsed[4]; //位圖使用調色闆的顔色數
char ciClrImportant[4]; //指定重要的顔色數,當該域的值等于顔色數時(或者等于0時),表示所有顔色都一樣重要
}__attribute__((packed)) BITMAPINFOHEADER;
typedef struct
{
unsigned short blue;
unsigned short green;
unsigned short red;
unsigned short reserved;
}__attribute__((packed)) PIXEL;//顔色模式RGB
BITMAPFILEHEADER FileHead;
BITMAPINFOHEADER InfoHead;
static char *fbp = 0;
static int xres = 0;
static int yres = 0;
static int bits_per_pixel = 0;
int show_bmp();
int main ( int argc, char *argv[] )
{
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
struct fb_bitfield red;
struct fb_bitfield green;
struct fb_bitfield blue;
//打開顯示裝置
fbfd = open("/dev/fb0", O_RDWR);
if (!fbfd)
{
printf("Error: cannot open framebuffer device.\n");
exit(1);
}
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo))
{
printf("Error:reading fixed information.\n");
exit(2);
}
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))
{
printf("Error: reading variable information.\n");
exit(3);
}
printf("R:%d,G:%d,B:%d \n", vinfo.red, vinfo.green, vinfo.blue );
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
xres = vinfo.xres;
yres = vinfo.yres;
bits_per_pixel = vinfo.bits_per_pixel;
//計算螢幕的總大小(位元組)
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
printf("screensize=%d byte\n",screensize);
//對象映射
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if ((int)fbp == -1)
{
printf("Error: failed to map framebuffer device to memory.\n");
exit(4);
}
printf("sizeof file header=%d\n", sizeof(BITMAPFILEHEADER));
printf("into show_bmp function\n");
//顯示圖像
show_bmp();
//删除對象映射
munmap(fbp, screensize);
close(fbfd);
return 0;
}
int show_bmp()
{
FILE *fp;
int rc;
int line_x, line_y;
long int location = 0, BytesPerLine = 0;
char tmp[1024*10];
fp = fopen( "./niu.bmp", "rb" );
if (fp == NULL)
{
return( -1 );
}
rc = fread( &FileHead, sizeof(BITMAPFILEHEADER),1, fp );
if ( rc != 1)
{
printf("read header error!\n");
fclose( fp );
return( -2 );
}
//檢測是否是bmp圖像
if (memcmp(FileHead.cfType, "BM", 2) != 0)
{
printf("it's not a BMP file\n");
fclose( fp );
return( -3 );
}
rc = fread( (char *)&InfoHead, sizeof(BITMAPINFOHEADER),1, fp );
if ( rc != 1)
{
printf("read infoheader error!\n");
fclose( fp );
return( -4 );
}
//跳轉的資料區
fseek(fp, FileHead.cfoffBits, SEEK_SET);
//每行位元組數
BytesPerLine = (InfoHead.ciWidth * InfoHead.ciBitCount + 31) / 32 * 4;
line_x = line_y = 0;
//向framebuffer中寫BMP圖檔
while(!feof(fp))
{
PIXEL pix;
unsigned short int tmp;
rc = fread( (char *)&pix, 1, sizeof(PIXEL), fp);
if (rc != sizeof(PIXEL))
break;
location = line_x * bits_per_pixel / 8 + (InfoHead.ciHeight - line_y - 1) * xres * bits_per_pixel / 8;
//顯示每一個像素
*(fbp + location + 0)=pix.blue;
*(fbp + location + 1)=pix.green;
*(fbp + location + 2)=pix.red;
*(fbp + location + 3)=pix.reserved;
line_x++;
if (line_x == InfoHead.ciWidth )
{
line_x = 0;
line_y++;
if(line_y == InfoHead.ciHeight)
break;
}
}
fclose( fp );
return( 0 );
}
注意:上面的程式隻在framebuffer上顯示圖檔,卻沒有删除重新整理螢幕,可以使用下面的指令恢複螢幕
儲存螢幕資訊:dd if=/dev/fb0 of=fbfile 或: cp /dev/fb0 fbfile
恢複螢幕資訊:dd if=fbfile of=/dev/fb0 或: cat fbfile > /dev/fb0
FrameBuffer系列 之 介紹
http://blog.csdn.net/younger_china/article/details/14479859
FrameBuffer系列 之 相關結構與結構體
http://blog.csdn.net/younger_china/article/details/14480967
FrameBuffer系列 之 簡單程式設計
http://blog.csdn.net/younger_china/article/details/14236251
FrameBuffer系列之顯示圖檔
http://blog.csdn.net/younger_china/article/details/14481755
FrameBuffer系列之一點資源
http://blog.csdn.net/younger_china/article/details/14482049
轉載于:https://www.cnblogs.com/youngerchina/p/5624535.html