码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-12-12 11:37:18      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   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运行时的方法得到当前屏幕截图的方法(转)

标签:style   blog   http   io   ar   color   os   sp   java   

原文地址:http://www.cnblogs.com/softidea/p/4159167.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!