当前位置:   article > 正文

Java 调用 ADB 命令截取安卓手机屏幕到PC_java查看安卓系统画面

java查看安卓系统画面

原文引用:http://blog.sina.com.cn/s/blog_66e177dd0102w41i.html。向作者致敬。

原作者方案2中的fixBytes方法丢失了一些代码,通过网络的搜索和一些尝试,补全了其中的代码,可以正常运行。方案二在调用adb命令进行获取图片的效率上提高了很多,在1920*1080的手机上。时间节省了很多,详见下图,单位ms。

 

  1. 方案1 方案2
  2. 4065 1886
  3. 3145 2069
  4. 3238 1701
  5. 3196 1566
  6. 3117 1735

 

 

 

  1. public class ScreenCapture {
  2. public static String[] getDevices() {
  3. String command = "adb devices";
  4. System.out.println(command);
  5. ArrayList<String> devices = new ArrayList<>();
  6. try {
  7. Process process = Runtime.getRuntime().exec(command);
  8. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  9. String line = bufferedReader.readLine();
  10. while (line != null) {
  11. System.out.println(line);
  12. if (line.endsWith("device")) {
  13. String device = line.substring(0, line.length() - "device".length()).trim();
  14. devices.add(device);
  15. }
  16. line = bufferedReader.readLine();
  17. }
  18. process.destroy();
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. return devices.toArray(new String[] {});
  23. }
  24. public static String getModel(String device) {
  25. String command = "adb -s " + device + " shell getprop";
  26. System.out.println(command);
  27. String model = null;
  28. try {
  29. Process process = Runtime.getRuntime().exec(command);
  30. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  31. String line = bufferedReader.readLine();
  32. while (line != null) {
  33. if (line.contains("[ro.product.model]:")) {
  34. model = line.substring(("[ro.product.model]:").length()).trim();
  35. model = model.substring(1, model.length() - 1);
  36. break;
  37. }
  38. line = bufferedReader.readLine();
  39. }
  40. process.destroy();
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. return model;
  45. }
  46. public static void snapshot(String device, String toPath, String toFile) {
  47. String temp = "scrsnp.png";
  48. long t0 = new Date().getTime();
  49. String command1 = "adb -s " + device + " shell screencap -p /sdcard/" + temp;
  50. System.out.println(command1);
  51. cmdExecute(command1);
  52. long t1 = new Date().getTime();
  53. System.out.println(t1 - t0);
  54. String command2 = "adb -s " + device + " pull /sdcard/" + temp + " " + toPath + "/" + toFile;
  55. System.out.println(command2);
  56. cmdExecute(command2);
  57. long t2 = new Date().getTime();
  58. System.out.println("消耗时间:"+(t2 - t1));
  59. String command3 = "adb -s " + device + " shell rm /sdcard/" + temp;
  60. System.out.println(command3);
  61. cmdExecute(command3);
  62. long t3 = new Date().getTime();
  63. System.out.println(t3 - t2);
  64. }
  65. public static void directSnapshot(String device, String toPath, String toFile) {
  66. try {
  67. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(toPath + "/" + toFile));
  68. String command = "adb -s " + device + " shell screencap -p";
  69. Runtime runtime = Runtime.getRuntime();
  70. Process getProcess = runtime.exec(command);
  71. BufferedInputStream bis = new BufferedInputStream(getProcess.getInputStream());
  72. byte[] buf = new byte[1024 * 1024 * 4];
  73. int len = bis.read(buf);
  74. while (len != -1) {
  75. bos.write(fixBytes(buf, len));
  76. len = bis.read(buf);
  77. }
  78. bos.close();
  79. getProcess.destroy();
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. static byte[] fixBytes(byte[] src, int len) {
  85. java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
  86. for (int i = 0; i < len; i++) {
  87. if (src[i] == 0x0D) {
  88. if(i+2<len) {
  89. if(src[i+1] == 0x0D && src[i+2] == 0x0A) {
  90. i+=1;
  91. continue;
  92. }else {
  93. }
  94. }else {
  95. }
  96. }
  97. baos.write(src[i]);
  98. }
  99. return baos.toByteArray();
  100. }
  101. private static void cmdExecute(String command){
  102. try{
  103. Process process = Runtime.getRuntime().exec(command);
  104. process.waitFor();
  105. process.destroy();
  106. }catch(Exception e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. public static void main(String[] args) throws Exception {
  111. String[] ds = ScreenCapture.getDevices();
  112. if (ds.length > 0) {
  113. // String model = ScreenCapture.getModel(ds[0]);
  114. //System.out.println(model);
  115. long t0 = new Date().getTime();
  116. ScreenCapture.snapshot(ds[0], ".", "a.png");
  117. // //ScreenCapture.snapshot(ds[0], "d:\\temp\\", "a.png");
  118. long t1 = new Date().getTime();
  119. ScreenCapture.directSnapshot(ds[0], ".", "b.png");
  120. long t2 = new Date().getTime();
  121. System.out.println(t1 - t0);
  122. System.out.println(t2 - t1);
  123. }
  124. }
  125. }

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/335622
推荐阅读
相关标签
  

闽ICP备14008679号