[英]Screen Capture in Java not capturing whole screen
我有一小段代码可以用来跟踪时间——非常简单,它每四分钟拍一张我的桌面照片,这样以后我就可以 go 回顾我白天所做的事情——效果很好,除了当我连接到外接显示器时 - 此代码只拍摄我的笔记本电脑屏幕的屏幕截图,而不是我正在使用的更大的外接显示器 - 任何想法如何更改代码? 我正在运行 OSX 以防相关...
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
class ScreenCapture {
public static void main(String args[]) throws
AWTException, IOException {
// capture the whole screen
int i=1000;
while(true){
i++;
BufferedImage screencapture = new Robot().createScreenCapture(
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
// Save as JPEG
File file = new File("screencapture"+i+".jpg");
ImageIO.write(screencapture, "jpg", file);
try{
Thread.sleep(60*4*1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
按照给出的解决方案,我做了一些改进,对于那些感兴趣的人,代码正在https://codereview.stackexchange.com/questions/10783/java-screengrab进行代码审查
有一个教程Java 多显示器屏幕截图显示了如何操作。 基本上你必须迭代所有屏幕:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();
for (GraphicsDevice screen : screens) {
Robot robotForScreen = new Robot(screen);
...
我知道这是一个老问题,但已接受答案中链接的解决方案可能不适用于某些多显示器设置(肯定是在 windows 上)。
例如,如果您以这种方式设置显示器:[3] [1] [2]
这是正确的代码:
public class ScreenshotUtil {
static public BufferedImage allMonitors() throws AWTException {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();
Rectangle allScreenBounds = new Rectangle();
for (GraphicsDevice screen : screens) {
Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
allScreenBounds = allScreenBounds.union(screenBounds);
}
Robot robot = new Robot();
return robot.createScreenCapture(allScreenBounds);;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.