java - 使用java获取其他程序的 Activity 窗口

标签 java windows java-native-interface

我正在尝试制作一个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/

相关文章:

javascript - 使用多数据表单上传文件时,通过 ajax 调用从服务器获取响应

java - HIbernate fetch join 发出额外的 sql 语句

JavaFX TreeView 恢复滚动状态

c++ - Win32 命名管道和远程客户端

java - JNI - java ArrayList 转换为 c++ std::string*

java 函数返回比 Boolean 更多的状态类型

c++ - 在测试工具下获取当前可执行目录失败

Windows 7 任务栏进度在一个应用程序中有效,但在另一个应用程序中无效

java - 在 JNI 调用中调用 getFilesDir() 时出现 NullPointerException

java.lang.UnsatisfiedLinkError,mach-o 但 Mac10.6.2 上的架构错误