android surfaceflinger 有包含 example code ,在 tests 這個 folder.
frameworks/base/services/surfaceflinger/tests/
其中 screencap 是作 螢幕抓圖 (screenshoot) 的範例。
int main(int argc, char** argv)
{
if (argc != 2) {
printf("usage: %s path\n", argv[0]);
exit(0);
}
const String16 name("SurfaceFlinger");
sp<ISurfaceComposer> composer;
getService(name, &composer);
sp<IMemoryHeap> heap;
uint32_t w, h;
PixelFormat f;
status_t err = composer->captureScreen(0, &heap, &w, &h, &f, 0, 0);
if (err != NO_ERROR) {
fprintf(stderr, "screen capture failed: %s\n", strerror(-err));
exit(0);
}
printf("screen capture success: w=%u, h=%u, pixels=%p\n",
w, h, heap->getBase());
printf("saving file as PNG in %s ...\n", argv[1]);
SkBitmap b;
b.setConfig(SkBitmap::kARGB_8888_Config, w, h);
b.setPixels(heap->getBase());
SkImageEncoder::EncodeFile(argv[1], b,
SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);
return 0;
}
大概就是:
1.請求 SurfaceFlinger Service 抓圖
2.交給 Skia 轉成PNG檔
沒有留言:
張貼留言