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();
            }
    
        }
    
    }

    .

  • 相关阅读:
    Proj THUDBFuzz Paper Reading: The Art, Science, and Engineering of Fuzzing: A Survey
    Proj THUDBFuzz Paper Reading: A systematic review of fuzzing based on machine learning techniques
    9.3 付费代理的使用
    11.1 Charles 的使用
    第十一章 APP 的爬取
    10.2 Cookies 池的搭建
    10.1 模拟登录并爬取 GitHub
    11.5 Appium 爬取微信朋友圈
    11.4 Appium 的基本使用
    11.3 mitmdump 爬取 “得到” App 电子书信息
  • 原文地址:https://www.cnblogs.com/jonban/p/10713458.html
Copyright © 2011-2022 走看看