• 嵌入式linux通用截图工具



    转载时请注明出处和作者联系方式
    文章出处:http://www.limodev.cn/blog
    作者联系方式:李先静 <xianjimli at hotmail dot com>

    为了截几张图片给broncho的A1做宣传,昨天写了一个截图工具。这个工具与其它截图工具不同的是,它不基于任何具体的GUI,直接从framebuffer中截图,然后保存为jpeg图片,所以适用于任何嵌入式linux设备。

    o 打开framebuffer

    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;
    }

    o 读取数据并写入jpeg

    static int snap(const char * filename, int quality, struct FB* fb)
    {
    	int row_stride = 0; 
    	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);
     
    	if ((outfile = fopen(filename, "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;
    }

    o 主函数

    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] [framebuffer]/n", argv[0]);
    		printf("-----------------------------------------/n");
    		printf("Powered by broncho(www.broncho.cn)/n");
    		return 0;
    	}
     
    	filename   = argv[1];
    	fbfilename = argv[2];
    	if (fb_open(&fb, fbfilename) == 0)
    	{
    		snap(filename, 100, &fb);
    		fb_close(&fb);
    	}
     
    	return 0;
    }

    完整代码请到这里下载。

  • 相关阅读:
    .net core使用NLog+Elasticsearch记录日志
    .net core使用EasyNetQ做EventBus
    .net core使用Apollo做统一配置管理
    .net core使用App.Metrics+InfluxDB+Grafana进行APM监控
    .net core使用Ocelot+Identity Server统一网关验证
    .net core微服务之基于Docker+Consul+Registrator服务注册服务发现
    windows上禁止某个软件联网
    Windows修改命令行默认启动路径
    Win10对调Esc和CapsLock键
    animation模块的使用
  • 原文地址:https://www.cnblogs.com/zhangyunlin/p/6167517.html
Copyright © 2020-2023  润新知