1、从手机截图到sdcard
2、将图片导出到PC
3、从PC加载图片
4、开启定时器
5、操作电脑UI事件
1、
private static void takeScreenshot(String path) {long t1 = System.currentTimeMillis();String command = "adb devices"; // 替换为你需要执行的shell命令String command1 = "adb shell screencap -p /sdcard/screencap.png"; // 替换为你需要执行的shell命令String command2 = "adb pull /sdcard/screencap.png " + path; // 替换为你需要执行的shell命令String command3 = "adb shell rm /sdcard/screencap.png"; // 替换为你需要执行的shell命令String command4 = "rm -rf " + path; // 替换为你需要执行的shell命令try {
// Process process = Runtime.getRuntime().exec(command);Process process1 = Runtime.getRuntime().exec(command1);Process process2 = Runtime.getRuntime().exec(command2);Process process3 = Runtime.getRuntime().exec(command3);
// Process process4 = Runtime.getRuntime().exec(command4);// {
// BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
// String line;
// while ((line = reader.readLine()) != null) {
// System.out.println(line);
// }
// }{BufferedReader reader = new BufferedReader(new InputStreamReader(process1.getInputStream()));String line;while ((line = reader.readLine()) != null) {System.out.println(line);}}
// {
// BufferedReader reader = new BufferedReader(new InputStreamReader(process2.getInputStream()));
// String line;
// while ((line = reader.readLine()) != null) {
// System.out.println(line);
// }
// }
// {
// BufferedReader reader = new BufferedReader(new InputStreamReader(process3.getInputStream()));
// String line;
// while ((line = reader.readLine()) != null) {
// System.out.println(line);
// }
// }
// {
// BufferedReader reader = new BufferedReader(new InputStreamReader(process4.getInputStream()));
// String line;
// while ((line = reader.readLine()) != null) {
// System.out.println(line);
// }
// }long t2 = System.currentTimeMillis();System.out.println("takeScreenshot Exited with code: 时间:" + (t2 - t1));} catch (Exception e) {e.printStackTrace();}}
2、
static BufferedImage getImage(String folderPath) {long t1 = System.currentTimeMillis();File folder = new File(folderPath);File[] listOfFiles = folder.listFiles();BufferedImage bufferedImage = null;for (File file : listOfFiles) {if (file.isFile() && file.getName().toLowerCase().endsWith(".png") || file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".jpeg")) {try {bufferedImage = ImageIO.read(file);// 1080*2340System.out.println("getImage " + bufferedImage.getWidth());System.out.println("getImage " + bufferedImage.getHeight());} catch (IOException e) {e.printStackTrace();} finally {long t2 = System.currentTimeMillis();System.out.println("getImage : 时间:" + (t2 - t1));return bufferedImage;}}}return null;
}
3、
static JFrame jFrame;static JLabel jLabel;static void showFrame(BufferedImage screenFullImage) {// 1080*2340jFrame = new JFrame("Screen Capture");jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jLabel = new JLabel("");//设置标签大小,这种可以设计成自己想要大小jLabel.setBounds(0, 0, 500, 900);//将图片进行转换添加到标签当中 这个是工具类,具体参考下面给出代码setImgSize(screenFullImage, jLabel);jFrame.add(jLabel);jFrame.pack();
// frame.setSize(300,600); // 设置JFrame的尺寸jFrame.setVisible(true);}
4、
private static final int DELAY = 400; // 帧间隔,单位毫秒public static void main(String[] args) {String path = "/Users/Desktop/command/png2";takeScreenshot(path);BufferedImage image = getImage(path);showFrame(image);Timer timer = new Timer(DELAY, new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("actionPerformed");takeScreenshot(path);BufferedImage screenFullImage = getImage(path);setImgSize(screenFullImage, jLabel);}});timer.start();
}
5、效果图:
6、