昨晚意外的发现在PC端进行截屏的方法相当多,在android sdk的tools里面有大量的jar包可以利用。
第一种方法:
这里使用AndroidDebugBridge及其相关类进行截屏,使用的jar包是ddmlib.jar,在android sdk的tools文件夹内。
代码如下(以下代码抽取自互联网并经修改过):
/* * @(#)ScreenShot.java       Project:lianmeng * Date-Time:2013-10-11 下午1:08:36 * * Copyright (c) 2013 CFuture09, Institute of Software,  * Guangdong Ocean University, Zhanjiang, GuangDong, China. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); *  you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package pw.msdx.lianmengassistant;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import com.android.ddmlib.AndroidDebugBridge;import com.android.ddmlib.IDevice;import com.android.ddmlib.RawImage;/** * copy from http://bbs.csdn.net/topics/390502035. modify by Geek_Soledad */public class AdbUtil {public static IDevice connect() {// init the lib// [try to] ensure ADB is runningString adbLocation = System.getProperty("com.android.screenshot.bindir"); //$NON-NLS-1$if (adbLocation != null && adbLocation.length() != 0) {adbLocation += File.separator + "adb"; //$NON-NLS-1$} else {adbLocation = "adb"; //$NON-NLS-1$}AndroidDebugBridge.init(false /* debugger support */);AndroidDebugBridge bridge = AndroidDebugBridge.createBridge(adbLocation, true /* forceNewBridge */);// we can't just ask for the device list right away, as the internal// thread getting// them from ADB may not be done getting the first list.// Since we don't really want getDevices() to be blocking, we wait// here manually.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 null;}}// now get the devicesIDevice[] devices = bridge.getDevices();if (devices.length == 0) {System.out.println("No devices found!");return null;}return devices[0];}public static BufferedImage screenShot(IDevice device) {RawImage rawImage;try {rawImage = device.getScreenshot();} catch (Exception ioe) {System.out.println("Unable to get frame buffer: " + ioe.getMessage());return null;}// device/adb not available?if (rawImage == null)return null;// convert raw data to an ImageBufferedImage image = new BufferedImage(rawImage.width, rawImage.height,BufferedImage.TYPE_INT_ARGB);int index = 0;int IndexInc = rawImage.bpp >> 3;for (int y = 0; y < rawImage.height; y++) {for (int x = 0; x < rawImage.width; x++) {int value = rawImage.getARGB(index);index += IndexInc;image.setRGB(x, y, value);}}return image;}/** * Grab an image from an ADB-connected device. */public static boolean screenShotAndSave(IDevice device, String filepath) throws IOException {boolean result = ImageIO.write(screenShot(device), "png", new File(filepath));if (result) {System.out.println("file is saved in:" + filepath);}return result;}public static void terminate() {AndroidDebugBridge.terminate();}}

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

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

但是执行之后,程序没有停止下来,不知如何才能使其终止。

更多相关文章

  1. [置顶] android 自定义控件
  2. Android中ExpandableListView的使用(一)
  3. Android(安卓)IPC机制(二):AIDL的基本使用方法
  4. 使用GCM服务(Google Cloud Messaging)实现Android消息推送
  5. 如何在Android(安卓)Studio使用Java8 Lambda表达式
  6. 使用Android(安卓)studio开发Android(安卓)App
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. android 6.0 不弹允许usb调试 授权对话框
  2. android读取plist文件
  3. 使用Android自带的DownloadManager下载文
  4. android webview css z-index属性无效
  5. android中去掉ActionBar或TabWidget的分
  6. Android用HTTP下载报错“android.os.Stri
  7. Android Studio中Edittext监听回车事件,
  8. Android中获得屏幕的尺寸
  9. android移动开发的很好的功能的网页
  10. 设置环境变量