从FrameBuffer中获取Android屏幕截图

时间:2022-08-14 15:26:27
我们知道,DDMS可以很容易的获取Android手机的屏幕截图,那么它是怎么做到的呢? 
其实,android手机上有一个叫做FrameBuffer的设备,图像信息都是通过FrameBuffer写到手机屏幕上去的。因此可以通过读取此设备中的数据来获取当前正在显示的图像。当然DDMS也是这么做到的。
FrameBuffer对应的设备文件就是/dev/graphics/fb0。因此我们可以通过下面的代码读取屏幕图像数据。其中传入的参数fd为一个文件描述符,也可以是socket描述符。这样我们就可以把从fb中读取的屏幕图像信息传递给我们自己的应用,从而获取手机屏幕信息。
void framebuffer_service(int fd) {     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;     printf("offset %d\n", offset);
    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); }