[英]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.