打開bmp文件
//打開.bmp文件
FILE *fb = fopen(bmpname, "rb");
if (fb == NULL)
{
printf("fopen bmp errorrn");
return -1;
}
獲取lcd參數(shù)
查看<linux/fb.h>
struct fb_var_screeninfo {
__u32 xres; /* visible resolution */
__u32 yres;
__u32 xres_virtual; /* virtual resolution */
__u32 yres_virtual;
__u32 xoffset; /* offset from virtual to visible */
__u32 yoffset; /* resolution */
__u32 bits_per_pixel; /* guess what */
__u32 grayscale; /* 0 = color, 1 = grayscale, */
/* >1 = FOURCC */
struct fb_bitfield red; /* bitfield in fb mem if true color, */
struct fb_bitfield green; /* else only length is significant */
struct fb_bitfield blue;
struct fb_bitfield transp; /* transparency */
__u32 nonstd; /* != 0 Non standard pixel format */
__u32 activate; /* see FB_ACTIVATE_* */
__u32 height; /* height of picture in mm */
__u32 width; /* width of picture in mm */
__u32 accel_flags; /* (OBSOLETE) see fb_info.flags */
/* Timing: All values in pixclocks, except pixclock (of course) */
__u32 pixclock; /* pixel clock in ps (pico seconds) */
__u32 left_margin; /* time from sync to picture */
__u32 right_margin; /* time from picture to sync */
__u32 upper_margin; /* time from sync to picture */
__u32 lower_margin;
__u32 hsync_len; /* length of horizontal sync */
__u32 vsync_len; /* length of vertical sync */
__u32 sync; /* see FB_SYNC_* */
__u32 vmode; /* see FB_VMODE_* */
__u32 rotate; /* angle we rotate counter clockwise */
__u32 colorspace; /* colorspace for FOURCC-based modes */
__u32 reserved[4]; /* Reserved for future compatibility */
};
頭文件中,用fb_var_screeninfo結(jié)構(gòu)體定義屏幕參數(shù)基本信息,如分辨率,像素點(diǎn)位數(shù)及RGB分別用多少位表示。
獲取結(jié)構(gòu)體fb_var_screeninfo
//獲取屏幕信息
//若屏幕顯示區(qū)域大小不合適,可用ioctl(dev_fd, FBIOPUT_VSCREENINFO, &scrinfo)設(shè)置
if (ioctl(dev_fd, FBIOGET_VSCREENINFO, &scrinfo))
{
printf("get screen infomation error!rn");
return -1;
}
使用ioctl的參數(shù),F(xiàn)BIOGET_VSCREENINFO來表示獲得屏幕的可變信息。
映射framebuffer
//計(jì)算需要的映射內(nèi)存大小
screensize = scrinfo.xres_virtual * scrinfo.yres_virtual * scrinfo.bits_per_pixel / 8;
// printf("screensize=%lu!rn", screensize);
//內(nèi)存映射
fbp = (char *)mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
if (-1 == (int)fbp)
{
printf("mmap error!rn");
return -1;
}
首先計(jì)算需要的映射內(nèi)存大小,計(jì)算公式為:映射內(nèi)存大小 = 屏幕大小 = xres * yres * 每個(gè)像素位數(shù) / 8。然后使用mmap映射內(nèi)存,PROT_READ | PROT_WRITE表示該區(qū)域可讀、可寫;MAP_SHARED表示該區(qū)域是共享的,APP寫入數(shù)據(jù)時(shí),會(huì)直達(dá)驅(qū)動(dòng)程序。到這一步之后就做好顯示圖片的準(zhǔn)備了。
閱讀全文