简体   繁体   English

Java 中的屏幕截图未捕获整个屏幕

[英]Screen Capture in Java not capturing whole screen

I have a small piece of code that I use to keep track of time - very simply it takes a picture of my desktop every four minutes so that later I can go back over what I've been up to during the day - It works great, except when I connect to an external monitor - this code only takes a screen shot of my laptop screen, not the larger external monitor I'm working from - any ideas how to change the code?我有一小段代码可以用来跟踪时间——非常简单,它每四分钟拍一张我的桌面照片,这样以后我就可以 go 回顾我白天所做的事情——效果很好,除了当我连接到外接显示器时 - 此代码只拍摄我的笔记本电脑屏幕的屏幕截图,而不是我正在使用的更大的外接显示器 - 任何想法如何更改代码? I'm running OSX in case that's relevant...我正在运行 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();
}

            }
        }
}

Following the solution given, I made some improvements and the code, for those interested, is under code review at https://codereview.stackexchange.com/questions/10783/java-screengrab按照给出的解决方案,我做了一些改进,对于那些感兴趣的人,代码正在https://codereview.stackexchange.com/questions/10783/java-screengrab进行代码审查

There is a tutorial Java multi-monitor screenshots that shows how to do it.有一个教程Java 多显示器屏幕截图显示了如何操作。 Basically you have to iterate all screens:基本上你必须迭代所有屏幕:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();

for (GraphicsDevice screen : screens) {
 Robot robotForScreen = new Robot(screen);
 ...

I know this is an old question, but the solution linked on the accepted answer does not work probably on some multi monitor setups (on windows for sure).我知道这是一个老问题,但已接受答案中链接的解决方案可能不适用于某些多显示器设置(肯定是在 windows 上)。

If you have your monitors setup this way for example: [3] [1] [2]例如,如果您以这种方式设置显示器:[3] [1] [2]

Here is the correct code:这是正确的代码:

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.

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