Openharmony投屏工具 原创

钟洪发老师
发布于 2024-2-20 12:14
浏览
4收藏

上课需要,临时写一个Openharmony投屏工具暂时用一下,实现代码分享如下:

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));
    }
}

运行效果如下:
Openharmony投屏工具-鸿蒙开发者社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
3
收藏 4
回复
举报
1条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

收藏学习下!

回复
2024-2-20 13:55:40
回复
    相关推荐