AWT机器人类Robot,实现屏幕的"拍照"

AWT 机器人类 Robot ,实现屏幕的 " 拍照 "
很多时候,我们希望能为我们的 JAVA 程序实现自动测试,自动演示功能,或者是其它的一些鼠标和键盘控制的应用。出于这样的目的,自从 JDK1.3 开始,它就为我们提供了一个用来产生本机输入事件的机器人类-- java.awt.Robot.  
  下面我们来看看 Robot 为我们提供了哪些功能。
   BufferedImage createScreenCapture(Rectangle screenRect)createScreenCapture 方法提供类似于键盘上的 PrintScreen 键的功能,将指定矩形区域内的屏幕像素 copy 下来产生一个 BufferedImage 。我们可以将这个方法用在图形程序中,或是用它来实现远端屏幕传输,可做成远端电脑监控程序等 . (在该实例中就用的是这个方法)
   void delay(int ms) 用来将当前的程序 (thread) 休眠 (sleep) 若干毫秒 (ms) 。可用来控制程序的延时。
   Color getPixelColor(int x, int y) 取得给定屏幕坐标像素位置的颜色值 . 用处就不多说了 .
   void keyPress(int keycode)
   void keyRelease(int keycode)
  这两个方法的作用一看便知,用来产生指定键的按键按下与抬起动作 , 相当于 Win32 API keyb_event 函数 . 可用于程序的自动演示、测试等 .
   void mouseMove(int x, int y) 将鼠标光标移动到指定的屏幕坐标 . 可用于程序的自动演示、测试等 .
   void mousePress(int buttons)
   void mouseRelease(int buttons)
   void mouseWheel(int wheelAmt)
  上面的三种方法,产生指定鼠标按钮的按下,抬起,及滚轮动作 . 一样也可用于程序的自动演示、测试等 .
以上介绍了 Robot 提供的实用的键盘鼠标控制功能,要了解更多 Robot 的方法,请查阅 JDK 文档 java.awt.Robot. 下面是完整的源代码,可以直接运行。
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;
/*******************************************************************
* 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 liluqun ([email][email protected][/email])
* @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()));
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);
}
}
public static void main(String[] args) {
GuiCamera cam = new GuiCamera( "d:\\Hello" , "png" ); //
cam.snapShot();
}
}
(注:解释及源代码,都是来自互联网,我只是综合了一下,感谢原作者。)

你可能感兴趣的:(java,jdk,.net,互联网,F#)