1. 程式人生 > >嵌入式Linux截圖工具gsnap移植與分析

嵌入式Linux截圖工具gsnap移植與分析

  由於 Linux 系統的 FrameBuffer 機制,把螢幕上的每個點對映成一段線性記憶體空間,這樣,程式就可以通過改變這段記憶體的值來改變螢幕上某一點的顏色。如果我們想把當前的顯示內容儲存起來,可能會想到如下命令:

# cat /dev/fb0 > fb_data.raw

  反過來,可以將這些資料回顯到 framebuffer 中:

# cat fb_data.raw > /dev/fb0

  使用 clear 命令清除,可以恢復正常。
  但是,用這用方法儲存起來的資料是原始資料,只有專用軟體才能開啟,並且大小固定(如:8MB)。基於這些原因,我們找到一個不錯的工具——gsnap,這個工具可以將 framebuffer 的資料儲存為圖片(png或jpeg格式)。下面我們介紹一下移植過程。
  這裡的移植很簡單,因為原始檔只有 gsnap.c,因此我們只需用相應平臺的編譯工具鏈進行編譯連結即可。命令如下:

# $(CC) gsnap.c -ljpeg -lpng -o gsnap

  顯然,gsnap 需要用到 libjpeg 和 libpng 兩個庫。那麼編譯成功與否就跟這兩個庫有關了。如果你的目標平臺還沒有這些依賴庫,那麼就有必要下載相關原始碼進行編譯安裝,步驟遵循 configure、make、make install 三部曲。
  由於我的目標平臺已經包含 libjpeg 和 libpng,於是我嘗試用上述命令進行編譯,提示缺少標頭檔案,所以編譯不成功。然後我將 libjpeg 和 libpng 原始碼包中的標頭檔案抽取出來,新增到 /usr/include。發現仍然缺少標頭檔案,如下:

  • libpng 方面——找不到 pnglibconf.h,經檢查發現將 scripts/pnglibconf.h.prebuilt 另存為
    pnglibconf.h,並新增到 /usr/include 即可。
  • libjpeg 方面——找不到 jconfig.h,經檢查發現將 jconfig.txt 另存為 jconfig.h,並新增到
    /usr/include 即可。

不用擔心,如果你遵循三部曲來安裝這些庫,上面的 pnglibconf.h 和 jconfig.h 都會在編譯的過程中生成。
  一旦編譯成功,我們就可以執行 gsnap 來擷取螢幕畫面了。gsnap 的使用也很簡單,格式為:

    gsnap <jpeg|png file> <framebuffer dev>

  例如:

# ./gsnap test.png /dev/fb0

  我這裡用的是 i.mx6q 的 yocto 1.5.3 系統,截圖 test.png 如下:

這裡寫圖片描述

  以下是 gsnap.c 的原始碼:

/*
 * File:    gsnap.c
 * Author:  Li XianJing <[email protected]>
 * Brief:   snap the linux mobile device screen.
 *
 * Copyright (c) 2009  Li XianJing <[email protected]>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * History:
 * ================================================================
 * 2009-08-20 Li XianJing <[email protected]> created
 * 2011-02-28 Li XianJing <[email protected]> suppport RGB888 framebuffer.
 * 2011-04-09 Li XianJing <[email protected]> merge figofuture's png output.
 *  ref: http://blog.chinaunix.net/space.php?uid=15059847&do=blog&cuid=2040565
 *
 */

#include <png.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <jpeglib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/fb.h>
#include <linux/kd.h>

struct _FBInfo;
typedef struct _FBInfo FBInfo;
typedef int (*UnpackPixel)(FBInfo* fb, unsigned char* pixel, 
    unsigned char* r, unsigned char* g, unsigned char* b);

