• 鸿蒙开发实战-手写一个Openharmony投屏工具


    实战手写一个Openharmony投屏工具,实现代码分享如下:

    java
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    public class ImageFrame extends JFrame {
    
        private BufferedImage originalImage;
        private final String imagePath = System.getProperty("user.dir") + File.separator + "scr.jpeg"; // 图片文件的路径
        private final double aspectRatio = 720.0 / 1280.0; // 定义比例
        private ImagePanel imagePanel;
        private long pressTime; // 用于记录鼠标按下的时间
        private int initWidth = 517;  //初始窗口宽
        private int initHeight = 908;   //初始窗口高
    
        public ImageFrame() {
            setTitle("OHScrcpy(by zhonghongfa)");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // setLocationRelativeTo(null);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            int screenWidth = screenSize.width;
            int screenHeight = screenSize.height;
            // 计算窗体在屏幕右侧的新位置
            int x = screenWidth - initWidth - 50;
            int y = 50; // 如果希望窗体垂直居中
            // 设置窗体的新位置
            setLocation(x, y);
    
            loadImage();
            imagePanel = new ImagePanel();
            add(imagePanel);
            setIconImage();
    
            addComponentListener(new ComponentAdapter() {
                public void componentResized(ComponentEvent e) {
                    maintainAspectRatio();
                }
            });
    
            imagePanel.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    pressTime = System.currentTimeMillis(); // 记录鼠标按下的时间
                }
    
                @Override
                public void mouseReleased(MouseEvent e) {
                    long releaseTime = System.currentTimeMillis(); // 记录鼠标释放的时间
                    double scaleX = (double) originalImage.getWidth() / imagePanel.getWidth();
                    double scaleY = (double) originalImage.getHeight() / imagePanel.getHeight();
                    int realX = (int) (e.getX() * scaleX);
                    int realY = (int) (e.getY() * scaleY);
                    if (releaseTime - pressTime > 500) { // 如果按下时间超过500毫秒,则认为是长按
                        System.out.println("Real Long pressed coordinates: (" + realX + ", " + realY + ")");
                        executeCommand(String.format("hdc shell uitest uiInput longClick %d %d", realX, realY));
                    }else{
                        System.out.println("Real clicked coordinates: (" + realX + ", " + realY + ")");
                        // 构建并执行命令
                        executeCommand(String.format("hdc shell uitest uiInput click %d %d", realX, realY));
                    }
                }
            });
    
            setSize(initWidth, initHeight); // 设置初始窗口大小
            new Timer(100, e -> updateImageAndReload()).start();
        }
    
        private void setIconImage() {
            try {
                BufferedImage iconImage = ImageIO.read(getClass().getResource("/OHScrcpy.png")); // 注意路径前的斜杠
                setIconImage(iconImage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private void loadImage() {
            try {
                File imageFile = new File(imagePath);
                if (imageFile.exists()) {
                    originalImage = ImageIO.read(imageFile);
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        private void executeCommand(String command) {
            try {
                System.out.println("Executing command: " + command);
                Process process = Runtime.getRuntime().exec("cmd.exe /c " + command);
                process.waitFor();
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        private void updateImage() {
            try {
                // 执行外部命令更新图片
                String[] cmd = {
                        "cmd.exe",
                        "/c",
                        "hdc shell rm -rf /data/local/tmp/scr.jpeg & hdc shell snapshot_display -f /data/local/tmp/scr.jpeg & hdc file recv  /data/local/tmp/scr.jpeg"
                };
                Process p = Runtime.getRuntime().exec(cmd);
                p.waitFor(); // 等待命令执行完成
    
            } catch (IOException | InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    
        private void updateImageAndReload() {
            updateImage();
            loadImage();
            imagePanel.repaint();
        }
    
        private void maintainAspectRatio() {
            Rectangle bounds = this.getBounds();
            int width = bounds.width;
            int height = (int) (width / aspectRatio);
            if (height != bounds.height) {
                this.setSize(width, height);
            }
        }
    
        // JPanel 的子类用于绘制图像
        private class ImagePanel extends JPanel {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (originalImage != null) {
                    g.drawImage(originalImage, 0, 0, getWidth(), getHeight(), this); // 动态调整图片大小
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> new ImageFrame().setVisible(true));
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150

    运行效果如下

    Openharmony投屏工具_Java

    485f48c927688f7ba739dc1871e0214e.jpeg

  • 相关阅读:
    jquery || js 实现阴影背景弹窗
    基于 QUIC 协议的 HTTP/3 正式发布!
    C语言学习之路(基础篇)—— 指针(上)
    Python入门教程 | Python 迭代器与生成器
    翻页类视图 ViewPager
    Feign源码解析:初始化过程(一)
    图像处理-形态学处理
    右岸物联面经
    UE4(unreal engine)虚幻引擎中安装light explorer灯光管理器插件
    如何应对数据安全四大挑战?亚马逊云科技打出“组合拳”
  • 原文地址:https://blog.csdn.net/m0_62167422/article/details/136302022