Android ddms截图代码实现
2015-02-06 15:47
267 查看
我们有时候只是需要截图,没必要连DDMS一起开,所以剥离了截图的代码,当然,并不是原生的啊,是根据原理自己写的,供大家参考
第一步,准备库包
我们既然是按照DDMS的方法截图,就需要用到ddmlib.jar这个包,它位于android的SDK目录的tools\lib下,我们需要把它加入到我们
的Eclipse工程的build path下。
第二步,建立连接,获取设备
有了ddmlib,我们就可以使用里面的 AndroidDebugBridge 类来获取已经同步的设备的列表并建立连接
IDevice device;
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();
waitDeviceList(bridge);
IDevice devices[] = bridge.getDevices();
device = devices[0];
上面的代码用到了一个waitDeviceList(bridge),主要是为了多次尝试连接,代码如下
private static void waitDeviceList(AndroidDebugBridge bridge) {
int count = 0;
while (bridge.hasInitialDeviceList() == false) {
try {
Thread.sleep(100); // 如果没有获得设备列表,则等待
ount++;
} catch (InterruptedException e) {}
if (count > 300) {
// 设定时间超过300×100 ms的时候为连接超时
System.err.print("Time out");
break;
}
}
}
这样我们就可以获得一个设备的类,IDevice,其中有一个getScreenshot()方法获得屏幕截图,类型为RawImage
RawImage rawScreen = device.getScreenshot(); 后面的方法就和Android无关了,纯粹的转换,Rawimage转换到bufferedimage,再保存
if(rawScreen != null){
BufferedImage image = null;
int width2 = landscape ? rawScreen.height : rawScreen.width;
int height2 = landscape ? rawScreen.width : rawScreen.height;
if (image == null) {
image = new BufferedImage(width2,height2,
BufferedImage.TYPE_INT_RGB);
} else {
if (image.getHeight() != height2 || image.getWidth() != width2) {
image = new BufferedImage(width2, height2,
BufferedImage.TYPE_INT_RGB);
}
}
int index = 0;
int indexInc = rawScreen.bpp >> 3;
for (int y = 0; y < rawScreen.height; y++) {
for (int x = 0; x < rawScreen.width; x++, index += indexInc) {
int value = rawScreen.getARGB(index);
if (landscape)
image.setRGB(y, rawScreen.width - x - 1, value);
else
image.setRGB(x, y, value);
}
}
ImageIO.write((RenderedImage)image,"PNG",new File("D:/temp.jpg"));
}
package com.android.ddms;
import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
public class aaa {
public static IDevice device;
public static void main(String[] args) {
AndroidDebugBridge.init(false); //很重要
device = getDevice(0);
System.out.println(device.getFileListingService().getRoot());
}
private static IDevice getDevice(int index) {
IDevice device = null;
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();
waitDevicesList(bridge);
IDevice devices[] = bridge.getDevices();
if(devices.length < index){
//没有检测到第index个设备
System.err.print("没有检测到第" + index + "个设备");
}else{
device = devices[index];
}
return device;
}
private static void waitDevicesList(AndroidDebugBridge bridge) {
int count = 0;
while (bridge.hasInitialDeviceList() == false) {
try {
Thread.sleep(500);
count++;
} catch (InterruptedException e) {
}
if (count > 60) {
System.err.print("等待获取设备超时");
break;
}
}
}
}
第一步,准备库包
我们既然是按照DDMS的方法截图,就需要用到ddmlib.jar这个包,它位于android的SDK目录的tools\lib下,我们需要把它加入到我们
的Eclipse工程的build path下。
第二步,建立连接,获取设备
有了ddmlib,我们就可以使用里面的 AndroidDebugBridge 类来获取已经同步的设备的列表并建立连接
IDevice device;
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();
waitDeviceList(bridge);
IDevice devices[] = bridge.getDevices();
device = devices[0];
上面的代码用到了一个waitDeviceList(bridge),主要是为了多次尝试连接,代码如下
private static void waitDeviceList(AndroidDebugBridge bridge) {
int count = 0;
while (bridge.hasInitialDeviceList() == false) {
try {
Thread.sleep(100); // 如果没有获得设备列表,则等待
ount++;
} catch (InterruptedException e) {}
if (count > 300) {
// 设定时间超过300×100 ms的时候为连接超时
System.err.print("Time out");
break;
}
}
}
这样我们就可以获得一个设备的类,IDevice,其中有一个getScreenshot()方法获得屏幕截图,类型为RawImage
RawImage rawScreen = device.getScreenshot(); 后面的方法就和Android无关了,纯粹的转换,Rawimage转换到bufferedimage,再保存
if(rawScreen != null){
BufferedImage image = null;
int width2 = landscape ? rawScreen.height : rawScreen.width;
int height2 = landscape ? rawScreen.width : rawScreen.height;
if (image == null) {
image = new BufferedImage(width2,height2,
BufferedImage.TYPE_INT_RGB);
} else {
if (image.getHeight() != height2 || image.getWidth() != width2) {
image = new BufferedImage(width2, height2,
BufferedImage.TYPE_INT_RGB);
}
}
int index = 0;
int indexInc = rawScreen.bpp >> 3;
for (int y = 0; y < rawScreen.height; y++) {
for (int x = 0; x < rawScreen.width; x++, index += indexInc) {
int value = rawScreen.getARGB(index);
if (landscape)
image.setRGB(y, rawScreen.width - x - 1, value);
else
image.setRGB(x, y, value);
}
}
ImageIO.write((RenderedImage)image,"PNG",new File("D:/temp.jpg"));
}
package com.android.ddms;
import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
public class aaa {
public static IDevice device;
public static void main(String[] args) {
AndroidDebugBridge.init(false); //很重要
device = getDevice(0);
System.out.println(device.getFileListingService().getRoot());
}
private static IDevice getDevice(int index) {
IDevice device = null;
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();
waitDevicesList(bridge);
IDevice devices[] = bridge.getDevices();
if(devices.length < index){
//没有检测到第index个设备
System.err.print("没有检测到第" + index + "个设备");
}else{
device = devices[index];
}
return device;
}
private static void waitDevicesList(AndroidDebugBridge bridge) {
int count = 0;
while (bridge.hasInitialDeviceList() == false) {
try {
Thread.sleep(500);
count++;
} catch (InterruptedException e) {
}
if (count > 60) {
System.err.print("等待获取设备超时");
break;
}
}
}
}
相关文章推荐
- (转)Android截图代码实现(DDMS使用部分)
- (转)Android截图代码实现(DDMS的ddmlib.jar)
- Android截图代码实现(DDMS使用部分)
- android应用内截图的代码实现
- android中 代码实现截图功能(静态+动态视频)
- Android杂谈--通过DDMS实现电脑与Android设备(如手机,平板)的网络连接、截图
- android应用内截图的代码实现
- android中 代码实现截图功能(静态+动态视频)
- 实现屏幕截图的代码-Android
- android应用内截图的代码实现
- Android实现截图功能(可根据该代码进行扩展功能)
- 实现屏幕截图的代码-Android
- Android截图代码实现
- android应用内截图的代码实现
- Android实现截图和分享功能的代码
- android中实现屏幕截图的代码
- Android实现截图功能(可根据该代码进行扩展功能)
- android中 代码实现截图功能(静态+动态视频)
- Android 代码实现屏幕截图功能
- 【转】Android截图代码实现