您的位置:首页 > 移动开发 > Android开发

从Android设备获取实时截屏(adb)

2015-10-06 23:53 399 查看
前两篇文章已经把如何控制android设备的输入讲了,这一篇就是如何获取输出,通过adb的方式

原理



AdbClient和AdbServer都是运行在PC上的,AdbDaemon运行在android设备上。那framebuffer倒底是个啥?

帧缓冲(frame buffer)是Linux视频系统的核心概念,因此先了解一下他的功能。

因为视频适配器可能基于不同的硬件体系架构,较高内核层和应用程序的实现可能会因视频卡的不同而不同,这会导致在使用不同视频卡的时需要采用不同的方案。随之而来的低可移植性和冗余的代码需要大量的投入和维护开销。帧缓冲的概念解决了这个问题,它进行了一般化的抽象并规定编程接口,从而开发人员可以以与平台无关的方式编写应用层和较高内核层程序。因此,内核的帧缓冲接口允许应用程序与底层图形硬件的变化无关,如果应用和显示器驱动程序遵循帧缓冲接口,应用程序不用改变就可以在不同类型的视频硬件上运行。 (引用链接)

我来简单的解释一下,其实就是当前时间和之前几个帧的屏幕图像信息(每个像素点的颜色,分辨率等等)。

java实现

我们来看看android系统里面对每一帧图像信息的定义

//ddmlib\src\com\android\ddmlib\Device.java

public RawImage getScreenshot()
throws TimeoutException, AdbCommandRejectedException, IOException {
return AdbHelper.getFrameBuffer(AndroidDebugBridge.getSocketAddress(), this);
}


//ddmlib\src\com\android\ddmlib\AdbHelper.java

/**
* Retrieve the frame buffer from the device.
* @throws TimeoutException in case of timeout on the connection.
* @throws AdbCommandRejectedException if adb rejects the command
* @throws IOException in case of I/O error on the connection.
*/
static RawImage getFrameBuffer(InetSocketAddress adbSockAddr, Device device)
throws TimeoutException, AdbCommandRejectedException, IOException {

RawImage imageParams = new RawImage();
byte[] request = formAdbRequest("framebuffer:"); //$NON-NLS-1$
byte[] nudge = {
0
};
byte[] reply;

SocketChannel adbChan = null;
try {
adbChan = SocketChannel.open(adbSockAddr);
adbChan.configureBlocking(false);

// if the device is not -1, then we first tell adb we're looking to talk
// to a specific device
setDevice(adbChan, device);

write(adbChan, request);

AdbResponse resp = readAdbResponse(adbChan, false /* readDiagString */);
if (resp.okay == false) {
throw new AdbCommandRejectedException(resp.message);
}

// first the protocol version.
reply = new byte[4];
read(adbChan, reply);

ByteBuffer buf = ByteBuffer.wrap(reply);
buf.order(ByteOrder.LITTLE_ENDIAN);

int version = buf.getInt();

// get the header size (this is a count of int)
int headerSize = RawImage.getHeaderSize(version);

// read the header
reply = new byte[headerSize * 4];
read(adbChan, reply);

buf = ByteBuffer.wrap(reply);
buf.order(ByteOrder.LITTLE_ENDIAN);

// fill the RawImage with the header
if (imageParams.readHeader(version, buf) == false) {
Log.e("Screenshot", "Unsupported protocol: " + version);
return null;
}

Log.d("ddms", "image params: bpp=" + imageParams.bpp + ", size="
+ imageParams.size + ", width=" + imageParams.width
+ ", height=" + imageParams.height);

write(adbChan, nudge);

reply = new byte[imageParams.size];
read(adbChan, reply);

imageParams.data = reply;
} finally {
if (adbChan != null) {
adbChan.close();
}
}

return imageParams;
}


上面两段源码取自于ddmlib库,展示了AdbClient需要如何去和AdbServer通信去获取framebuffer当前帧,PS:当前adb自带的一些工具都还没有实现该功能,如果我们想实现该功能,很简单,建立一个java程序,引用ddmlib和一些其他的库,去调用即可(getScreenshot()),怎么调用?请看《Android Adb调试功能漫谈》

buf.order(ByteOrder.LITTLE_ENDIAN);


注意上面这一句,涉及到一个大字节序和小字节序的问题,详情请见大字节序与小字节序详解

由于现在大部分民用cpu都是小字节序,所以我在adb.exe翻译成c代码时,这一段没有实现。也希望有兴趣的朋友把大小字节序转换的函数实现,网上也有部分代码参考哦。

adb.exe 实现

这里是一个代码翻译过程,不多啰嗦,看代码

