Java屏幕"照相机"---Java, Robot, 屏幕截取

本文介绍如何使用Java JDK 1.4中的Robot类实现屏幕截图功能。通过简单的几行代码即可完成屏幕区域的选择及图像文件的保存。

  "屏幕的截取"是比较接近操作系统底层的操作,在Windows平台下,该操作似乎成了VC、VB等语言开发的专利。事实上,"屏幕的截取"在Java应用程序中,及其简单,核心代码只需要几行。在Java JDK1.4 中提供了一个"机器人"Robot类。该类用于产生与本地操作系统有关的底层输入、测试应用程序运行或自动控制应用程序运行。Robot类提供了一个方法:.createScreenCapture(..),可以直接将全屏幕或某个屏幕区域的像素拷贝到一个BufferedImage对象中,我们只需要将该对象写入到一个图像文件之中,就完成了屏幕到图像的拷贝过程。
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
*****************************************************/
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()));
serialNum++;
      //根据文件前缀变量和文件格式变量,自动生成文件名
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);
}
}
}

//测试


Java应用程序开发人员,可以在此基础上,如果将此GuiCamera JavaBean与增加多线程和网络功能,可以实现远程监控网络上另一台计算机屏幕。
如果我们稍加改造,封装成一个Servlet,便可以实现浏览器/服务器(B/S)计算结构 模式的小应用。客户端浏览器访问服务器上的Servlet时候,服务器拷贝服务器屏幕,并按照c文件相应格式,将拷贝的数据按照二进制数据流的方式返回客户端,客户端使用浏览器便可以查看远程服务器的屏幕,从而实现服务器屏幕的远程监视。 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值