论坛首页 入门技术论坛

通过Robot截取屏幕事例

浏览 1839 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-05-16  
通过Robot截取屏幕简单事例

package com.wyh.io;

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 javax.imageio.ImageIO;

public class TestRobot implements Runnable {

	// 文件名
	private String fileName;
	// 文件格式
	private String imageFormat;
	//文件名序号
	private static int incrmentNum = 1;

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getImageFormat() {
		return imageFormat;
	}

	public void setImageFormat(String imageFormat) {
		this.imageFormat = imageFormat;
	}

	public TestRobot() {

	}

	public TestRobot(String s, String format) {
		fileName = s;
		imageFormat = format;
	}

	// 截取桌面图象
	public void startSnap(int i) {
		try {

			Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
			// 通Robot获当前屏幕对象
			BufferedImage screenshot = (new Robot())
					.createScreenCapture(new Rectangle(0, 0, (int) dimension
							.getWidth(), (int) dimension.getHeight()));

			String name = fileName + incrmentNum + "." + imageFormat;
			ImageIO.write(screenshot, imageFormat, new File(name));
			incrmentNum++;

			System.out.println("已经 保存线程 [" + i + "]" + "  " + name);
		} catch (Exception ex) {
			System.out.println(ex);
		}
	}

	public void run() {
		System.out.println("开始截取......");
		for (int i = 1;; i++) {
			this.startSnap(i);
			try {
				Thread.sleep(3800);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// test in main
	public static void main(String[] a) {
		TestRobot testRobot = new TestRobot("D:\\javaApp\\TestApp\\test", "jpg");
		Thread thread = new Thread(testRobot);
		thread.start();
	}
}

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics