首页 > 代码库 > java 利用java运行时的方法得到当前屏幕截图的方法(转)
java 利用java运行时的方法得到当前屏幕截图的方法(转)
将截屏图片保存到本地路径:
package com.test;import java.awt.AWTException;import java.awt.Dimension;import java.awt.HeadlessException;import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;public class MainRun { /** * @param args */ public static void main(String[] args) { try { Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); BufferedImage screenshot = (new Robot()).createScreenCapture(new Rectangle(0, 0,(int)dimension.getWidth(),(int)dimension.getHeight())); File file = new File("C:/screen.jpg"); ImageIO.write(screenshot, "jpg", file); } catch (HeadlessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
web版,将显示实时的图片作为流返回到页面:
/** * 得到当前屏幕截图 * * @param request * @param response */ private void getScreenImg(HttpServletRequest request, HttpServletResponse response) throws Exception { Thread.sleep(3000); response.setContentType("image/jpg"); ServletOutputStream sos = response.getOutputStream(); // 禁止页面缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // 创建内存图象并获得其图形上下文 Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); BufferedImage screenshot = (new Robot()) .createScreenCapture(new Rectangle(0, 0, (int) dimension .getWidth(), (int) dimension.getHeight())); // 将图像输出到客户端 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(screenshot, "jpg", bos); byte[] buf = bos.toByteArray(); response.setContentLength(buf.length); sos.write(buf); bos.close(); sos.close(); }
http://blog.csdn.net/songylwq/article/details/7714603
java 利用java运行时的方法得到当前屏幕截图的方法(转)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。