首页 > 代码库 > 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运行时的方法得到当前屏幕截图的方法(转)