java使用adb连接模拟器截图并把截图文件直接保存到电脑上

    xiaoxiao2022-07-05  177

     直接贴代码:

    import java.io.*; /** * * 利用java使用adb工具连接模拟器截图,并将截图保存到电脑上. * 涉及二进制和16进制的转换 */ public class test2 { /** * @param buf * @return * @description 将二进制转换成16进制, 字符变成大写 */ public static String parseByte2HexStr(byte buf[]) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } // toLowerCase(); toUpperCase() 大小写 return sb.toString(); } /** * 16进制的字符串表示转成字节数组 * * @param hexString 16进制格式的字符串 * @return 转换后的字节数组 **/ public static byte[] toByteArray(String hexString) { //StringUtils.isEmpty(hexString) if (hexString.isEmpty()) throw new IllegalArgumentException("this hexString must not be empty"); hexString = hexString.toLowerCase(); final byte[] byteArray = new byte[hexString.length() / 2]; int k = 0; for (int i = 0; i < byteArray.length; i++) {//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先 byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff); byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff); byteArray[i] = (byte) (high << 4 | low); k += 2; } return byteArray; } /** * 字节数组转成16进制表示格式的字符串,字符是小写 * * @param byteArray 需要转换的字节数组 * @return 16进制表示格式的字符串 **/ public static String toHexString(byte[] byteArray) { if (byteArray == null || byteArray.length < 1) throw new IllegalArgumentException("this byteArray must not be null or empty"); final StringBuilder hexString = new StringBuilder(); for (int i = 0; i < byteArray.length; i++) { if ((byteArray[i] & 0xff) < 0x10)//0~F前面不零 hexString.append("0"); hexString.append(Integer.toHexString(0xFF & byteArray[i])); } //大小写 // hexString.toString().toLowerCase(); toUpperCase() return hexString.toString().toLowerCase(); } public static void main(String[] args) throws Exception { String cmd = "adb shell screencap -p "; FileOutputStream fileOutputStream = null; int i = 0; while (true) { var su = System.currentTimeMillis(); File files = new File("F:\\screen" + i + ".png"); if (i == 5) break; i++; try { Process process = Runtime.getRuntime().exec(cmd); byte[] bbb = process.getInputStream().readAllBytes(); /** * 二进制转16进制方法一 * **/ String str16 = toHexString(bbb); String ok16 = str16.replace("0d0d0a", "0a"); /** * 二进制转16进制方法二 * **/ // String str16 = parseByte2HexStr(byt); // String ok16 = s16.replace("0D0D0A" ,"0A"); // 或者replaceAll支持正则表达式 ok16.replaceAll("0D0D0A","0A"); //16进制转二进制 byte[] okby = toByteArray(ok16); // System.out.println(ok16); fileOutputStream = new FileOutputStream(files); //用FileOutputStream 的write方法写入字节数组 fileOutputStream.write(okby); System.out.println("时间: " + (System.currentTimeMillis() - su)); //为了节省IO流的开销,需要关闭 process.waitFor(); if (process.exitValue() != 0) { throw new RuntimeException("截图失败"); } } catch (IOException e) { } catch (InterruptedException e) { e.printStackTrace(); } finally { if (fileOutputStream != null) { fileOutputStream.close(); } } } } }

     

    最新回复(0)