zoukankan      html  css  js  c++  java
  • Java应用程序实现屏幕的"拍照"

    原文: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!
    ");
            }
            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();
        }
    }

    数据记录生成图片

  • 相关阅读:
    odoo开发笔记 -- 新建模块扩展原模块增加菜单示例
    div内部div居中
    Css中!important的用法
    SQLServer日期格式转换
    jquery中innerheight outerHeight()与height()的区别
    简单明了区分escape、encodeURI和encodeURIComponent
    PDF预览之PDFObject.js总结
    PDFObject.js,在页面显示PDF文件
    System.IO.Directory.GetCurrentDirectory与System.Windows.Forms.Application.StartupPath的用法
    angular 模块 @NgModule的使用及理解
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6286597.html
Copyright © 2011-2022 走看看