在PC端进行android截屏的多种方法
程序员文章站
2022-05-11 12:28:09
...
昨晚意外的发现在PC端进行截屏的方法相当多,在android sdk的tools里面有大量的jar包可以利用。
第一种方法:
这里使用AndroidDebugBridge及其相关类进行截屏,使用的jar包是ddmlib.jar,在android sdk的tools文件夹内。
代码如下(以下代码抽取自互联网并经修改过):
第二种方法:
使用monkeyrunner.jar包,当然还要添加相关jar包。由于新版的API改变,不知道其中参数应该怎么传,未深入去做。
第三种方法,使用chimpchat.jar及相关jar包(common.jar,guava-13.0.1.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 running String 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 devices IDevice[] 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 Image BufferedImage 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(); }
但是执行之后,程序没有停止下来,不知如何才能使其终止。
上一篇: Ansible-Playbook--剧本
下一篇: emma