【发布时间】:2021-01-30 21:02:51
【问题描述】:
我发现java awt可以截取全屏,但是如何截取特定应用程序的屏幕呢?比如我打开了一个matlab应用和eclipse应用。而且我无法告诉程序应用程序屏幕的大小,只知道 matlab 现在是一个活动窗口。我只想捕获 Matlab 的屏幕。我该怎么做?
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;
import javax.imageio.ImageIO;
public class Screenshot {
public static final long serialVersionUID = 1L;
public static void main(String[] args)
{
try {
Thread.sleep(120);
Robot r = new Robot();
// It saves screenshot to desired path
String path = "D:// Shot.jpg";
// Used to get ScreenSize and capture image
Rectangle capture =
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage Image = r.createScreenCapture(capture);
ImageIO.write(Image, "jpg", new File(path));
System.out.println("Screenshot saved");
}
catch (AWTException | IOException | InterruptedException ex) {
System.out.println(ex);
}
}
}
【问题讨论】: