我正在尝试制作一个Java应用程序,它可以从其他 Activity 窗口程序获取信息,例如信息栏,甚至其他 Activity 窗口的屏幕截图。
在这种情况下 JNI 是唯一的选择吗?
谢谢。
最佳答案
您可以使用以下代码并能够检索激活窗口的屏幕截图。
此外, 要捕获更好裁剪的窗口图像,请根据需要调整以下变量
private static int adjustLeft = 6;
private static int fineCutterX = 6;
private static int fineCutterY = 6;
public void getActiveWindowImage() {
char[] buffer = new char[MAX_TITLE_LENGTH * 2];
HWND hwnd = User32.INSTANCE.GetForegroundWindow();
User32.INSTANCE.GetWindowText(hwnd, buffer, MAX_TITLE_LENGTH);
System.out.println("Active window title: " + Native.toString(buffer));
RECT rect = new RECT();
User32.INSTANCE.GetWindowRect(hwnd, rect);
int x = rect.left + adgustLeft;
int y = rect.top;
int width = rect.right - x - fineCutterX;
int hieght = rect.bottom - y - fineCutterY;
System.out.println(x + "," + y + "," + width + "," + hieght);
System.out.println("rect = " + rect);
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Capture the screen shot of the area of the screen defined by the rectangle
BufferedImage bi = robot.createScreenCapture(new Rectangle(x, y, width, hieght));
try {
ImageIO.write(bi, "jpg", new File("<File Path you need to save>"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
关于java - 使用java获取其他程序的 Activity 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2937844/