我们知道,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);
}
相关文章
- 如何在谷歌地图android中获取当前位置
- Android 中的 framebuffer 和SurFace的关系(GUI更新过程)
- Android:如何在特定区域设置中获取字符串而不更改当前区域设置
- 获取Android的Java源代码并在Eclipse中关联
- 如何在Android应用程序中分析Apk文件?获取权限
- 如何从Android应用程序中的Web服务器获取数据?
- Android中获取字符串长度、宽度(所占像素宽度),手动设定字符串换行
- Android中获取字符串长度、宽度(所占像素宽度)的方法
- Android中获取字符串长度、宽度(所占像素宽度)
- Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug