简单实现通过电脑操作手机

embedded/2024/10/21 5:02:13/

通过电脑操作手机,支持单击,拖抓事件,延时有1-2秒。

具体步骤:

1、从手机截图到sdcard

2、将图片导出到PC

3、从PC加载展示图片

4、开启定时器

5、设置点击、滚动事件

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*2340screenW = bufferedImage.getWidth();screenH = bufferedImage.getHeight();System.out.println("getImage " + bufferedImage.getWidth() + " " + 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 int screenW;static int screenH;static int scale = 3;static void showFrame(BufferedImage screenFullImage) {// 1080*2340jFrame = new JFrame("Screen Capture");jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jLabel = new JLabel("");//设置标签大小,这种可以设计成自己想要大小int windW = screenW / 3;int windH = screenH / 3;scale = 3;jLabel.setBounds(0, 0, windW, windH);////86 212//28 63//将图片进行转换添加到标签当中  这个是工具类,具体参考下面给出代码setImgSize(screenFullImage, jLabel);jFrame.add(jLabel);jFrame.pack();
//        frame.setSize(300,600);     // 设置JFrame的尺寸jFrame.setVisible(true);addMouseListener(jLabel);}

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、

 /*** 添加单击事件** @param jLabel*/private static void addMouseListener(JLabel jLabel) {jLabel.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);onMouseClicked(e);}@Overridepublic void mousePressed(MouseEvent e) {super.mousePressed(e);System.out.println("mousePressed");xPress = e.getX();yPress = e.getY();}@Overridepublic void mouseReleased(MouseEvent e) {super.mouseReleased(e);isMouseDragged = false;
//                System.out.println("mouseReleased isMouseDragged==" + isMouseDragged);}@Overridepublic void mouseEntered(MouseEvent e) {super.mouseEntered(e);
//                System.out.println("mouseEntered");}@Overridepublic void mouseExited(MouseEvent e) {super.mouseExited(e);
//                System.out.println("mouseExited");}});jLabel.addMouseMotionListener(new MouseMotionListener() {@Overridepublic void mouseDragged(MouseEvent e) {System.out.println("mouseDragged addMouseMotionListener " + e.getY());isMouseDragged = true;xEndPress = e.getX();yEndPress = e.getY();onMouseDragged(e);}@Overridepublic void mouseMoved(MouseEvent e) {
//                System.out.println("mouseMoved addMouseMotionListener");}});jLabel.addMouseWheelListener(new MouseWheelListener() {@Overridepublic void mouseWheelMoved(MouseWheelEvent e) {
//                System.out.println("mouseWheelMoved addMouseWheelListener "+e.getY());}});}static int xPress;static int yPress;static int xEndPress;static int yEndPress;static boolean isMouseDragged = false;private static void onMouseDragged(MouseEvent mouseEvent) {System.out.println("onMouseDragged " + mouseEvent.getX() + " " + mouseEvent.getY());if (Math.abs(yEndPress - yPress) < 20 * scale) {return;}//adb shell input touchscreen draganddrop <x1> <y1> <x2> <y2>//adb shell input swipe 250 250 300 300String command = "adb shell input swipe " + xPress * scale + " " + yPress * scale + " " + xEndPress * scale + " " + yEndPress * scale;System.out.println("commond==" + command);Process process;try {process = Runtime.getRuntime().exec(command);BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line;while ((line = reader.readLine()) != null) {System.out.println(line);}} catch (IOException e) {e.printStackTrace();}}private static void onMouseClicked(MouseEvent mouseEvent) {if (isMouseDragged) {return;}System.out.println("onMouseClicked " + mouseEvent.getX() + " " + mouseEvent.getY());float TARGET_X = mouseEvent.getX() * scale;float TARGET_Y = mouseEvent.getY() * scale;System.out.println("onMouseClicked " + TARGET_X + " " + TARGET_Y);//adb shell input tap 250 250String command = "adb shell input tap " + TARGET_X + " " + TARGET_Y;// 使用adb shell输入命令模拟点击// adb shell input tap $TARGET_X $TARGET_YProcess process;try {process = Runtime.getRuntime().exec(command);BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line;while ((line = reader.readLine()) != null) {System.out.println(line);}} catch (IOException e) {e.printStackTrace();}}

6、


http://www.ppmy.cn/embedded/129181.html

相关文章

Kafka异常重试方案小记

背景 在最近进行的项目架构升级中&#xff0c;我们对原有的核心项目结构进行了细致的拆分。 现在&#xff0c;核心项目与非核心项目之间的通信和数据交换主要通过Kafka这一中间件来实现。 这种设计主要体现在核心项目向非核心项目发送通知&#xff0c;这些通知大致可以分为三个…

【Linux】并行与并发(含时间片)

简单来说 并发&#xff1a;多个进程轮流使用同一个CPU&#xff0c;在逻辑层面上&#xff0c;一段时间内推进完成了多个进程 并行&#xff1a;机器中有多个CPU可以使用&#xff0c;在物理层面上&#xff0c;做到同一时间会有多个进程同时在运行 举个例子&#xff1a;一群人需要…

基于SSM+微信小程序的宠物管理系统1

&#x1f449;文末查看项目功能视频演示获取源码sql脚本视频导入教程视频 1、项目介绍 基于SSM微信小程序的宠物管理系统实现了管理员、店主、用户。 管理员实现了店主管理、附件宠物店、管理员、用户管理、猫狗查询、猫狗宠物社区、商品信息等、店主实现了商品信息管理。用户…

Windows PowerShell 有没有类似conda的虚拟环境功能?

PowerShell本身并不直接提供与Conda完全相同的环境功能&#xff0c;但PowerShell可以通过一些方法和工具来实现类似的环境管理。以下是对PowerShell和Conda环境功能的详细对比及PowerShell实现类似功能的途径&#xff1a; 一、Conda的环境功能 Conda是一个开源的包管理系统和…

OpenAI研究揭示ChatGPT的性别和种族偏见

&#x1f989; AI新闻 &#x1f680; OpenAI研究揭示ChatGPT的性别和种族偏见 摘要&#xff1a;OpenAI发布了一项新研究&#xff0c;指出ChatGPT在回应用户时&#xff0c;可能会根据姓名推断性别和种族特征&#xff0c;从而反映训练数据中的社会偏见。例如&#xff0c;女性名…

基于Docker安装Grafana及其基本功能

Grafana是一款用Go语言开发的开源数据可视化工具&#xff0c;可以做数据监控和数据统计&#xff0c;带有告警功能。 拉取Grafana镜像 docker pull grafana/grafana 运行镜像 docker run -d -p 3000:3000 --namegrafana grafana/grafana 打开浏览器&#xff0c;访问 http://l…

css3新增都知识点

1.新的选择器 属性选择器、伪类选择器、伪元素选择器 2.圆角与阴影 border-radius&#xff08;圆角&#xff09;、box-shadow&#xff08;阴影&#xff09; 3.渐变 线性渐变&#xff08;linear-gradient&#xff09; 径向渐变&#xff08;radial-gradient&#xff09; 4…

MySQL 连接的使用

MySQL 连接的使用 MySQL 是一种广泛使用的开源关系数据库管理系统。它基于 Structured Query Language (SQL) 进行数据管理,因其高性能、易用性和可靠性而受到开发者的青睐。在许多应用程序中,尤其是那些需要处理大量数据或支持复杂查询的应用程序中,MySQL 的使用是至关重要…