简单的屏幕截图

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 Screenshot {
 
    public void captureScreen(String nameOfScreenshotFile, String formatOfFile, String screenshotPath) throws Exception {
        /*Dimension对象能帮助我们轻松地调整图片的高度和宽度 */
 
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        /*创建一个矩形,这个矩形的尺寸宽高从0坐标到screenSize的宽和高 */
 
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
 
        /*创建的图片将不会包含鼠标光标*/
 
        ImageIO.write(image, formatOfFile, new File(screenshotPath + nameOfScreenshotFile + "." + formatOfFile));
    }
 
    public static void main(String args[]) {
        Screenshot obj = new Screenshot();
        try {
            obj.captureScreen("trial", "jpeg", "C://");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(exception,String,image,File,Class,import)