接上一篇(截图原理)
视频地址:http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=877121&courseId=712011
四、非APK的操作,通过PC端实现截屏操作,及原理
通过adb桥接的方式,调用ddmlib.jar中的IDevice和AndroidDebugBridge类对通过adb连接的设备进行截图,截图之后保存在PC端,可在短时间内截取多张图
五、adb桥接方式实现截屏的代码分析
import java.awt.*;
import java.awt.event.*;
import java.io.File; import com.android.ddmlib.AndroidDebugBridge; public class PerformAnalysis extends Frame implements ActionListener { private String title = "性能测试工具";
private Button takepicture;
private Button getCPUmem;
private Label tip;
private static TextField picturenum; public PerformAnalysis() {
setTitle(title);
takepicture = new Button("获取屏幕截图");
takepicture.setSize(150, 100);
getCPUmem = new Button("获取内存和CPU");
getCPUmem.setSize(350, 300);
picturenum = new TextField("1");
picturenum.setSize(100, 20);
tip = new Label("输入截图个数,一秒截一次"); FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT,50,60);
setLayout(flowLayout);
add(takepicture);
add(getCPUmem);
add(tip);
add(picturenum); takepicture.addActionListener(this); addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
}); File file = new File("D://lixia//getpicture");if(!file.isDirectory()){//这里的多级目录跟Python中一样,必须得是mkdirs才行,否则根本就建不成功
file.mkdirs();
}
AndroidDebugBridge.init(false); setBounds(50, 350, 350, 350);
setVisible(true);
} @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == takepicture){
ScreenShot screenShot = new ScreenShot();
screenShot.main();
}
} public void setNumber(){
picturenum.setText("123");
} public static String getNumber(){
return picturenum.getText().toString();
} public static void main(String[] args){
PerformAnalysis p = new PerformAnalysis(); } }
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date; import javax.imageio.ImageIO; import com.android.ddmlib.AdbCommandRejectedException;
import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.RawImage;
import com.android.ddmlib.TimeoutException; /**
* @author lixia-xy
* @function 截屏类,需要用到device类,直接去进行截图操作。。。
*/
public class ScreenShot { private static int number = 1;
public ScreenShot(){
//啥也不干了
} public void main(){ ScreenShot screenShot = new ScreenShot();
number = Integer.parseInt(PerformAnalysis.getNumber());
System.out.println("截图个数" + number); IDevice device = screenShot.getDevice();
for(int i = 0; i < number; i++){
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String nowTime = df.format(date); if(device != null)
{
boolean landscape = false;
screenShot.getScreenShot(device, "D://lixia//getpicture", nowTime, false);
}
} } private void getScreenShot(IDevice device, String filePath, String fileName, boolean landscape) {
// TODO Auto-generated method stub
//调用系统的devie.getScreenShot()方法来获取到截图,并且按照我想要的路径将其存起来
RawImage rawScreen = null;
try{
try {
rawScreen = device.getScreenshot();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AdbCommandRejectedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(IOException e){
return ;
} if(rawScreen == null){
return ;
} int width = landscape? rawScreen.height:rawScreen.width;
int height = landscape? rawScreen.width:rawScreen.height;
//初始化一个缓冲图
BufferedImage bufferImage = new BufferedImage(width, height,
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++) {
int value = rawScreen.getARGB(index);
index += IndexInc;
bufferImage.setRGB(x, y, value);
}
} try {
if (!ImageIO.write(bufferImage, "png", new File(filePath+"//"+fileName+".png"))) {
throw new IOException("Failed to find png writer");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} private IDevice getDevice() {
// TODO Auto-generated method stub
IDevice device = null;
AndroidDebugBridge bridge = AndroidDebugBridge.createBridge("adb", false);
waitDevicesList(bridge);
System.out.println("等待设备连接...");
IDevice devices[] = bridge.getDevices();
if(devices.length > 0)
{
device = devices[0];
System.out.println("设备名称:" + device.getName());
}
return device;
} private void waitDevicesList(AndroidDebugBridge bridge) {
// TODO Auto-generated method stub
int count = 0;
while (bridge.hasInitialDeviceList() == false) {
try {
Thread.sleep(100);
count++;
} catch (InterruptedException e) {
// pass
} // let's not wait > 10 sec.
if (count > 100) {
System.err.println("Timeout getting device list!");
return;
}
}
}
}
金阳光测试 |
新浪微博:金阳光woody |
网站地址 |
1、百度搜:金阳光测试 2、官网:www.goldensunshine.cc |
微信公众号 |
|