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,参考上面把截图保持为文件的代码。

 

Java屏幕截图及剪裁的更多相关文章

  1. Java屏幕截图工具 捕获屏幕

    原文:http://www.open-open.com/code/view/1420037709781 import java.awt.BorderLayout; import java.awt.Co ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. JAVA B/S系统实现客户端屏幕截图,Java版的QQ截图

    简介:本方法首先需要抓到屏幕的整个图象,将图象显示在一个JFrame中,再将JFrame全屏显示,这样就模拟出了一个桌面,Java也就可以获得鼠标的作用区域从而实现桌面中的小范围截屏.Java实现QQ ...

  4. java 利用java运行时的方法得到当前屏幕截图的方法(转)

    将截屏图片保存到本地路径: package com.test; import java.awt.AWTException; import java.awt.Dimension; import java ...

  5. selenium3(java)之屏幕截图操作

    selenium提供了截图的功能,分别是接口是TakesScreenshot和类RemoteWebDriver.该功能是在运行测试用例的过程中,需要验证某个元素的状态或者显示的数值时,可以将屏幕截取下 ...

  6. java+selenium-3.9.1多线程 打开连接截取屏幕截图

    废话不多说上代码:(我是用的chrome举得例子哈) 第一步,需要chromedriver.exe 目的和调起chrome 浏览器打开连接,chromedriver.exe的版本与你的chrome版本 ...

  7. 通过java进行电脑屏幕截图

    package image; import java.awt.Desktop; import java.awt.Dimension; import java.awt.Rectangle; import ...

  8. 【搬砖】安卓入门(1)- Java开发入门

    01.01_计算机基础知识(计算机概述)(了解) A:什么是计算机?计算机在生活中的应用举例 计算机(Computer)全称:电子计算机,俗称电脑.是一种能够按照程序运行,自动.高速处理海量数据的现代 ...

  9. java Fn键

    需求分析 我想开机禁用触摸板. 方案设计 安装驱动:比较麻烦,驱动也不一定支持开机禁用触摸板. 编程实现,让一段代码开机禁用触摸板 编程实现也分好几种方法: 使用windows API禁用触摸板,这需 ...

随机推荐

  1. guava cache学习

    Guava Cache与ConcurrentMap很相似,但也不完全一样.最基本的区别是ConcurrentMap会一直保存所有添加的元素,直到显式地移除.相对地,Guava Cache为了限制内存占 ...

  2. top命令查看线程信息和jstack使用介绍

    top -Hp pid可以查看某个进程的线程信息 -H 显示线程信息,-p指定pid jstack 线程ID 可以查看某个线程的堆栈情况,特别对于hung挂死的线程,可以使用选项-F强制打印dump信 ...

  3. Setting up Storm and Running Your First Topology

    http://www.haroldnguyen.com/blog/2015/01/setting-up-storm-and-running-your-first-topology/ --------- ...

  4. 008 frame relay

    Router>en Router#config t Enter configuration commands, one per line.  End with CNTL/Z. Router(co ...

  5. hdu1025 Constructing Roads In JGShining's Kingdom(二分+dp)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem ...

  6. PAT Rational Arithmetic (20)

    题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...

  7. 展开阅读全文 js 爬虫操作

    from selenium import webdriver import time import random from bs4 import * browser = webdriver.Chrom ...

  8. POJ 2629:Common permutation

    Common permutation Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5510   Accepted: 168 ...

  9. luence全文检索(简介)

    刚开始做全文检索也是找了很多资料但是网上的都不是很齐全luence是个很不多的工具 Lucene4.0的官网文档:http://lucene.apache.org/core/4_0_0/core/ov ...

  10. 【USACO 2010FEB】 slowdown

    [题目链接] 点击打开链接 [算法] dfs序 + 线段树 树链剖分同样可以解决这个问题 [代码] #include<bits/stdc++.h> using namespace std; ...