现在的位置: 首页 > 综合 > 正文

C语言实现的在framebuff上面显示图片

2014年08月07日 ⁄ 综合 ⁄ 共 1953字 ⁄ 字号 评论关闭

/**************************************************
//初始化framebuffer,和在framebuffer上面显示图片数据
*************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/input.h>   
#include <time.h>
#include <stdlib.h>

//framebuffer
char *fb_addr;
int screen_fbd = 0;
struct fb_fix_screeninfo fb_fix;//这两个用于获取framebuffer相关信息的
struct fb_var_screeninfo fb_var;
unsigned fb_size; //buffer的长度
char *env = NULL;//fb的路径

//初始化framebuffer函数
int initFramebuffer(void)
{

        if(!(env = getenv("FRAMEBUFFER"))) //getenv()用来取得参数envvar环境变量的内容,如果没有这样的变量,默认framebuffer路径是下面的
        {
        env = "/dev/fb0";
        }
        
        screen_fbd = open(env, O_RDWR);       //以可读可写的方式打fb0       
        if(screen_fbd < 0) 
        {
                return 0;
        }
         //ioctl:控制I/O设备 ,提供了一种获得设备信息和向设备发送控制参数的手段       
        if(ioctl(screen_fbd, FBIOGET_FSCREENINFO, &fb_fix) == -1) 
        {//返回与Framebuffer有关的固定的信息,比如图形硬件上实际的帧缓存空间的大小、
               close(screen_fbd);
               return 0;
        }        
        if(ioctl(screen_fbd, FBIOGET_VSCREENINFO, &fb_var) == -1) 
        {//而后者返回的是与Framebuffer有关的可变信息,可变的信息就是指Framebuffer的长度、宽度以及颜色深度等信息
              close(screen_fbd);
              return 0;
        } //应该是位深度吧,用几位表示一个像素点
        fb_size = fb_var.yres * fb_fix.line_length;        
        fb_addr = (char *)mmap(NULL, fb_size, PROT_READ|PROT_WRITE,MAP_SHARED, screen_fbd, 0);
       return 1;     
}
/*
写数据文件到LCD的freamebuffer中
参数1:解码后的图片数据的指针
参数2:要在LCD上显示的位置的X坐标
参数3:要在LCD上显示的位置的Y坐标
参数4:图片宽度
参数5:图片高度
*/
void write_buf_image(unsigned char *gimage,int x,int y,int width,int height)
{ short *buff = (short*)fb_addr;
short *buffer  = buff;
int i = 0,j = 0,a = 0;
for(j = 0; (j < height)&&(y+j)<272; j++ )
{
buff = (buffer+x)+480*(j+y);

for(i = 0; ((i < width)&&((x+i)<480)); i++)    
{
*buff++ = *(gimage+a+1)<<8|*(gimage+a);

a++;
a++;   
}
a=width*2*j;
}
}
/*
int main(void)
{
int i;
initFramebuffer();

unsigned char *image=(char*)malloc(480*272*2);
for(i=0; i<480*272*2; i++)
image[i] = 0xff;

write_buf_image(image,0,0,480,272);

return 0;
}
*/

抱歉!评论已关闭.