首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用java获取其他程序的活动窗口

使用java获取其他程序的活动窗口
EN

Stack Overflow用户
提问于 2010-05-30 15:13:34
回答 3查看 2.6K关注 0票数 1

我正在尝试制作一个Java应用程序,可以从其他活动窗口程序,如信息栏,甚至其他活动窗口的屏幕截图中获取信息。

在这种情况下,JNI是唯一的选择吗?

谢谢。

EN

回答 3

Stack Overflow用户

发布于 2010-05-30 16:21:53

使用纯Java是不可能实现这些事情的。

您的选择是通过JNI / JNI使用本机库,或者使用Runtime.exec(...)调用一些外部应用程序。

票数 0
EN

Stack Overflow用户

发布于 2010-05-31 10:39:50

获取有关操作系统的其他项目的信息需要通过特定于操作系统的方法来完成。你也许能够从你的程序内部启动一些脚本,并通过读取这些脚本的输出来获取一些信息?

否则,JNI就是最佳选择。Java背后的整个理念是独立于平台,这是通过隐藏操作系统来实现的。

屏幕截图是可能的

代码语言:javascript
运行
复制
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class RobotExp {

    public static void main(String[] args) {

        try {

            Robot robot = new Robot();
            // Capture the screen shot of the area of the screen defined by the rectangle
            BufferedImage bi=robot.createScreenCapture(new Rectangle(100,100));
            ImageIO.write(bi, "jpg", new File("C:/imageTest.jpg"));

        } catch (AWTException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2018-07-18 01:24:29

您可以使用以下代码,并能够检索已激活窗口的屏幕截图。

此外,为了捕获窗口裁剪得更好的图像,请根据需要调整以下变量

代码语言:javascript
运行
复制
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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2937844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档