Java 调用 ADB 命令截取安卓手机屏幕到PC

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

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

 

方案1	方案2
4065	1886
3145	2069
3238	1701
3196	1566
3117	1735

 

 

 

public class ScreenCapture {
	public static String[] getDevices() {
		String command = "adb devices";
		System.out.println(command);
		ArrayList<String> devices = new ArrayList<>();

		try {
			Process process = Runtime.getRuntime().exec(command);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

			String line = bufferedReader.readLine();
			while (line != null) {
				System.out.println(line);
				if (line.endsWith("device")) {
					String device = line.substring(0, line.length() - "device".length()).trim();
					devices.add(device);
				}

				line = bufferedReader.readLine();
			}
			process.destroy();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return devices.toArray(new String[] {});
	}

	public static String getModel(String device) {
		String command = "adb -s " + device + " shell getprop";
		System.out.println(command);
		String model = null;

		try {
			Process process = Runtime.getRuntime().exec(command);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

			String line = bufferedReader.readLine();
			while (line != null) {
				if (line.contains("[ro.product.model]:")) {
					model = line.substring(("[ro.product.model]:").length()).trim();
					model = model.substring(1, model.length() - 1);
					break;
				}
				line = bufferedReader.readLine();
			}
			process.destroy();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}

	public static void snapshot(String device, String toPath, String toFile) {
		String temp = "scrsnp.png";
		long t0 = new Date().getTime();
		String command1 = "adb -s " + device + " shell screencap -p /sdcard/" + temp;
		System.out.println(command1);
		cmdExecute(command1);
		long t1 = new Date().getTime();
		System.out.println(t1 - t0);
		String command2 = "adb -s " + device + " pull /sdcard/" + temp + " " + toPath + "/" + toFile;
		System.out.println(command2);
		cmdExecute(command2);
		long t2 = new Date().getTime();
		System.out.println("消耗时间:"+(t2 - t1));
		String command3 = "adb -s " + device + " shell rm /sdcard/" + temp;
		System.out.println(command3);
		cmdExecute(command3);
		long t3 = new Date().getTime();
		System.out.println(t3 - t2);
	}

	public static void directSnapshot(String device, String toPath, String toFile) {

		try {
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(toPath + "/" + toFile));

			String command = "adb -s " + device + " shell screencap -p";
			Runtime runtime = Runtime.getRuntime();
			Process getProcess = runtime.exec(command);
			BufferedInputStream bis = new BufferedInputStream(getProcess.getInputStream());
			byte[] buf = new byte[1024 * 1024 * 4];
			int len = bis.read(buf);
			while (len != -1) {
				bos.write(fixBytes(buf, len));
				len = bis.read(buf);
			}
			bos.close();
			getProcess.destroy();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static byte[] fixBytes(byte[] src, int len) {
		java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
		for (int i = 0; i < len; i++) {
			if (src[i] == 0x0D) {
				if(i+2<len) {					
					if(src[i+1] == 0x0D && src[i+2] == 0x0A) {
						i+=1;
						continue;
					}else {
						
					}
				}else {
					
				}
			}
			baos.write(src[i]);
		}
		return baos.toByteArray();
	}

	private static void cmdExecute(String command){
		try{
		Process process = Runtime.getRuntime().exec(command);
		process.waitFor();
		process.destroy();
		}catch(Exception e) {
		e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		String[] ds = ScreenCapture.getDevices();
		if (ds.length > 0) {
//			String model = ScreenCapture.getModel(ds[0]);
			//System.out.println(model);
			long t0 = new Date().getTime();
			ScreenCapture.snapshot(ds[0], ".", "a.png");
//			//ScreenCapture.snapshot(ds[0], "d:\\temp\\", "a.png");
			long t1 = new Date().getTime();
			ScreenCapture.directSnapshot(ds[0], ".", "b.png");
			long t2 = new Date().getTime();
			System.out.println(t1 - t0);
			System.out.println(t2 - t1);
		}
	}
}

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值