实战手写一个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);
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) {
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);
}
}
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));
}
}
运行效果如下 :
(责任编辑:阿里技术) |