linux开发板截图程序

今天因为工作需要,要截取开发板屏幕上面的图片,在网上找了几个可能是由于个人操作原因,都失败了,没办法自己想想吧,其实很简单,就是让屏幕显示的逆操作!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <asm/types.h> 
#include <linux/videodev2.h>
#include <linux/fb.h>


static unsigned int capframe = 0;
static unsigned char filename[30];
FILE *bmpFile;

unsigned char bmp_head_t[] = {
        0x42,0x4d,0x42,0x58,0x02,0x00,0x00,0x00,0x00,0x00,
        0x42,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0xf0,0x00,
        0x00,0x00,0x40,0x01,0x00,0x00,0x01,0x00,0x10,0x00,
        0x03,0x00,0x00,0x00,0x00,0x58,0x02,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0xf8,0x00,0x00,0xe0,0x07,
        0x00,0x00,0x1f,0x00,0x00,0x00
};


static int fb_bpp;
unsigned char bmp_head[54];

static int fb_fp = -1;
static char *fb_addr = NULL;

int width=0;
int height=0;

static inline int fb_init(void)
{
	int dev_fp = -1;
	int fb_size;
	struct fb_var_screeninfo vinfo;

	dev_fp = open("/dev/fb0", O_RDWR);
	if (dev_fp < 0) {
		perror("/dev/fb0");
		return -1;
	}
	if (ioctl(dev_fp, FBIOGET_VSCREENINFO, &vinfo)) {
	        printf("Error reading variable information.\n");
		exit(1);
	}
	width=vinfo.xres;
	height=vinfo.yres;
	fb_bpp=vinfo.bits_per_pixel;
	if(fb_bpp==24) fb_bpp=32;
	fb_size=width*height*fb_bpp/8;
	if ((fb_addr = (char*)mmap(0, fb_size, 
			PROT_READ | PROT_WRITE, MAP_SHARED, dev_fp, 0)) < 0) {
		perror("mmap()");
		return -1;
	}
	printf("%dx%d bpp:%d mmaped 0x%08x\n",width,height,fb_bpp,fb_addr);

	return dev_fp;
}

void writeImageToFile(unsigned int size)
{
	capframe++;
	sprintf(filename,"/test/0%d.bmp",capframe);
   	bmpFile=fopen(filename, "w+");
	if(fb_bpp == 16)
		fwrite(bmp_head_t,1,66,bmpFile);
	else
		fwrite(bmp_head,1,54,bmpFile);
   	fwrite(fb_addr,1,size,bmpFile);
   	fclose(bmpFile);
}

int main(int argc, char *argv[])
{
	unsigned int i = 10;
	unsigned long size = 0;
	fb_fp = fb_init();	
	size=width*height*fb_bpp/8;

	if(fb_bpp==16){
	    *((unsigned int*)(bmp_head_t+18)) = width;
	    *((unsigned int*)(bmp_head_t+22)) = height;
	    *((unsigned short*)(bmp_head+28)) = 16;
	}else{
	bmp_head[0] = 'B';
	bmp_head[1] = 'M';
	*((unsigned int*)(bmp_head+2)) = (width*fb_bpp/8)*height+54;
	*((unsigned int*)(bmp_head+10)) = 54;
	*((unsigned int*)(bmp_head+14)) = 40;
	*((unsigned int*)(bmp_head+18)) = width;
	*((unsigned int*)(bmp_head+22)) = height;
	*((unsigned short*)(bmp_head+26)) = 1;
	*((unsigned short*)(bmp_head+28)) = fb_bpp;
	*((unsigned short*)(bmp_head+34)) = (width*fb_bpp/8)*height;
	}

	while(i--)
	{
		
		writeImageToFile(size);

	}
	 

	return 0;
}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页