直接上代码:
package robot;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.glass.events.KeyEvent;
public class Robot_test {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
//创建robot类
Robot robot = new Robot();
screen_shot(robot);
//robot.delay(5000);
/*robot.keyPress(KeyEvent.VK_N);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_P);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_D);
robot.keyPress(KeyEvent.VK_ENTER);*/
//robot.mousePress(InputEvent.BUTTON1_MASK);
//robot.mousePress(InputEvent.BUTTON1_MASK);
//robot.mousePress(InputEvent.BUTTON1_MASK);
//robot.mouseRelease(InputEvent.BUTTON1_MASK);
}catch(Exception e){
e.printStackTrace();
}
}
/*
* 截屏
*/
public static void screen_shot(Robot robot){
Rectangle screenRect = new Rectangle(100, 150, 400, 800);//这里可以自己设置图片范围
//截图
BufferedImage bufferedImage = robot.createScreenCapture(screenRect);
//保存截图
File file = new File("screenRect.png");
try {
ImageIO.write(bufferedImage, "png", file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
效果图:
ps:Robot类真的好强大啊!!