zoukankan      html  css  js  c++  java
  • Java 截屏工具类

    PrintScreenUtils.java

    package javax.utils;
    
    import java.awt.AWTException;
    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 java.io.IOException;
    import java.io.OutputStream;
    
    import javax.imageio.ImageIO;
    
    /**
     * 截屏工具类
     * 
     * @author Logan
     * @createDate 2019-04-15
     * @version 1.0.0
     *
     */
    public class PrintScreenUtils {
    
        /**
         * 图片类型
         * 
         * @author Logan
         * @createDate 2019-04-15
         * @version 1.0.0
         *
         */
        public class ImageType {
            public static final String JPG = "jpg";
            public static final String PNG = "png";
            public static final String GIF = "gif";
        }
    
        /**
         * 截取当前屏幕图片
         * 
         * @return BufferedImage对象
         * @throws AWTException 抛出异常,由调用者处理
         */
        public static BufferedImage getImage() throws AWTException {
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle screenRect = new Rectangle(screenSize);
    
            Robot robot = new Robot();
            return robot.createScreenCapture(screenRect);
        }
    
        /**
         * 截取当前屏幕图片并输出到指定文件
         * 
         * @param output 指定输出文件,默认输出png格式
         * @return 是否保存截屏成功
         * @throws IOException 抛出异常,由调用者处理
         * @throws AWTException 抛出异常,由调用者处理
         */
        public static boolean write(File output) throws IOException, AWTException {
            return write(ImageType.PNG, output);
        }
    
        /**
         * 截取当前屏幕图片并输出到指定文件
         * 
         * @param imageType 输出图片类型
         * @param output 指定输出文件
         * @return 是否保存截屏成功
         * @throws IOException 抛出异常,由调用者处理
         * @throws AWTException 抛出异常,由调用者处理
         */
        public static boolean write(String imageType, File output) throws IOException, AWTException {
            return ImageIO.write(getImage(), imageType, output);
        }
    
        /**
         * 截取当前屏幕图片并输出到指定输出流
         * 
         * @param output 图片输出流,可以是网络响应输出流,文件输出流等。默认输出png格式
         * @return 是否输出截屏图片成功
         * @throws IOException 抛出异常,由调用者处理
         * @throws AWTException 抛出异常,由调用者处理
         */
        public static boolean write(OutputStream output) throws IOException, AWTException {
            return write(ImageType.PNG, output);
        }
    
        /**
         * 截取当前屏幕图片并输出到指定输出流
         * 
         * @param imageType 输出图片类型
         * @param output 图片输出流,可以是网络响应输出流,文件输出流等
         * @return 是否输出截屏图片成功
         * @throws IOException 抛出异常,由调用者处理
         * @throws AWTException 抛出异常,由调用者处理
         */
        public static boolean write(String imageType, OutputStream output) throws IOException, AWTException {
            return ImageIO.write(getImage(), imageType, output);
        }
    
    }

    以下是测试程序

    PrintScreenTest.java

    package com.java.test;
    
    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.utils.PrintScreenUtils;
    import javax.utils.PrintScreenUtils.ImageType;
    
    import org.apache.commons.io.FileUtils;
    import org.junit.Test;
    
    /**
     * @author Logan
     * @createDate 2019-04-15
     * @version 1.0.0
     *
     */
    public class PrintScreenTest {
    
        private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        private SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    
        @Test
        public void demo() {
            Date now = new Date();
            String dir = dateFormat.format(now);
            String name = timeFormat.format(now) + ".jpg";
            File file = FileUtils.getFile(FileUtils.getUserDirectory(), dir, name);
    
            try {
                FileUtils.forceMkdirParent(file);
                System.out.println(file.getAbsolutePath());
                boolean write = PrintScreenUtils.write(ImageType.JPG, file);
                System.out.println(write);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    }

    .

  • 相关阅读:
    使用golang访问kubebernetes
    使用 Rancher 管理现有 Kubernetes 集群
    Running powershell scripts during nuget package installation and removal
    How to Create, Use, and Debug .NET application Crash Dumps in 2019
    寻找写代码感觉(一)之使用 Spring Boot 快速搭建项目
    Selenium+Java之解决org.openqa.selenium.InvalidArgumentException: invalid argument报错问题
    Selenium环境搭建
    关于Xpath定位方法知道这些基本够用
    Web自动化之浏览器启动
    【翻译】编写代码注释的最佳实践
  • 原文地址:https://www.cnblogs.com/jonban/p/10713458.html
Copyright © 2011-2022 走看看