1. 程式人生 > >Java指定螢幕區域截圖

Java指定螢幕區域截圖

擷取指定螢幕區域的螢幕,並儲存到指定資料夾中,Demo如下:

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

public class Screenshot {

	/**
	 * 截圖
	 * 
	 * @param filePath
	 *            截圖儲存資料夾路徑
	 * @param fileName
	 *            截圖檔名稱
	 * @throws Exception
	 */
	public static void captureScreen(String filePath, String fileName) throws Exception {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle screenRectangle = new Rectangle(screenSize);
		Robot robot = new Robot();
		BufferedImage image = robot.createScreenCapture(screenRectangle);
		// 截圖儲存的路徑
		File screenFile = new File(filePath + fileName);
		// 如果資料夾路徑不存在,則建立
		if (!screenFile.getParentFile().exists()) {
			screenFile.getParentFile().mkdirs();
		}

		// 指定螢幕區域,引數為截圖左上角座標(100,100)+右下角座標(500,500)
		BufferedImage subimage = image.getSubimage(100, 100, 500, 500);
		ImageIO.write(subimage, "png", screenFile);

	}

	public static void main(String[] args) throws Exception {
		Date now = new Date();
		SimpleDateFormat sdfPath = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdfName = new SimpleDateFormat("yyyyMMddHHmmss");
		String path = sdfPath.format(now);
		String name = sdfName.format(now);
		captureScreen("D:" + File.separator + path + File.separator, name + ".png");
	}
}