码迷,mamicode.com
首页 > 编程语言 > 详细

Java应用程序实现屏幕的"拍照"

时间:2017-01-15 09:50:14      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:***   远程服务器   public   getheight   照相   计算机   rectangle   int   windows   

原文:http://www.cnblogs.com/visec479/p/3842970.html

 

有时候,在Java应用程序开发中,如:远程监控或远程教学,常常需要对计算机的屏幕进行截取,由于屏幕截取是比较接近操作系统的操作,在Windows操作系统下,该操作几乎成了VC、VB等的专利,事实上,使用Java JDK1.4 的Robot对象,来完成"屏幕截取操作,更加简单。Java JDK1.4 的Robot对象,该对象可以完成对"屏幕"像素的拷贝,完成屏幕图像截取操作。Java应用程序中可以直接调用此对象,完成对特定应用程序的屏幕截取,如果将此功能配合网络,便可以轻而易举地实现远程服务器屏幕的监视。本文向大家介绍如何用Java构建屏幕"照相机"并实现远程服务器屏幕的监视,并给出了相应的Java源代码。

package Camera;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import java.awt.*;
/*******************************************************************
 * 该JavaBean可以直接在其他Java应用程序中调用,实现屏幕的"拍照"
 * This JavaBean is used to snapshot the GUI in a 
 * Java application! You can embeded
 * it in to your java application source code, and us
 * it to snapshot the right GUI of the application
 * @see javax.ImageIO
 * @author Visec·Dana
 * @version 1.0
 *****************************************************/
public class GuiCamera {
    private String fileName; //文件的前缀
    private String defaultName = "GuiCamera";
    static int serialNum=0;
    private String imageFormat; //图像文件的格式
    private String defaultImageFormat="png";
    Dimension d=Toolkit.getDefaultToolkit().getScreenSize();

    /****************************************************************
     * 默认的文件前缀为GuiCamera,文件格式为PNG格式
     * The default construct will use the default 
     * Image file surname "GuiCamera", 
     * and default image format "png"
     ****************************************************************/
    public GuiCamera() {
        fileName = defaultName;
        imageFormat=defaultImageFormat;

    }
    /****************************************************************
     * @param s the surname of the snapshot file
     * @param format the format of the  image file, 
     * it can be "jpg" or "png"
     * 本构造支持JPG和PNG文件的存储
     ****************************************************************/
    public GuiCamera(String s,String format){
        fileName = s;
        imageFormat=format;
    }
    /****************************************************************
     * 对屏幕进行拍照
     * snapShot the Gui once
     ****************************************************************/
    public void snapShot(){
        try {
            //拷贝屏幕到一个BufferedImage对象screenshot
            BufferedImage screenshot = (new Robot()).createScreenCapture(new Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
            //根据文件前缀变量和文件格式变量,自动生成文件名
            String name=fileName+String.valueOf(serialNum)+"."+imageFormat;
            File f = new File(name);
            System.out.print("Save File "+name);
            //将screenshot对象写入图像文件
            ImageIO.write(screenshot, imageFormat, f);
            System.out.print("..Finished!\n");
        }
        catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

 

调用测试案例

 

package Camera;
import java.text.SimpleDateFormat;
import java.util.Date;
/***
 * 实现屏幕的"拍照"
 * @author Visec·Dana
 */
public class Client{
    public static void main(String[] args) {
        Date date=new Date();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd-HH-mm");
        GuiCamera cam= new GuiCamera("F://"+df.format(date), "png"); 
        cam.snapShot();
    }
}

数据记录生成图片

技术分享

Java应用程序实现屏幕的"拍照"

标签:***   远程服务器   public   getheight   照相   计算机   rectangle   int   windows   

原文地址:http://www.cnblogs.com/shihaiming/p/6286597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!