//outScreen由函数里面申请,外面用完了必须要释放内存,因为协议里面带有长度
int getScreen(char* inSerial, unsigned char** outScreen, int* outWidth, int* outHeight)
{
int fd = 0;
int rel = 0;
int dbgRel = 0;
char buf[10240];
int version = -1;
int headerSize = 0;
unsigned char* retScreen;
int* cousor;
unsigned char* cCousor;
unsigned char nudge[] = {0};

//图片的信息
int bpp;
int size;
int width;
int height;
int red_offset;
int red_length;
int blue_offset;
int blue_length;
int green_offset;
int green_length;
int alpha_offset;
int alpha_length;

transport_type ttype = kTransportAny;
int server_port = DEFAULT_ADB_PORT;

adb_set_transport(ttype, inSerial);
adb_set_tcp_specifics(server_port);

fd = adb_connect("framebuffer:");   //+
if(fd < 0) {
return 0;
//read_finished(fd);
//adb_close(fd);
}

//开始接收数据
if (readx(fd,buf,4) == 0){
version = *(int*)(buf);
//printf("version:%d\n",version);
}else{
fprintf(stderr,"err recv size");
return 0;
}

switch (version)
{
case 1:
headerSize = 12;
break;
case 16:
headerSize = 3;
break;
default:
break;
}

if (headerSize == 0)
{
adb_close(fd);
return 0;
}

retScreen = (unsigned char*)malloc(headerSize*4);

if (readx(fd,retScreen,headerSize*4) != 0)
{
fprintf(stderr,"err recv size");
adb_close(fd);
return 0;
}

cousor = (int*)retScreen;
if (version == 1)
{
bpp = *cousor;cousor++;
size = *cousor;cousor++;
width = *cousor;cousor++;
height = *cousor;cousor++;
red_offset = *cousor;cousor++;
red_length = *cousor;cousor++;
blue_offset = *cousor;cousor++;
blue_length = *cousor;cousor++;
green_offset = *cousor;cousor++;
green_length = *cousor;cousor++;
alpha_offset = *cousor;cousor++;
alpha_length = *cousor;

}
else if(version == 16)
{
bpp = 16;

// read actual values.
size = *cousor;cousor++;
width = *cousor;cousor++;
height = *cousor;cousor++;

// create default values for the rest. Format is 565
red_offset = 11;
red_length = 5;
green_offset = 5;
green_length = 6;
blue_offset = 0;
blue_length = 5;
alpha_offset = 0;
alpha_length = 0;
}
else
{
fprintf(stderr,"unsupport version");
}
//printf("bpp:%d width:%d high:%d size:%d\n",bpp,width,height,size);

adb_write(fd,nudge,sizeof nudge);

retScreen = (unsigned char*)realloc(retScreen,size);

if (readx(fd,retScreen,size) != 0)
{
fprintf(stderr,"err recv size should be:%d\n",size);
adb_close(fd);
return 0;
}
* outScreen = retScreen;
* outWidth = width;
* outHeight = height;
//write png
//write_PNG(retScreen,"d:\\a.png",width,height);
//free(retScreen);
adb_close(fd);
return 1;
}


使用该函数就可以把当前帧的framebuffer取过来,进行处理。C代码?怎么用?请见《让Adb.exe支持Monkey》

framebuffer格式

那把framebuffer取下来之后怎么处理呢?这是一个问题。

对字节流进行处理

typedef struct RGBA
{
RGBA()
{
R = 0;
G = 0;
B = 0;
A = 0;
}
RGBA(BYTE pR, BYTE pG, BYTE pB, BYTE pA)
{
R = pR;
G = pG;
B = pB;
A = pA;
}

BYTE R;
BYTE G;
BYTE B;
BYTE A;
}*PRGBA;


这是一个像素点的RGBA信息,R是Alpha通道,与透明度相关,GBA就不解释了。一个像素是占用4个字节,按顺序是R,G,B,A信息。整个字节流是一个二维数组,可以根据framebuffer里面的元数组取得长和宽,这样就可以精确的读取到每一个像素点的信息。

用opencv进行处理

//传入 framebuffer, frameWidth, frameHeight
...
try
{
Size size(frameWidth, frameHeight);
//这里创建不会复制数据,只会创建一个头部
Mat ref(size, CV_8UC4, framebuffer);
cvtColor(ref, ref, CV_BGRA2RGB);

ret = ...(function)(Mat ref)

}
catch (Exception* e)
{
fprintf(stderr, "Function execute err:%s\r\n", e->what());
ret = false;
}
if (freeFramebuffer)
free(framebuffer);
...


现在的图形处理如果还不用opencv可就有点out了哦 :)

后记

经过实测,framebuffer是无压缩的,有些分辨率高的设备取到的framebuffer非常大,从设备传送到AdbClient耗时会比较大,5秒左右的也有,如果追求极限是可以在android侧制作一个程序先获取framebuffer再压缩,再传送到AdbClient,就会快很多,有兴趣的朋友可以交流。

framebuffer获取的原理和格式都已经清楚了,其实已经可以做非常多的自由发挥了。下一篇是啥?怎么用opencv做一些处理?或者用tesseract做一些有意思的东东?

目录

原理

java实现

adbexe 实现

framebuffer格式
对字节流进行处理

用opencv进行处理

后记

目录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: