将开发板中显示的图像保存为bmp

从/dev/fb0中获取数据保存为bmp文件

/********************************

File name : fbtools.h 

*/ 

#ifndef _FBTOOLS_H_ 

#define _FBTOOLS_H_ 

#include <linux/fb.h> 

typedef unsigned int  size_t ;

//a framebuffer device structure; 

typedef struct fbdev{ 

int fb; 

unsigned long fb_mem_offset; 

unsigned long fb_mem; 

struct fb_fix_screeninfo fb_fix; 

struct fb_var_screeninfo fb_var; 

char dev[20]; 

} FBDEV, *PFBDEV; 

//open & init a frame buffer 

//to use this function, 

//you must set FBDEV.dev="/dev/fb0" 

//or "/dev/fbX" 

//it's your frame buffer. 

int fb_open(PFBDEV pFbdev); 

//close a frame buffer 

int fb_close(PFBDEV pFbdev); 

//get display depth 

int get_display_depth(PFBDEV pFbdev); 

//full screen clear 

void fb_memset(void *addr, int c, size_t len); 

#endif 

/****************** 

File name : fbtools.c 

*/ 

#include <stdio.h> 

#include <stdlib.h> 

#include <fcntl.h> 

#include <unistd.h> 

#include <string.h> 

#include <sys/ioctl.h> 

#include <sys/mman.h> 

//#include <asm/page.h> 

//#include "fbtools.h" 

#define TRUE 1 

#define FALSE 0 

#define MAX(x,y) ((x)>(y)?(x)y))

#define MIN(x,y) ((x)<(y)?(x)y))

//open & init a frame buffer

#define PAGE_MASK 0xffff

int fb_open(PFBDEV pFbdev)

{

pFbdev->fb = open(pFbdev->dev, O_RDWR);

if(pFbdev->fb < 0)

{

	printf("Error opening %s: %m. Check kernel config\n", pFbdev->dev);

	return FALSE;

}

if (-1 == ioctl(pFbdev->fb,FBIOGET_VSCREENINFO,&(pFbdev->fb_var)))

{

	printf("ioctl FBIOGET_VSCREENINFO\n");

	return FALSE;

}

if (-1 == ioctl(pFbdev->fb,FBIOGET_FSCREENINFO,&(pFbdev->fb_fix)))

{

	printf("ioctl FBIOGET_FSCREENINFO\n");

	return FALSE; 

} 

//map physics address to virtual address 

pFbdev->fb_mem_offset = 0; //(unsigned long)(pFbdev->fb_fix.smem_start) & (~PAGE_MASK);

pFbdev->fb_mem = (unsigned long int)mmap(NULL, pFbdev->fb_fix.smem_len + pFbdev->fb_mem_offset, PROT_READ | PROT_WRITE, MAP_SHARED, pFbdev->fb, 0);

if (-1L == (long) pFbdev->fb_mem) 

{ 

	printf("mmap error! mem:%d offset:%d\n", pFbdev->fb_mem, pFbdev->fb_mem_offset); 

	return FALSE; 

} 

return TRUE; 

} 

//close frame buffer

int fb_close(PFBDEV pFbdev) 

{ 

	close(pFbdev->fb); 

	pFbdev->fb=-1; 

} 

//get display depth

int get_display_depth(PFBDEV pFbdev)
{ 

if(pFbdev->fb<=0) 

{ 

printf("fb device not open, open it first\n"); 

return FALSE; 

} 

return pFbdev->fb_var.bits_per_pixel; 

} 

//full screen clear 

void fb_memset (void *addr, int c, size_t len) 

{ 

memset(addr, c, len); 

} 

//use by test

#define DEBUG

//#ifdef DEBUG


typedef unsigned short WORD ;
typedef unsigned long DWORD ;
typedef  long LONG ;

#pragma pack(1)//c编译器按n个字节对齐

                


typedef struct tagBITMAPFILEHEADER {
  WORD  bfType;
  DWORD bfSize;
  WORD  bfReserved1;
  WORD  bfReserved2;
  DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;


typedef struct tagBITMAPINFOHEADER {
  DWORD biSize;
  LONG  biWidth;
  LONG  biHeight;
  WORD  biPlanes;
  WORD  biBitCount;
  DWORD biCompression;
  DWORD biSizeImage;
  LONG  biXPelsPerMeter;
  LONG  biYPelsPerMeter;
  DWORD biClrUsed;
  DWORD biClrImportant;
} BITMAPINFOHEADER;


#pragma pack()

main()

{

FBDEV fbdev;
FILE * fpout;
BITMAPFILEHEADER bmpFile;
BITMAPINFOHEADER bmpInfo ;

memset(&bmpFile, 0, sizeof(bmpFile));
memset(&bmpInfo, 0, sizeof(bmpInfo));
bmpFile.bfType = 'MB' ;
bmpFile.bfOffBits = sizeof(bmpFile) + sizeof(bmpInfo) ;
bmpInfo.biSize = sizeof(bmpInfo) ;

memset(&fbdev, 0, sizeof(FBDEV));
strcpy(fbdev.dev, "/dev/fb0");

if(fb_open(&fbdev)==FALSE)
{
	printf("open frame buffer error\n");
	return 0;
}


bmpInfo.biWidth=  fbdev.fb_var.xres_virtual ;
bmpInfo.biHeight =  - fbdev.fb_var.yres_virtual ;  
bmpInfo.biPlanes = 1 ;
bmpInfo.biBitCount= fbdev.fb_var.bits_per_pixel ;

bmpFile.bfSize = sizeof(bmpFile) + sizeof(bmpInfo) +  fbdev.fb_fix.smem_len ;

fpout=fopen("fbsnap.bmp", "w");
fwrite((char*)&bmpFile, sizeof(bmpFile) ,1, fpout);
fwrite((char*)&bmpInfo, sizeof(bmpInfo) ,1, fpout);
fwrite((char*)fbdev.fb_mem, fbdev.fb_fix.smem_len ,1, fpout);

fclose(fpout);

printf(" mem %p %p  len %d \n", fbdev.fb_mem, fbdev.fb_mem_offset, fbdev.fb_fix.smem_len);



fb_close(&fbdev);

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值