一,模擬器資訊
Android 模擬器手機的色深是16bit, 即R/G/B=5/6/5. 故需要一些特殊處理才能獲得其螢幕映像。
我們可以adb登入進入Android模擬器,先dump出/dev/fb0的內容,然後將dump出的檔案拷貝到PC(ubuntu)上用convert命令進行處理。
通過程式擷取模擬器FrameBuffer 資訊
The mem is :614400
The line_length is :640
The xres is :320
The yres is :480
bits_per_pixel is :16
二,截屏並轉換過程
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格式的.
dd命令:用指定大小的塊拷貝一個檔案,並在拷貝的同時進行指定的轉換
if=file //輸入檔案名稱,預設為標準輸入。
of=file //輸出檔案名,預設為標準輸出。
ibs=bytes //一次讀入 bytes 個位元組(即一個塊大小為 bytes 個位元組)。
obs=bytes //一次寫 bytes 個位元組(即一個塊大小為 bytes 個位元組)。
bs=bytes //同時設定讀寫塊的大小為 bytes ,可代替 ibs 和 obs 。
cbs=bytes //一次轉換 bytes 個位元組,即轉換緩衝區大小。
skip=blocks //從輸入檔案開頭跳過 blocks 個塊後再開始複製。
seek=blocks //從輸出檔案開頭跳過 blocks 個塊後再開始複製。(通常只有當輸出檔案是磁碟或磁帶時才有效)
count=blocks //僅拷貝 blocks 個塊,塊大小等於 ibs 指定的位元組數。
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; //5 shift 3 = 8 out[1] = ((in >> 5) & 0x3f) << 2; //6 shift 2 = 8 out[0] = ((in >> 11) & 0x1f) << 3; //5 shift 3 = 8 write(1, out, 3); } return 0;}
將以上代碼編譯成執行檔案/tmp/565to88後,再執行:
$ /tmp/565to888 </tmp/screenshot1.xmp >/tmp/screenshot888.xmp
這將把顏色轉化為24bit色深。
5)最後把24bit色深的映像轉換成PNG格式:
$ /usr/bin/convert -depth 8 -size 320x480 rgb:screenshot888.xmp
screenshot.png
6)現在可以用以下命令查看截屏(screenshot.png檔案)啦:
$ /usr/bin/display screenshot.png
附: HTC HD7上查看截屏:
手機側:
# cat /dev/graphics/fb0 > /mnt/sdcard/fb0
PC側:
$ dd bs=$((480*800*2)) count=2 if=/tmp/fb0 of=/tmp/screenshot1.xmp
$ /tmp/565to888 </tmp/screenshot1.xmp >/tmp/screenshot888.xmp
$ convert -depth 16 -size 480x800 /tmp/screenshot888.xmp /tmp/screenshot888.png
$ display /tmp/screenshot888.png