import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class CaptureScreen {
public static void captureScreen(String fileName, String folder) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
//保存路径
File screenFile = new File(fileName);
if (!screenFile.exists()) {
screenFile.mkdir();
}
File f = new File(screenFile, folder);
ImageIO.write(image, "png", f);
//自动打开
if (Desktop.isDesktopSupported()
&& Desktop.getDesktop().isSupported(Desktop.Action.OPEN))
Desktop.getDesktop().open(f);
}
public static void main(String[] args) {
try {
captureScreen("/Users/sun/Documents","1.png");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
版权属于: 技术客
原文地址: https://www.sunjs.com/article/detail/64be78bd8a014bf8b9f18a1b4ac3a9b0.html
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。