Java标准API中有个Robot类,该类可以实现屏幕截图,模拟鼠标键盘操作这些功能。这里只展示其屏幕截图。

  截图的关键方法createScreenCapture(Rectangle rect) ,该方法需要一个Rectangle对象,Rectangle就是定义屏幕的一块矩形区域,构造Rectangle也相当容易:

new Rectangle(int x, int y, int width, int height),四个参数分别是矩形左上角x坐标,矩形左上角y坐标,矩形宽度,矩形高度。截图方法返回BufferedImage对象,示例代码:
     /**
* 指定屏幕区域截图,返回截图的BufferedImage对象
* @param x
* @param y
* @param width
* @param height
* @return
*/
public BufferedImage getScreenShot(int x, int y, int width, int height) {
BufferedImage bfImage = null;
try {
Robot robot = new Robot();
bfImage = robot.createScreenCapture(new Rectangle(x, y, width, height));
} catch (AWTException e) {
e.printStackTrace();
}
return bfImage;
}

如果需要把截图保持为文件,使用ImageIO.write(RenderedImage im, String formatName, File output) ,示例代码:

     /**
* 指定屏幕区域截图,保存到指定目录
* @param x
* @param y
* @param width
* @param height
* @param savePath - 文件保存路径
* @param fileName - 文件保存名称
* @param format - 文件格式
*/
public void screenShotAsFile(int x, int y, int width, int height, String savePath, String fileName, String format) {
try {
Robot robot = new Robot();
BufferedImage bfImage = robot.createScreenCapture(new Rectangle(x, y, width, height));
File path = new File(savePath);
File file = new File(path, fileName+ "." + format);
ImageIO.write(bfImage, format, file);
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

捕捉屏幕截图后,也许,我们需要对其剪裁。主要涉及两个类CropImageFilter和FilteredImageSource,关于这两个类的介绍,看java文档把。

     /**
* BufferedImage图片剪裁
* @param srcBfImg - 被剪裁的BufferedImage
* @param x - 左上角剪裁点X坐标
* @param y - 左上角剪裁点Y坐标
* @param width - 剪裁出的图片的宽度
* @param height - 剪裁出的图片的高度
* @return 剪裁得到的BufferedImage
*/
public BufferedImage cutBufferedImage(BufferedImage srcBfImg, int x, int y, int width, int height) {
BufferedImage cutedImage = null;
CropImageFilter cropFilter = new CropImageFilter(x, y, width, height);
Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(srcBfImg.getSource(), cropFilter));
cutedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = cutedImage.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return cutedImage;
}

如果剪裁后需要保存剪裁得到的文件,使用ImageIO.write,参考上面把截图保持为文件的代码。

 

最新文章

  1. 解决SVN Upgrade working copy问题
  2. 常用的获取时间差的sql语句
  3. HDU 5033 Building --离线+单调栈
  4. java 16-8 泛型高级之通配符
  5. 30天,App创业从0到1【7.12西安站】
  6. 续【C# 以管理员方式启动Winform,进而使用管理员控制Windows Service】
  7. XP下,文件夹添加右键命令行
  8. 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据
  9. 【WEB小工具】EncodingFilter—设置全局编码
  10. Eclipse问题解决方案,不断更新
  11. Asp.net自制模板框架
  12. MVC中的文件上传-小结
  13. Php设计模式(三):行为型模式part2
  14. SVG格式转Visio的vsd格式方法,附带C#动态调用Office的Com组件方法
  15. 3G设置linux路由-iptables配置
  16. iOS------Xcode 的clang 扫描器可以检测出所有的内存泄露吗
  17. select前台转义后台取到的值为对应的文本 select同时接受list和map
  18. keras 实现人工神经网络
  19. 手动安装ettercap的过程
  20. (qsort)绝对值排序

热门文章

  1. 类的operator new与operator delete的重载【转】
  2. 踩坑录-mysql不允许远程连接(错误码:1130) Host'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server“
  3. 正则表达式的捕获组(capture group)在Java中的使用
  4. 重置网络命令win7
  5. Flink内存管理源代码解读之基础数据结构
  6. 第6章1节《MonkeyRunner源代码剖析》Monkey原理分析-事件源-事件源概览
  7. influxDB+telefraf+grafana
  8. Android 跑马灯效果与EditText冲突
  9. cocos2dX 之数据存储
  10. 混合式框架-AgileLite