4

私は時間を追跡するために使用する小さなコードを持っています-非常に単純に、4分ごとにデスクトップの写真を撮るので、後で私が日中何をしていたかを振り返ることができます-それは素晴らしい働きをします、外部モニターに接続する場合を除いて-このコードはラップトップ画面のスクリーンショットのみを撮影し、作業中のより大きな外部モニターは撮影しません-コードを変更する方法はありますか?関連する場合に備えて、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 でコードレビュー中です。

4

2 に答える 2

10

その方法を示すチュートリアルJava マルチモニターのスクリーンショットがあります。基本的に、すべての画面を反復する必要があります。

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

for (GraphicsDevice screen : screens) {
 Robot robotForScreen = new Robot(screen);
 ...
于 2012-04-06T10:31:50.003 に答える
0

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).

If you have your monitors setup this way for example: [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);;
    }
}
于 2017-02-06T12:58:42.780 に答える