zoukankan      html  css  js  c++  java
  • java实现截屏

     1 import java.awt.Dimension;
     2 import java.awt.Rectangle;
     3 import java.awt.Robot;
     4 import java.awt.Toolkit;
     5 import java.awt.image.BufferedImage;
     6 import java.io.File;
     7 
     8 import javax.imageio.ImageIO;
     9 
    10 /*******************************************************************
    11 * 该JavaBean可以直接在其他Java应用程序中调用,实现屏幕的"拍照"
    12 * This JavaBean is used to snapshot the GUI in a
    13 * Java application! You can embeded
    14 * it in to your java application source code, and us
    15 * it to snapshot the right GUI of the application
    16 * @see javax.ImageIO
    17 * @author liluqun (liluqun@263.net)
    18 * @version 1.0
    19 *
    20 *****************************************************/
    21 
    22 public class GuiCamera
    23 {  
    24     private String fileName; //文件的前缀
    25     private String defaultName = "GuiCamera";
    26     static int serialNum=0;
    27     private String imageFormat; //图像文件的格式
    28     private String defaultImageFormat="png";
    29     Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    30 
    31     /****************************************************************
    32      * 默认的文件前缀为GuiCamera,文件格式为PNG格式
    33      * The default construct will use the default
    34      * Image file surname "GuiCamera",
    35      * and default image format "png"
    36      ****************************************************************/
    37     public GuiCamera() {
    38       fileName = defaultName;
    39       imageFormat=defaultImageFormat;
    40    
    41     }
    42 
    43     /****************************************************************
    44      * @param s the surname of the snapshot file
    45      * @param format the format of the  image file,
    46      * it can be "jpg" or "png"
    47      * 本构造支持JPG和PNG文件的存储
    48      ****************************************************************/
    49     public GuiCamera(String s,String format) {
    50    
    51       fileName = s;
    52       imageFormat=format;
    53     }
    54    
    55     /****************************************************************
    56      * 对屏幕进行拍照
    57      * snapShot the Gui once
    58      ****************************************************************/
    59     public void snapShot() {
    60    
    61       try {
    62       //拷贝屏幕到一个BufferedImage对象screenshot
    63         BufferedImage screenshot = (new Robot()).createScreenCapture(new
    64             Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
    65         serialNum++;
    66         //根据文件前缀变量和文件格式变量,自动生成文件名
    67         String name=fileName+String.valueOf(serialNum)+"."+imageFormat;
    68         File f = new File(name);
    69         System.out.print("Save File "+name);
    70       //将screenshot对象写入图像文件
    71         ImageIO.write(screenshot, imageFormat, f);
    72         System.out.print("..Finished!
    ");
    73       }
    74       catch (Exception ex) {
    75         System.out.println(ex);
    76       }
    77     }
    78 
    79     public static void main(String[] args)
    80     {
    81         GuiCamera cam= new GuiCamera("d:\Hello", "png");//
    82 
    83         cam.snapShot();
    84     }
    85 }

    原文:http://bbs.chinaunix.net/thread-770968-1-4.html

  • 相关阅读:
    PHP——语句和时间函数
    数据库——环境初建改端口和密码(转)
    PHP——初学,基础
    数据库——常用函数
    数据库——触发器(转)
    数据库——浅谈数据库中的存储过程(转)
    JavaScrip——简单练习(抓错误信息,for循环,日期)
    数据库——练习题答案
    django 创建APP 后找不到 templates文件夹下的Html文件。
    POSTGRESQL 9.1 FATAL: password authentication failed for user "postgres"
  • 原文地址:https://www.cnblogs.com/jevan/p/3450307.html
Copyright © 2011-2022 走看看