简体   繁体   English

捕获活动窗口的屏幕截图

[英]Capture screenshot of Active Window

I've created a small application that allows the user to capture the screenshot and save the image at a user defined location. 我创建了一个小应用程序,该应用程序允许用户捕获屏幕快照并将图像保存在用户定义的位置。 It also allows the user to capture the screenshot of the active window as well. 它还允许用户捕获活动窗口的屏幕截图。

The issue is with the active window. 问题在于活动窗口。 I have learnt that Java does not provide a method to get the screenshot of the active window. 我了解到Java没有提供获取活动窗口屏幕快照的方法。 So, I googled and found that developers have been using the Alt+PrntScrn to get the screenshot and get the image from the clipboard. 因此,我搜寻了一下,发现开发人员一直在使用Alt + PrntScrn来获取屏幕截图并从剪贴板中获取图像。

The code is the following one: 代码如下:

     robot.keyPress(KeyEvent.VK_ALT);
     robot.keyPress(KeyEvent.VK_PRINTSCREEN);
     robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
     robot.keyRelease(KeyEvent.VK_ALT);
     Thread.sleep(1500);
     RenderedImage image = (RenderedImage)getClipboard();
     saveToImage(image,targetLocation);

Now what happens is that when the Alt key is pressed by the robot class, the focus goes on the toolbar of the application of which the user wants to get the screenshot and the screenshot is taken of the toolbar and not that of the window that the user has focus on. 现在发生的是,当机器人类按下Alt键时,焦点移到了用户想要获取其屏幕截图的应用程序的工具栏上,而该屏幕截图是该工具栏上的,而不是该窗口的截图。用户专注于。

Please advise on how to solve this problem. 请提供有关如何解决此问题的建议。

如果这是我的应用程序,我将使用JNA-Java Native Access获取活动窗口及其大小,然后使用该信息允许机器人抓取图像。

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

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