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