zoukankan      html  css  js  c++  java
  • 在2440里面添加截屏功能

    gsnap.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <linux/fb.h>
    #include <linux/kd.h>
    #include <string.h>
    #include "jpeglib.h"
    
    struct FB 
    {
    	int fd;
    	unsigned size;
    	unsigned short *bits;
    	struct fb_fix_screeninfo fi;
    	struct fb_var_screeninfo vi;
    };
    
    #define fb_width(fb) ((fb)->vi.xres)
    #define fb_height(fb) ((fb)->vi.yres)
    #define fb_size(fb) ((fb)->vi.xres * (fb)->vi.yres * 2)
    
    static int fb_open(struct FB *fb, const char* fbfilename)
    {
    	fb->fd = open(fbfilename, O_RDWR);
    	if (fb->fd < 0)
    	{
    		fprintf(stderr, "can't open %s\n", fbfilename);
    		return -1;
    	}
    
    	if (ioctl(fb->fd, FBIOGET_FSCREENINFO, &fb->fi) < 0)
    		goto fail;
    	if (ioctl(fb->fd, FBIOGET_VSCREENINFO, &fb->vi) < 0)
    		goto fail;
    
    	fb->bits = mmap(0, fb_size(fb), PROT_READ | PROT_WRITE, 
    					MAP_SHARED, fb->fd, 0);
    	if (fb->bits == MAP_FAILED)
    		goto fail;
    
    	return 0;
    fail:
    	printf("%s is not a framebuffer.\n", fbfilename);
    	close(fb->fd);
    
    	return -1;
    }
    
    static void fb_close(struct FB *fb)
    {
    	munmap(fb->bits, fb_size(fb));
    	close(fb->fd);
    }
    
    static int snap(const char * filename, int quality, struct FB* fb)
    {
    	int row_stride = 0; 
    	const char fullpath[]="/PIC/";//默认存储路径
    	const char exname[]=".jpg"; //默认存储图片类型
    	FILE * outfile = NULL;
    	JSAMPROW row_pointer[1] = {0};
    	struct jpeg_error_mgr jerr = {0};
    	struct jpeg_compress_struct cinfo = {0};
    
    	cinfo.err = jpeg_std_error(&jerr);
    	jpeg_create_compress(&cinfo);
    	strcat(fullpath,filename);
    	strcat(fullpath,exname);
    
    	if ((outfile = fopen( fullpath, "wb+" )) == NULL) 
    	{
    		fprintf(stderr, "can't open %s\n", filename);
    
    		return -1;
    	}
    
    	jpeg_stdio_dest(&cinfo, outfile);
    	cinfo.image_width = fb_width(fb);
    	cinfo.image_height = fb_height(fb);
    	cinfo.input_components = 3;
    	cinfo.in_color_space = JCS_RGB;
    	jpeg_set_defaults(&cinfo);
    	jpeg_set_quality(&cinfo, quality, TRUE);
    	jpeg_start_compress(&cinfo, TRUE);
    
    	row_stride = fb_width(fb) * 2;
    	JSAMPLE* image_buffer = malloc(3 * fb_width(fb));
    
    	while (cinfo.next_scanline < cinfo.image_height) 
    	{
    		int i = 0;
    		unsigned short* line = fb->bits + cinfo.next_scanline * fb_width(fb);
    		for(i = 0; i < fb_width(fb); i++)
    		{
    			int offset = i * 3;
    			unsigned short color = line[i];
    			unsigned char r = ((color >> 11) & 0xff) << 3;
    			unsigned char g = ((color >> 5) & 0xff)  << 2;
    			unsigned char b = (color & 0xff )<< 3;
    			image_buffer[offset]     = r;
    			image_buffer[offset + 1] = g;
    			image_buffer[offset + 2] = b;
    		}
    
    		row_pointer[0] = image_buffer;
    		(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
    	}
    
    	jpeg_finish_compress(&cinfo);
    	fclose(outfile);
    
    	jpeg_destroy_compress(&cinfo);
    
    	return 0;
    }
    
    int main(int argc, char* argv[])
    {
    	struct FB fb = {0};
    	const char* filename   = NULL;
    	const char* fbfilename = NULL;
    
    	if(argc >= 3)
    	{	
    		printf("\nusage: %s [jpeg] \n", argv[0]);
    		printf("-----------------------------------------\n");
    		return 0;
    	}
    	filename    = argv[1];
    	fbfilename  = "/dev/fb0";
    	if (fb_open(&fb,fbfilename) == 0)
    	{
    		snap(filename, 100, &fb);
    		fb_close(&fb);
    	}
    
    	return 0;
    }
    

     Makefile

    all:
    	arm-linux-gcc -g gsnap.c -ljpeg -o gsnap
    clean:
    	rm -f gsnap
    

     将生成的gsnap文件加入到/bin文件中即可……

  • 相关阅读:
    WCF 发布在iis上
    记一次服务器迁移过程
    期末总结
    典型用户和用户场景
    软件工程第四次作业 结对编程 增量开发
    第三次作业 结对编程
    我对git认识
    浅谈对IT的认识!
    本地Git仓库和远程仓库的创建和关联及github上传(git push)时出现error: src refspec master does not match any解决办法
    CSS下拉菜单
  • 原文地址:https://www.cnblogs.com/Neddy/p/2279611.html
Copyright © 2011-2022 走看看