直接上代码:

package robot;


import java.awt.Rectangle;

import java.awt.Robot;

import java.awt.event.InputEvent;

import java.awt.p_w_picpath.BufferedImage;

import java.io.File;

import java.io.IOException;


import javax.p_w_picpathio.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();

}

}






效果图:

java robot类自动截屏

ps:Robot类真的好强大啊!!



转载于:https://blog.51cto.com/7385573/1390849

相关文章:

  • 2021-09-17
  • 2021-11-03
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2021-12-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-28
  • 2022-12-23
  • 2021-05-24
  • 2022-01-18
  • 2022-01-09
  • 2022-12-23
相关资源
相似解决方案