我想一次截取比我的屏幕大的整个网站的屏幕截图。我知道如何使用 Robot 类来截取可见区域的屏幕截图。我认为这样做的一种方法是:
- 启动浏览器,转到所需的网站
- 启动我的程序
- 程序将对可见区域进行截图
- 程序将向下滚动以使页面的后半部分可见并截取屏幕截图
- 两个屏幕截图将合并
这有点笨拙的解决方案,在这一点上我什至不确定它是否可能(滚动浏览器的窗口)。所以我正在寻找更好的解决方案的提示。
编辑 1:这有点像我在原始帖子中所设想的。流程是(原型):
- 打开浏览器,在显示器 1 中转到所需的网站
- 在监视器 2 中,运行程序(在我的例子中来自 Net beans)
- 程序捕获第一个屏幕截图,但如果调用 Robot.mouseScroll 它是 NetBeans 窗口滚动而不是 webbrowser
- 要滚动浏览器屏幕,我移动鼠标 Monitor 1,单击它以获得焦点,滚动然后再截取另一个屏幕截图。现在我有两个可以拼接的 png 图像。
问题是,虽然我在技术上可以做到这一点,但我无法同时在我的计算机上做任何其他事情,因为我正在通过程序移动鼠标。目标是让我的程序在 Excel 中工作时截取屏幕截图并同时分析图像(拼接和提取信息)。按照目前的设置,这是不可能的。
此外,Java 似乎根本无法做到这一点。有趣的是,足够多的 JavaScript 可能能够并且看起来像带有 Win Api 的 C++ 将能够如此。这是一种耻辱。有什么想法吗?
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
int width;
int height;
try{
// Get screen devices.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gdevices = ge.getScreenDevices();
// Find out widht and height of each, print line.
for (int i = 0; i < gdevices.length; i++){
System.out.println("Device " + i);
System.out.println("Width:" + gdevices[i].getDisplayMode().getWidth());
System.out.println("Height:" + gdevices[i].getDisplayMode().getHeight());
}
// Get width and height again for later. Don't worry about this now.
width = gdevices[1].getDisplayMode().getWidth();
height = gdevices[1].getDisplayMode().getHeight();
// Initiate robot.
Robot robot = new Robot(gdevices[1]);
// Size of printscreen area. Temporary. Will be entire screen later.
Rectangle captureSize = new Rectangle(0,0,500,500);
// Take screenshot on gdevice[1]
BufferedImage bufferedImage = robot.createScreenCapture(captureSize);
File outputfile = new File("My Screenshot" + 1 + ".png");
boolean write = ImageIO.write(bufferedImage, "png",outputfile);
// Preparing to take another screenshot after.
/* Need to move mouse to monitor where screenshot is taken. Mouse
* is currently at a monitor that displays NetBeans, which is where
* I'm running this from, for now.
*/
robot.mouseMove(200,200);
/* Need to activate window by gaining focus, don't how to do with robot
* do with mouse instead.
*/
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
/* After the first screen shot, scroll screen that's now
* active (focused?)
*/
robot.mouseWheel(3);
// Take second screenshot.
robot.delay(1000);
bufferedImage = robot.createScreenCapture(captureSize);
outputfile = new File("My Screenshot" + 2 + ".png");
//write into second half
write = ImageIO.write(bufferedImage, "png",outputfile);
}
catch (AWTException e){
System.err.println("Somethingfishy is going on ...");
}
}
}
编辑 2:新方法 好的,考虑到这一点,即使我找到了焦点(例如使用 alt+tab 而不是移动)它也无济于事,因为我需要同时在 excel 中工作。我将开始另一个关于“虚拟监视器”想法的问题。感谢两位提供想法。