myScreenShot.java
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.UUID;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class myScreenShot {

    public static void main(String[] args) throws AWTException{
        String fileName = "mypic";    //是个文件夹名字
        screenShot(fileName);
    }

    public static final Logger logger = LogManager.getLogger(myScreenShot.class);


    public static String screenShot(String fileName) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小
        Rectangle screenRectangle = new Rectangle(screenSize);//根据屏幕大小创建一个矩形
        String randomName = UUID.randomUUID().toString().replace("-", "");
        String name = randomName + ".png";//jpg等也可
        // 截图保存的路径
        File screenFile = new File(fileName);
        if (!screenFile.exists()) {
            screenFile.mkdirs();//创建文件路径
        }
        Robot robot;
        String path = "";
        boolean b = false;
        try {
            robot = new Robot();
            BufferedImage image = robot.createScreenCapture(screenRectangle);//使用Robot类提供的截屏方法,
            File f = new File(screenFile, name);
            b = ImageIO.write(image, "png", f);
        } catch (Exception e) {

            logger.error("error : ", e);
        }

        if (b) {
            path = fileName + File.separator + name;
        }
        return path;
    }
}

 

posted on 2022-07-04 21:46  大话人生  阅读(1046)  评论(0编辑  收藏  举报