Android螢幕截圖實現 (adbd部分)

來源:互聯網
上載者:User
Android螢幕實現(手記)

/android2.1/system/core/adb/framebuffer_service.c  這個裡面是DDMS 裡面通訊adbd進程的代碼   DDMS那邊接到的資料是用JAVA處理的  !   framebuffer_service.c代碼如下:

 

 

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the “License”);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an “AS IS” BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#include “fdevent.h”
#include “adb.h”

#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

/* TODO:
** – sync with vsync to avoid tearing
*/
/* This version number defines the format of the fbinfo struct.
   It must match versioning in ddms where this data is consumed. */
#define DDMS_RAWIMAGE_VERSION 1
struct fbinfo {
    unsigned int version;
    unsigned int bpp;
    unsigned int size;
    unsigned int width;
    unsigned int height;
    unsigned int red_offset;
    unsigned int red_length;
    unsigned int blue_offset;
    unsigned int blue_length;
    unsigned int green_offset;
    unsigned int green_length;
    unsigned int alpha_offset;
    unsigned int alpha_length;
} __attribute__((packed));

void framebuffer_service(int fd, void *cookie)
{
    struct fb_var_screeninfo vinfo;
    int fb, offset;
    char x[256];

    struct fbinfo fbinfo;
    unsigned i, bytespp;

    fb = open(“/dev/graphics/fb0″, O_RDONLY);
    if(fb < 0) goto done;

    if(ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) < 0) goto done;
    fcntl(fb, F_SETFD, FD_CLOEXEC);

    bytespp = vinfo.bits_per_pixel / 8;

    fbinfo.version = DDMS_RAWIMAGE_VERSION;
    fbinfo.bpp = vinfo.bits_per_pixel;
    fbinfo.size = vinfo.xres * vinfo.yres * bytespp;
    fbinfo.width = vinfo.xres;
    fbinfo.height = vinfo.yres;
    fbinfo.red_offset = vinfo.red.offset;
    fbinfo.red_length = vinfo.red.length;
    fbinfo.green_offset = vinfo.green.offset;
    fbinfo.green_length = vinfo.green.length;
    fbinfo.blue_offset = vinfo.blue.offset;
    fbinfo.blue_length = vinfo.blue.length;
    fbinfo.alpha_offset = vinfo.transp.offset;
    fbinfo.alpha_length = vinfo.transp.length;

    /* HACK: for several of our 3d cores a specific alignment
     * is required so the start of the fb may not be an integer number of lines
     * from the base.  As a result we are storing the additional offset in
     * xoffset. This is not the correct usage for xoffset, it should be added
     * to each line, not just once at the beginning */
    offset = vinfo.xoffset * bytespp;

    offset += vinfo.xres * vinfo.yoffset * bytespp;

    if(writex(fd, &fbinfo, sizeof(fbinfo))) goto done;

    lseek(fb, offset, SEEK_SET);
    for(i = 0; i < fbinfo.size; i += 256) {
      if(readx(fb, &x, 256)) goto done;
      if(writex(fd, &x, 256)) goto done;
    }

    if(readx(fb, &x, fbinfo.size % 256)) goto done;
    if(writex(fd, &x, fbinfo.size % 256)) goto done;

done:
    if(fb >= 0) close(fb);
    close(fd);
}

       

用framebuffer截屏,就是把當前framebuffer的記憶體dump成bitmap或者壓縮成其他圖片格式.

 

 

在ubuntu下使用命令截屏:

 

Android G1手機的色深是16bit, 即R/G/B=5/6/6. 故需要一些特殊處理才能獲得其螢幕映像。我們可以adb登入進入Android手機,先dump出/dev/fb0的內容,然後將dump出的檔案拷貝到PC(ubuntu)上用convert命令進行處理。 1. 在Android手機上擷取framebuffer內容: # cat /dev/graphics/fb0 > /mnt/sdcard/fb0 2. 把檔案拷貝到PC上的/tmp目錄: $ adb pull /mnt/sdcard/fb0
/tmp/fb0 3. 從/tmp/fb0檔案中取出第一屏的內容(一般會有2到3屏的內容): $ dd bs=307200 count=1 if=/tmp/fb0 of=/tmp/screenshot1.xmp 這裡,手機螢幕解析度是320*480; 色彩深度為16bit (R/G/B=5/6/5),即2位元組; 故有 320*480*2=307200 或在shell下執行: $ echo $((320*480*2)) 這裡得到的映像是pixmap格式的. 4. 這時取到的截屏資料要轉成每個顏色值為8bit的raw映像資料:
原理如下: int main(int argc, char *argv[]) { unsigned short in; // 16bit unsigned char out[3]; // 8bit*3 while (read(0, &in, 2) == 2) { out[2] = (in & 0x1f) << 3; out[1] = ((in >> 5) & 0x3f) << 2; out[0] = ((in >> 11) & 0x1f) << 3; write(1, out, 3); } return 0;
} 將以上代碼編譯成執行檔案/tmp/565to88後,再執行: $ /tmp/565to888 /tmp/screenshot888.xmp 這將把顏色轉化為24bit色深。 5.最後把24bit色深的映像轉換成PNG格式: $ /usr/bin/convert -depth 8 -size 320×480 rgb:screenshot888.xmp screenshot.png 現在可以用以下命令查看截屏(screenshot.png檔案)啦: $ /usr/bin/display screenshot.png

轉自:http://www.howandroid.com/2011/07/02/960.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.