闲的没事,写个从书上看到的一个小例子,java获取电脑屏幕内容,类似于qq截图吧。直接上代码,都有注释比较简单。
public class ScreenPhoto {
public static void main(String[] args) throws AWTException, IOException {
// 获取屏幕尺寸大小
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(size);
Robot robot = new Robot();
// 截取屏幕
BufferedImage image = robot.createScreenCapture(rectangle);
File screenFile = new File("D:");
if (!screenFile.exists()) {
screenFile.mkdir();
}
File f = new File(screenFile, "1.png");
// 写入文件
ImageIO.write(image, "png", f);
// 自动打开
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN))
Desktop.getDesktop().open(f);
}
}
最后贴一张效果图