struct _FBInfo
{
    int fd;
    UnpackPixel unpack;
    unsigned char *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_bpp(fb)    ((fb)->vi.bits_per_pixel>>3)
#define fb_size(fb)   ((fb)->vi.xres * (fb)->vi.yres * fb_bpp(fb))

static int fb_unpack_rgb565(FBInfo* fb, unsigned char* pixel, 
    unsigned char* r, unsigned char* g, unsigned char* b)
{
    unsigned short color = *(unsigned short*)pixel;

    *r = ((color >> 11) & 0xff) << 3;
    *g = ((color >> 5) & 0xff)  << 2;
    *b = (color & 0xff )<< 3;

    return 0;
}

static int fb_unpack_rgb24(FBInfo* fb, unsigned char* pixel, 
    unsigned char* r, unsigned char* g, unsigned char* b)
{
    *r = pixel[fb->vi.red.offset>>3];
    *g = pixel[fb->vi.green.offset>>3];
    *b = pixel[fb->vi.blue.offset>>3];

    return 0;
}

static int fb_unpack_argb32(FBInfo* fb, unsigned char* pixel, 
    unsigned char* r, unsigned char* g, unsigned char* b)
{
    *r = pixel[fb->vi.red.offset>>3];
    *g = pixel[fb->vi.green.offset>>3];
    *b = pixel[fb->vi.blue.offset>>3];

    return 0;
}

static int fb_unpack_none(FBInfo* fb, unsigned char* pixel, 
    unsigned char* r, unsigned char* g, unsigned char* b)
{
    *r = *g = *b = 0;

    return 0;
}

static void set_pixel_unpacker(FBInfo* fb)
{
    if(fb_bpp(fb) == 2)
    {
        fb->unpack = fb_unpack_rgb565;
    }
    else if(fb_bpp(fb) == 3)
    {
        fb->unpack = fb_unpack_rgb24;
    }
    else if(fb_bpp(fb) == 4)
    {
        fb->unpack = fb_unpack_argb32;
    }
    else
    {
        fb->unpack = fb_unpack_none;
        printf("%s: not supported format.\n", __func__);
    }

    return;
}

static int fb_open(FBInfo* 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;

    printf("---------------framebuffer---------------\n");
    printf("%s: \n  width : %8d\n  height: %8d\n  bpp   : %8d\n  r(%2d, %2d)\n  g(%2d, %2d)\n  b(%2d, %2d)\n",
        fbfilename, fb_width(fb), fb_height(fb), fb_bpp(fb), 
        fb->vi.red.offset, fb->vi.red.length,
        fb->vi.green.offset, fb->vi.green.length,
        fb->vi.blue.offset, fb->vi.blue.length);
    printf("-----------------------------------------\n");

    set_pixel_unpacker(fb);

    return 0;

fail:
    printf("%s is not a framebuffer.\n", fbfilename);
    close(fb->fd);

    return -1;
}

static void fb_close(FBInfo* fb)
{
    munmap(fb->bits, fb_size(fb));
    close(fb->fd);

    return;
}

static int snap2jpg(const char * filename, int quality, FBInfo* fb)
{
    int row_stride = 0; 
    FILE * outfile = NULL;
    JSAMPROW row_pointer[1] = {0};
    struct jpeg_error_mgr jerr;
    struct jpeg_compress_struct cinfo;

    memset(&jerr, 0x00, sizeof(jerr));
    memset(&cinfo, 0x00, sizeof(cinfo));

    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;
        int offset = 0;
        unsigned char* line = fb->bits + cinfo.next_scanline * fb_width(fb) * fb_bpp(fb);

        for(i = 0; i < fb_width(fb); i++, offset += 3, line += fb_bpp(fb))
        {
            fb->unpack(fb, line, image_buffer+offset, image_buffer + offset + 1, image_buffer + offset + 2);
        }

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

//Ref: http://blog.chinaunix.net/space.php?uid=15059847&do=blog&cuid=2040565
static int snap2png(const char * filename, int quality, FBInfo* fb)
{
    FILE *outfile;
    if ((outfile = fopen(filename, "wb+")) == NULL)
    {
        fprintf(stderr, "can't open %s\n", filename);
        return -1;
    }

    /* prepare the standard PNG structures */
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0);

    png_infop info_ptr = png_create_info_struct(png_ptr);

    /* setjmp() must be called in every function that calls a PNG-reading libpng function */
    if (setjmp(png_jmpbuf(png_ptr)))
    {
        png_destroy_write_struct(&png_ptr, &info_ptr);
        fclose(outfile);
        return -1;
    }

    /* initialize the png structure */
    png_init_io(png_ptr, outfile);

    //
    int width = 0;
    int height = 0;
    int bit_depth = 8;
    int color_type = PNG_COLOR_TYPE_RGB;
    int interlace = 0;
    width = fb_width(fb);
    height = fb_height(fb);

    png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
                    (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
                    PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

    /* write the file header information */
    png_write_info(png_ptr, info_ptr);

    png_bytep row_pointers[height];
    png_byte* image_buffer = malloc(3 * width);

    int i = 0;
    int j = 0;
    unsigned char* line = NULL;
    for( ; i < height; i++ )
    {
        line = (char*)fb->bits + i * width * fb_bpp(fb);
        for(j = 0; j < width; j++, line += fb_bpp(fb))
        {
            int offset = j * 3;
            fb->unpack(fb, line, image_buffer+offset, image_buffer+offset+1, image_buffer+offset+2);
        }
        row_pointers[i] = image_buffer;
        png_write_rows(png_ptr, &row_pointers[i], 1);
    }

    png_destroy_write_struct(&png_ptr, &info_ptr);

    fclose(outfile);

    return 0;

}

int main(int argc, char* argv[])
{
    FBInfo fb;
    const char* filename   = NULL;
    const char* fbfilename = NULL;

    if(argc != 3)
    {
        printf("\nUsage: %s [jpeg|png file] [framebuffer dev]\n", argv[0]);
        printf("Example: %s fb.jpg /dev/fb0\n", argv[0]);
        printf("-----------------------------------------\n");
        printf("Powered by broncho(www.broncho.cn)\n\n");

        return 0;
    }

    filename   = argv[1];
    fbfilename = argv[2];

    memset(&fb, 0x00, sizeof(fb));
    if (fb_open(&fb, fbfilename) == 0)
    {
        if(strstr(filename, ".png") != NULL)
        {
            snap2png(filename, 100, &fb);
        }
        else
        {
            snap2jpg(filename, 100, &fb);
        }
        fb_close(&fb);
    }

    return 0;
}

相關推薦

嵌入式Linux工具gsnap移植分析【轉】

ram creat 嵌入式linux mbed 生活 rip 改變 因此 sig 轉自:http://blog.csdn.net/lu_embedded/article/details/53934184 版權聲明:開心源自分享,快樂源於生活 —&mdash

嵌入式Linux工具gsnap移植分析

  由於 Linux 系統的 FrameBuffer 機制,把螢幕上的每個點對映成一段線性記憶體空間,這樣,程式就可以通過改變這段記憶體的值來改變螢幕上某一點的顏色。如果我們想把當前的顯示內容儲存起來,可能會想到如下命令: # cat /dev/fb0 &

centos6.8 ubuntu12.04 嵌入式工具gsnap的交叉編譯

準備 gsnap的原始碼檔案,從別人那拷貝的 gsnap.c /* * File: gsnap.c * Author: Li XianJing <[email protected]> * Brief: snap the linux mobile devic

linux 工具 shutter

pre erl ins ubun ash true shutter light highlight ubuntu 安裝shutter sudo apt install shutter libgoo-canvas-perl libgoo-canvas-perl是提供對截

scrot 一個好用的Linux工具用法

一般用法,使用 scrot 可以抓取整個桌面、某個指定的視窗、以及選擇的矩形區域。 抓取桌面:scrot desktop.png,該命令將當前的整個桌面抓取下來,並儲存為 desktop.png 檔案。可以在當前的目錄中找到此影象檔案。 抓取視窗:scrot -bs window.png,選

kali--linux--安裝工具scrot

文章 文件 term pro tex 技術 vpd 保存 nag 忙活著在kali上找個截圖工具,發現附件裏面沒有截圖工具在網上找了幾篇文章,發現截圖工具都是在附件裏面的可是我這沒有啊,我又安裝了一個截圖工具在這分享給大家 1.安裝命令 2.安裝過程 3.打印-h信息 4.

Linux下的工具

可能有一個困擾你多時的問題,當你想要獲取一張螢幕截圖向開發者反饋問題,或是在 Stack Overflow 尋求幫助時,你可能缺乏一個可靠的螢幕截圖工具去儲存和傳送截圖。在 GNOME 中有一些這種型別的程式和 shell 拓展工具。這裡介紹的是 Linux 最好的螢幕截圖工具,可以供你擷取圖片或截

cs231n-pytorch安裝+linux工具安利

一、pytorch安裝(linux) linux下,很簡單.. 點選,根據自己的情況點點點,點選項就OK。這是我的情況,顯示卡趕緊買買買,加速duangduangduang 底下很貼心的出來命令,跑就是了。 然後驗證安裝 沒出錯就OK啦,我是直接裝好的,

Linux上好用的工具——flameshot

我在windows上習慣了QQ的Ctrl+Alt+A截圖,在linux也想擁有這樣愉快截圖的體驗,flameshot無疑是一個比較好的選擇。 flameshot的功能: 繪畫 直線 箭頭 矩形框 實心矩形 圓環 標記 模糊 儲存 複製到剪下板 移動 該百

Android 應用內某些頁面禁止工具

1.禁止截圖的實現 APP的某些介面如果不希望使用者能夠截圖,可以對當前當前的Activity設定一個安全標記 實際上,Android 本身就提供了對應的 Api 的,使用起來也非常的簡單,只需要拿到 window 物件,對其新增一個 FLAG_SECURE

Linux--安裝工具Shutter

1. 新增安裝包軟體源 $ sudo add-apt-repository ppa:shutter/ppa 2. 更新源並安裝 shutter $ sudo apt-get update $ sudo apt-get install shutter 3. 搜尋 shut

Linux中的工具

win有的東東,linux可以說都能找到相同功能的工具。win下的截圖工具紅蜻蜓應該算不錯的。linux也有截圖軟體。scrot。同樣能截圖和滑鼠擷取。只要yum源沒有問題的話,直接yum install scrot就能安裝好了。在終端裡敲入scrot即可截圖了,圖片儲存在當

技能 | linux工具【shutter】的安裝和使用

由於需要邊做研究編寫記錄過程,就找了個截圖工具,看了看選擇了shutter工具。 下面是安裝過程: 1.新增源: sudo add-apt-repository ppa:shutter/ppa 2.更新Linux系統的源: sudo apt-get u

centos7 rhel7 linux怎麼安裝shutter 有哪些最好的工具

    shutter是linux下開源強大的截圖工具,支援自選區截圖,截圖後編輯圖片等,但在centos發行版上用yum是不能安裝shutter的,但是有第三方提供的 Nux Dextop的rpm軟體包   1.解決 epel-release依賴問題 ,執行命令: y

工具當前未在計算機運行

幫助 設置 dll文件 itl reg 查詢 c盤 win7系統 搜索 win7系統自帶截圖工具,重裝win7系統後打開截圖工具,顯示“截圖工具當前未在計算機上運行。請重新啟動計算機,然後重試”,也不知道怎麽回事,網上查詢關於解決重裝win7系統後無法打開截圖工具的方法,但

Linux

tex scrip this addclass () 活動 ddc ack num 方法一:快捷鍵截圖 對整個屏幕截圖: PrintScreen 對活動窗體截圖: Alt+PrintScreen 對隨意矩形截圖: Shift+PrintScree

C#軟件開發實例.私人訂制自己的屏幕工具(十)在中包括鼠標指針形狀

graphics code tar mic rect ext 技術 family fill 本實例所有文章文件夾(一)功能概覽(二)創建項目、註冊熱鍵、顯示截圖主窗體(三)托盤圖標及菜單的實現(四)基本截圖功能實現(五)針對拖拽時閃爍卡頓現象的優化(六)加入配置管理功能

Java屏幕工具 捕獲屏幕

資源 urn stat oid reat gets phi super border 原文:http://www.open-open.com/code/view/1420037709781 import java.awt.BorderLayout; import ja

Snipaste - 開發了三年的工具,但不只是

開發 cal 官網 穿透 oba 此外 完全 1.0 頻道 先不要在意標題的“三年”,讓我先介紹一下軟件的功能。 Snipaste ,顧名思義, Snip + Paste ,截圖 + 貼圖。 截圖: 貼圖,即是使圖片成為一個窗口並置頂顯示: 貼圖窗口可以旋轉、縮放

工具,更改系統默認快捷鍵,系統配置實用程序,以管理員身份運行cmd(win7)

con 單單 align size 程序 高級 外觀 ria mysq 截圖工具: 開始--附件--右鍵發送到桌面快捷方式---桌面截圖工具右鍵屬性--快捷方式 更改系統默認快捷鍵: 控制面板--外觀和個性化--調整屏幕分辨率--高級設置---英特爾圖形和媒體控制面板---