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();
	}