java 截取电脑屏幕转为图片

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public static void main(String[] args) {
        // 创建JMapViewer实例
//        JMapViewer mapViewer = new JMapViewer();

        // 获取屏幕截图
        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        try {
            Robot robot = new Robot();
            BufferedImage screenFullImage = robot.createScreenCapture(screenRect);

            // 截取屏幕区域
            BufferedImage mapImage = screenFullImage.getSubimage(0, 0, 1920, 1080);

            // 保存图片
            File outputFile = new File("E:/map.png");
            ImageIO.write(mapImage, "png", outputFile);

            System.out.println("图片已保存至 " + outputFile.getAbsolutePath());
        } catch (AWTException | IOException e) {
            e.printStackTrace();
        }
    }

 

posted @ 2024-08-15 11:16  涂小二  阅读(6)  评论(0编辑  收藏  举报