简体   繁体   English

如何截取任何 java 本机应用程序而不将其带到 Java 中的前台?

[英]How to take screenshot of any java native application without bringing it to foreground in Java?

I want to take the screenshot of a java native application ( any framework AWT, Swing, JavaFx ) without bringing it to the foreground .我想截取 java 本机应用程序(任何框架 AWT、Swing、JavaFx)的屏幕截图,而不将其带到前台 Are there any framework-specific methods available for this?是否有任何特定于框架的方法可用于此?

I have tried using Robot class to get the screenshot我试过使用 Robot class 来获取屏幕截图

private static void capture(int x, int y , int width , int height, String setName) {
    Robot robot = new Robot();
    Rectangle area = new Rectangle(x, y, width, height);
    BufferedImage image = robot.createScreenCapture(area);
    ImageIO.write(image, "png", new File(System.getProperty("user.dir") + "\\images\\" + setName +".png"));
}

Now robot class with just take the area coordinates and capture the image, whether the target application is on the top or not, to get the application on the top I am using JNA to bring it to focus现在机器人 class 只需获取区域坐标并捕获图像,无论目标应用程序是否在顶部,为了让应用程序位于顶部,我正在使用 JNA 使其聚焦

private static void bringToFocus() {
    for (DesktopWindow desktopWindow : WindowUtils.getAllWindows(true)) {
        if (desktopWindow.getTitle().contains("notepad")) {
            HWND hwnd = User32.INSTANCE.FindWindow(null, desktopWindow.getTitle());
            User32.INSTANCE.SetForegroundWindow(hwnd);
            break;
        }
    }
}

But this is an example where we need to capture only one application, if we need to capture 10 applications screenshots we need to one by one bring them to the front and capture and bring next.但这是一个我们只需要捕获一个应用程序的示例,如果我们需要捕获 10 个应用程序的屏幕截图,我们需要将它们一个一个地带到前面,然后捕获并带到下一个。

Is there any framework specific method availabe which can take the application screenshot without bringing it to the front.是否有任何可用的框架特定方法可以截取应用程序屏幕截图而无需将其带到前面。

If your screen shot only needs to be of the Java GUI, you can paint to a BufferedImage如果您的屏幕截图只需要是 Java GUI,您可以绘制到 BufferedImage

public static Image screenShot(Component c) {
  BufferedImage im = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
  Graphics g = im.getGraphics();
  c.paint(g); // Paint is the proper entry point to painting a (J)Component, rather than paintComponent
  g.dispose(); // You should dispose of your graphics object after you've finished
  return im;
}

If your requirement is to paint the Java GUI component along with the rest of the screen, but like your java (J)Frame is in front, you can do that by painting the screen using the robot first, then doing what I've posted above, but with the BufferedImage (which has already been drawn on) being passed in as a parameter rather than being created in the method.如果您的要求是绘制 Java GUI 组件以及屏幕的 rest,但就像您的 java (J)Frame 在前面一样,您可以先使用机器人绘制屏幕,然后执行我发布的操作上面,但是 BufferedImage(已经绘制)作为参数传入,而不是在方法中创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM