在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
使用android底层自带的截屏源码进行修改后,将截取屏幕的内容再次显示在屏幕上,使屏幕呈现出暂停的效果。 android自带的截屏代码在android\JB\frameworks\base\cmds\screencap\screencap.cpp文件中,我是参考这个进行修改的,代码如下:
1 int main(int argc, char** argv) 2 { 3 4 sp<ProcessState> proc(ProcessState::self()); 5 // create a client to surfaceflinger 6 sp<SurfaceComposerClient> client = new SurfaceComposerClient(); //创建SurfaceFlinger的本地代理 7 char str[80]; 8 DisplayInfo display; 9 //获取屏幕的宽高等信息 10 client->getDisplayInfo(client->getBuiltInDisplay(HWC_DISPLAY_PRIMARY), &display); 11 12 //创建SurfaceControl的本地代理 13 sp<SurfaceControl> surfaceControl = client->createSurface(String8("testsurface"), 14 1920/*240*/, 1080/*160*/, PIXEL_FORMAT_RGBA_8888, 0); 15 16 /*********************************************************************/ 17 SurfaceComposerClient::openGlobalTransaction(); 18 surfaceControl->setLayer(120000); //设置z轴 19 surfaceControl->setSize(display.w, display.h); 20 surfaceControl->setPosition(0, 0); //起始位置 21 SurfaceComposerClient::closeGlobalTransaction(); 22 // 获取Surface本地代理 23 sp<Surface> surface = surfaceControl->getSurface();//获取surface 24 Surface::SurfaceInfo info; 25 26 //创建SurfaceControl的本地代理 27 sp<SurfaceControl> surfaceControl1 = client->createSurface(String8("wenfu"), 28 display.w, display.h, PIXEL_FORMAT_RGBA_8888, 0); 29 SurfaceComposerClient::openGlobalTransaction(); 30 surfaceControl1->setLayer(110000); //设置z轴 31 surfaceControl1->setSize(display.w, display.h); 32 surfaceControl1->setPosition(0, 0); //起始位置 33 SurfaceComposerClient::closeGlobalTransaction(); 34 // 获取Surface本地代理 35 sp<Surface> surface1 = surfaceControl1->getSurface();//获取surface 36 Surface::SurfaceInfo info1; 37 38 ssize_t bpr ; 39 SkBitmap bitmap; 40 SkCanvas canvas ; 41 SkCanvas canvas1; 42 SkBitmap bitmap1; 43 SkPath path; 44 SkPaint paint; //paint可以指定绘图的颜色,文本的大小及对齐方式,编码格式 45 46 int sx,sy; 47 int w,h; 48 int flg = 1; 49 int flg2 = 0; 50 int lastx = 0, lasty = 0, ctrx = 0, ctry = 0; 51 char st[30]; 52 ///////////////////////////////////////////////////////////////////////////////////////// 53 void const* mapbase = MAP_FAILED; 54 ssize_t mapsize = -1; 55 void const* base = 0; 56 size_t size = 0; 57 int ww,hh; 58 ScreenshotClient screenshot; //下面是截屏操作 59 sp<IBinder> display1 = SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain); 60 if (display1 != NULL && screenshot.update(display1) == NO_ERROR) 61 { 62 base = screenshot.getPixels(); 63 ww = screenshot.getWidth(); 64 hh = screenshot.getHeight(); 65 size = screenshot.getSize(); 66 } 67 68 if (base) //以下是显示部分 69 { 70 SkBitmap bitm; 71 bitm.setConfig(SkBitmap::kARGB_8888_Config, ww, hh); 72 bitm.setPixels((void*)base); 73 surface1->lock(&info1); 74 bpr = info1.s * bytesPerPixel(info1.format); 75 bitmap1.setConfig(SkBitmap::kARGB_8888_Config, display.w, display.h, bpr); 76 bitmap1.setPixels(info1.bits); 77 canvas1.setDevice(new SkDevice(bitmap1)); 78 canvas1.drawBitmap(bitm, SkIntToScalar(0), SkIntToScalar(0)); //从0,0点开始画图 79 surface1->unlockAndPost(); 80 } 81 if (mapbase != MAP_FAILED) { 82 munmap((void *)mapbase, mapsize); 83 } 84 //////////////////////////////////////////////////////////////////////////////////////// 85 86 while(1); 87 88 return 0; 89 } 代码写的比较粗糙,实验型代码。最开始使用自带的screencap来做,比较耗时,自带的是将截取的屏幕内容保存到png文件里,让后我再读取png文件,这样截屏存储又显示速度很慢,需要5到6秒的时间,这样直接截屏显示只需要不到500ms时间。 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论