zoukankan      html  css  js  c++  java
  • java 获取当前屏幕截图

    package tools;
    
    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 JieTu{
        private String fileName; //文件的前缀
        private String defaultName = "GuiCamera";
        static int serialNum=0;
        private String imageFormat; //图像文件的格式
        private String defaultImageFormat="png";
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        private int ax = 0;
        private int ay = 0;
        private int bx = 0;
        private int by = 0;
    
        public JieTu() {
            this.fileName = defaultName;
            this.imageFormat=defaultImageFormat;
    
        }
    
        public JieTu(String s,String format) {
            this.fileName = s;
            this.imageFormat=format;
        }
    
        public JieTu(String s,String format,int ax, int ay, int bx, int by) {
            this.ax = ax;
            this.ay = ay;
            this.bx = bx;
            this.by = by;
            this.fileName = s;
            this.imageFormat=format;
        }
    
        /****************************************************************
        * 对屏幕进行拍照
        * snapShot the Gui once
        ****************************************************************/
        public void snapShot() {
            try {
                //拷贝屏幕到一个BufferedImage对象screenshot
    //            int x = (int) d.getWidth();//屏幕宽;
    //            int y = (int) d.getHeight();//屏幕高
                BufferedImage screenshot = (new Robot()).createScreenCapture(new
                Rectangle(ax, ay, bx-ax, by-ay));
                serialNum++;
                //根据文件前缀变量和文件格式变量,自动生成文件名
                String name=fileName+String.valueOf(serialNum)+"."+imageFormat;
                File f = new File(name);
                if(f.exists()){
                    f.delete();
                }
                System.out.print("Save File "+name);
                //将screenshot对象写入图像文件
                ImageIO.write(screenshot, imageFormat, f);
                System.out.print("..Finished!
    ");
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    
        public static void main(String[] args){
            //Thread.sleep(5000);
            JieTu cam= new JieTu("d:\Hello", "png", 0, 0, 600, 800);//
            cam.snapShot();
    
        }
    }
  • 相关阅读:
    关于最大最小的k个数的类型题总结
    最小的K个数(剑指offer)
    215. Kth Largest Element in an Array(返回数组中第几大元素)(leetcode)
    数组中出现次数超过一半的数字(剑指offer)
    二叉搜索树与双向链表(剑指offer)
    第四届蓝桥杯c/c++B组6
    第四届蓝桥杯c/c++B组7
    第四届蓝桥杯c/c++B组8
    第四届蓝桥杯c/c++B组9
    第五届蓝桥杯 c/c++ B组8
  • 原文地址:https://www.cnblogs.com/jinloooong/p/14241082.html
Copyright © 2011-2022 走看看