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

通过电脑操作手机,支持单击,拖抓事件,延时有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*2340
                screenW = 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*2340
        jFrame = 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() {
        @Override
        public 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() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                onMouseClicked(e);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
                System.out.println("mousePressed");
                xPress = e.getX();
                yPress = e.getY();

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
                isMouseDragged = false;
//                System.out.println("mouseReleased isMouseDragged==" + isMouseDragged);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
//                System.out.println("mouseEntered");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                super.mouseExited(e);
//                System.out.println("mouseExited");
            }
        });

        jLabel.addMouseMotionListener(new MouseMotionListener() {

            @Override
            public void mouseDragged(MouseEvent e) {
                System.out.println("mouseDragged addMouseMotionListener " + e.getY());
                isMouseDragged = true;
                xEndPress = e.getX();
                yEndPress = e.getY();
                onMouseDragged(e);
            }

            @Override
            public void mouseMoved(MouseEvent e) {
//                System.out.println("mouseMoved addMouseMotionListener");
            }
        });
        jLabel.addMouseWheelListener(new MouseWheelListener() {
            @Override
            public 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 300
        String 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 250
        String command = "adb shell input tap " + TARGET_X + " " + TARGET_Y;
        // 使用adb shell输入命令模拟点击
        // adb shell input tap $TARGET_X $TARGET_Y
        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();
        }

    }

6、

相关推荐
聚名网15 小时前
手机无法连接服务器1302什么意思?
运维·服务器·智能手机
bbtianshi15 小时前
android 手机拍照相册选照片简单实现
智能手机
差不多就得了吧16 小时前
手机文件可以打印出来吗
智能手机·打印·手机打印·手机如何打印
vvw&19 小时前
使用同一个链接,如何实现PC打开是web应用,手机打开是一个H5应用
开发语言·前端·javascript·智能手机·面试题·每日一道前端面试题
鸿儒5171 天前
利用adb工具安装卸载安卓平板(手机)软件
android·adb·智能手机
刚作愁时又忆卿2 天前
小米PC电脑手机互联互通,小米妙享,小米电脑管家,老款小米笔记本怎么使用,其他品牌笔记本怎么使用,一分钟教会你
智能手机·电脑
阳排2 天前
一加ACE 3 Pro手机无法连接电脑传输文件问题
智能手机·电脑·一加
铅华尽3 天前
Java---JDBC案例--手机信息管理系统
java·开发语言·智能手机
花先锋队长3 天前
升级鸿蒙NEXT后,你的手机相机发生了这些变化……
华为·智能手机
老C资料库4 天前
《泰拉瑞亚》PC+手机版安装教程
智能手机