android Pc端截屏方法

昨晚意外的发现在PC端进行截屏的方法相当多,在android sdk的tools里面有大量的jar包可以利用。 
第一种方法: 
这里使用AndroidDebugBridge及其相关类进行截屏,使用的jar包是ddmlib.jar,在android sdk的tools文件夹内。 
代码如下(以下代码抽取自互联网并经修改过):
Java代码   收藏代码
  1. /* 
  2.  * @(#)ScreenShot.java         Project:lianmeng 
  3.  * Date-Time:2013-10-11 下午1:08:36 
  4.  * 
  5.  * Copyright (c) 2013 CFuture09, Institute of Software,  
  6.  * Guangdong Ocean University, Zhanjiang, GuangDong, China. 
  7.  * All rights reserved. 
  8.  * 
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  10.  *  you may not use this file except in compliance with the License. 
  11.  * You may obtain a copy of the License at 
  12.  * 
  13.  *     http://www.apache.org/licenses/LICENSE-2.0 
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing, software 
  16.  * distributed under the License is distributed on an "AS IS" BASIS, 
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  18.  * See the License for the specific language governing permissions and 
  19.  * limitations under the License. 
  20.  */  
  21. package pw.msdx.lianmengassistant;  
  22.   
  23. import java.awt.image.BufferedImage;  
  24. import java.io.File;  
  25. import java.io.IOException;  
  26.   
  27. import javax.imageio.ImageIO;  
  28.   
  29. import com.android.ddmlib.AndroidDebugBridge;  
  30. import com.android.ddmlib.IDevice;  
  31. import com.android.ddmlib.RawImage;  
  32.   
  33. /** 
  34.  * copy from http://bbs.csdn.net/topics/390502035. modify by Geek_Soledad 
  35.  */  
  36. public class AdbUtil {  
  37.     public static IDevice connect() {  
  38.         // init the lib  
  39.         // [try to] ensure ADB is running  
  40.         String adbLocation = System.getProperty("com.android.screenshot.bindir"); //$NON-NLS-1$  
  41.         if (adbLocation != null && adbLocation.length() != 0) {  
  42.             adbLocation += File.separator + "adb"//$NON-NLS-1$  
  43.         } else {  
  44.             adbLocation = "adb"//$NON-NLS-1$  
  45.         }  
  46.   
  47.         AndroidDebugBridge.init(false /* debugger support */);  
  48.   
  49.         AndroidDebugBridge bridge = AndroidDebugBridge  
  50.                 .createBridge(adbLocation, true /* forceNewBridge */);  
  51.   
  52.         // we can't just ask for the device list right away, as the internal  
  53.         // thread getting  
  54.         // them from ADB may not be done getting the first list.  
  55.         // Since we don't really want getDevices() to be blocking, we wait  
  56.         // here manually.  
  57.         int count = 0;  
  58.         while (bridge.hasInitialDeviceList() == false) {  
  59.             try {  
  60.                 Thread.sleep(100);  
  61.                 count++;  
  62.             } catch (InterruptedException e) {  
  63.                 // pass  
  64.             }  
  65.   
  66.             // let's not wait > 10 sec.  
  67.             if (count > 100) {  
  68.                 System.err.println("Timeout getting device list!");  
  69.                 return null;  
  70.             }  
  71.         }  
  72.   
  73.         // now get the devices  
  74.         IDevice[] devices = bridge.getDevices();  
  75.   
  76.         if (devices.length == 0) {  
  77.             System.out.println("No devices found!");  
  78.             return null;  
  79.         }  
  80.   
  81.         return devices[0];  
  82.     }  
  83.   
  84.     public static BufferedImage screenShot(IDevice device) {  
  85.         RawImage rawImage;  
  86.         try {  
  87.             rawImage = device.getScreenshot();  
  88.         } catch (Exception ioe) {  
  89.             System.out.println("Unable to get frame buffer: " + ioe.getMessage());  
  90.             return null;  
  91.         }  
  92.   
  93.         // device/adb not available?  
  94.         if (rawImage == null)  
  95.             return null;  
  96.   
  97.         // convert raw data to an Image  
  98.         BufferedImage image = new BufferedImage(rawImage.width, rawImage.height,  
  99.                 BufferedImage.TYPE_INT_ARGB);  
  100.   
  101.         int index = 0;  
  102.         int IndexInc = rawImage.bpp >> 3;  
  103.         for (int y = 0; y < rawImage.height; y++) {  
  104.             for (int x = 0; x < rawImage.width; x++) {  
  105.                 int value = rawImage.getARGB(index);  
  106.                 index += IndexInc;  
  107.                 image.setRGB(x, y, value);  
  108.             }  
  109.         }  
  110.         return image;  
  111.     }  
  112.   
  113.     /** 
  114.      * Grab an image from an ADB-connected device. 
  115.      */  
  116.     public static boolean screenShotAndSave(IDevice device, String filepath) throws IOException {  
  117.         boolean result = ImageIO.write(screenShot(device), "png"new File(filepath));  
  118.         if (result) {  
  119.             System.out.println("file is saved in:" + filepath);  
  120.         }  
  121.         return result;  
  122.     }  
  123.   
  124.     public static void terminate() {  
  125.         AndroidDebugBridge.terminate();  
  126.     }  
  127. }  

第二种方法: 
使用monkeyrunner.jar包,当然还要添加相关jar包。由于新版的API改变,不知道其中参数应该怎么传,未深入去做。 

第三种方法,使用chimpchat.jar及相关jar包(common.jar,guava-13.0.1.jar),代码大致如下: 
Java代码   收藏代码
  1. AdbBackend adb = new AdbBackend();  
  2. IChimpDevice device = adb.waitForConnection();  
  3. ImageIO.write(device.takeSnapshot().createBufferedImage(), "png"new File("E:\\tmp.png"));  
  4. device.dispose();  
  5. if (adb != null) {  
  6.     adb.shutdown();  
  7. }  

但是执行之后,程序没有停止下来,不知如何才能使其终止。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值