admin 管理员组

文章数量: 1086019


2024年3月11日发(作者:drx击败geng挺进决赛)

Android PC端截图源代码

我们有时候只是需要截图,没必要连DDMS一起开,所以剥离了截图的代码,

当然,并不是原生的啊,是根据原理自己写的,供大家参考

第一步,准备库包

我们既然是按照DDMS的方法截图,就需要用到这个包,它

位于android的SDK目录的toolslib下,我们需要把它加入到我们

的Eclipse工程的build path下。

第二步,建立连接,获取设备

有了ddmlib,我们就可以使用里面的 AndroidDebugBridge 类来获取已

经同步的设备的列表并建立连接

Code:

IDevice device;

AndroidDebugBridge bridge = Bridge();

waitDeviceList(bridge);

IDevice devices[] = ices();

device = devices[0];

上面的代码用到了一个waitDeviceList(bridge),主要是为了多次尝试连接,代

码如下

Code:

private static void waitDeviceList(AndroidDebugBridge bridge) {

int count = 0;

while (tialDeviceList() == false) {

 try {

 (100); // 如果没有获得设备列表,则等待

 ount++;

 } catch (InterruptedException e) {}

 if (count > 300) { // 设定时间超过300×100 ms的时候为连接超时

 ("Time out");

 break;

 }

 }

 }

这样我们就可以获得一个设备的类,IDevice,其中有一个getScreensho

t()方法获得屏幕截图,类型为RawImage

Code:

 RawImage rawScreen = eenshot();

后面的方法就和Android无关了,纯粹的转换,Rawimage转换到buffe

redimage,再保存

Code:

 if(rawScreen != null){

 BufferedImage image = null;

 int width2 = landscape ? : ;

 int height2 = landscape ? : ;

 if (image == null) {

 image = new BufferedImage(width2, height2,

 _INT_RGB);

 } else {

 if (ght() != height2 || th() != wid

th2) {

 image = new BufferedImage(width2, height2,

 _INT_RGB);

 }

 }



 int index = 0;

 int indexInc = >> 3;

 for (int y = 0; y < ; y++) {

 for (int x = 0; x < ; x++, index += indexIn

c) {

 int value = B(index);

 if (landscape)

 (y, - x - 1, value);

 else

 (x, y, value);

 }

 }

 ((RenderedImage)image,"PNG",new File("D:/

"));

 }


本文标签: 截图 设备 连接