[置顶] 添加屏幕截图的一个辅助类ScreenDumpHelper

package cn.iamsese.prj.db.helper;

import java.awt.AWTException;
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.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import javax.imageio.ImageIO;

interface ImageType {
	public final static String JPG = "JPG" ;
	public final static String PNG = "PNG" ;
	public final static String GIF = "GIF" ;
}

/**
 * 屏幕截取 类 
 * 参考 http://www.iteye.com/topic/289052 实现
 * cn.iamsese.prj.db.helper
 * Author: vb2005xu [JAVA菜鸟]
 */
public class ScreenDumpHelper {
	/** ScreenDumpHelper 静态对象*/
	private static ScreenDumpHelper defaultScreenDumpHelper ;
	/**
	 * 返回 ScreenDumpHelper 类的 单态实例
	 * @return ScreenDumpHelper
	 */
	public static ScreenDumpHelper getDefaultScreenDumpHelper(){
		if (defaultScreenDumpHelper == null)
			return new ScreenDumpHelper();
		return defaultScreenDumpHelper ;
	} 
	/**
	 * 构造函数中设置 截屏区域的默认值 -- 缺省为全屏
	 */
	public ScreenDumpHelper(){
		Dimension dime = Toolkit.getDefaultToolkit().getScreenSize();
		this.setScreenArea(new Rectangle(dime));
	}
	
	/**
	 *设置截屏的范围
	 */
	private Rectangle screenArea ;
	public Rectangle getScreenArea() {
		return this.screenArea;
	}
	public void setScreenArea(Rectangle screenArea) {
		this.screenArea = screenArea;
	}
	public void setScreenArea(int x, int y , int width, int height) {
		this.screenArea = new Rectangle(x,y,width,height);
	}
	
	/**
	 * 返回一个 BufferedImage
	 * @return
	 * @throws AWTException
	 */
	private BufferedImage getBufferedImage() throws AWTException{
		BufferedImage imgBuf = null ;;
		imgBuf = ( new Robot() ).createScreenCapture(this.getScreenArea());		
		return imgBuf ;
	}
	
	/**
	 * 将 图像数据写到 输出流中去,方便再处理
	 * @param fileFormat
	 * @param output
	 * @return
	 */
	public boolean saveToOutputStream(String fileFormat,OutputStream output){
		try {
			ImageIO.write(this.getBufferedImage(), fileFormat, output) ;
		} catch (AWTException e) {
			return false ;
			//e.printStackTrace();
		} catch (IOException e) {
			return false ;
			//e.printStackTrace();
		}
		return true ;
	}
	
	/**
	 * 根据文件格式 返回随机文件名称
	 * @param fileFormat
	 * @return
	 */
	public String getRandomFileName(String fileFormat){
		return "screenDump_" + (new Date()).getTime() + "." + fileFormat ;
	}
	
	/**
	 * 抓取 指定区域的截图 到指定格式的文件中 -- 这个函数是核心,所有的都是围绕它来展开的
	 *  * 图片的编码并不是以后缀名来判断: 
	 * 比如s.jpg 如果其采用png编码,那么这个图片就是png格式的	 
	 * @param fileName
	 * @param fileFormat
	 * @return boolean
	 */
	public boolean saveToFile(String fileName,String fileFormat){
		if (fileName == null)
			fileName = getRandomFileName(fileFormat) ;		
		try {
			FileOutputStream fos = new FileOutputStream(fileName);
			this.saveToOutputStream(fileFormat, fos);
		} catch (FileNotFoundException e) {
			System.out.println("非常规文件或不能创建抑或覆盖此文件: " + fileName);
			e.printStackTrace();
		}
		return true ;
	}
	
	/**
	 * 抓取 指定 Rectangle 区域的截图 到指定格式的文件中
	 * @param fileName
	 * @param fileFormat
	 * @param screenArea
	 * @return
	 */
	public boolean saveToFile(String fileName,String fileFormat,Rectangle screenArea){
		this.setScreenArea(screenArea);
		return this.saveToFile(fileName,fileFormat);
	}
	/**
	 * 抓取 指定区域的截图 到指定格式的文件中
	 * @param fileName
	 * @param fileFormat
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 */
	public boolean saveToFile(String fileName,String fileFormat,int x, int y , int width, int height){
		this.setScreenArea(x,y,width,height);
		return this.saveToFile(fileName, fileFormat);
	}	
	
	/**
	 * 将截图使用 JPG 编码
	 * @param fileName
	 */
	public void saveToJPG(String fileName){
		this.saveToFile(fileName, ImageType.JPG);
	}
	public void saveToJPG(String fileName,Rectangle screenArea){
		this.saveToFile(fileName, ImageType.JPG,screenArea);
	}
	public void saveToJPG(String fileName,int x, int y , int width, int height){
		this.saveToFile(fileName, ImageType.JPG,x,y,width,height);
	}
	
	/**
	 * 将截图使用 PNG 编码
	 * @param fileName
	 */
	public void saveToPNG(String fileName){
		this.saveToFile(fileName, ImageType.PNG);
	}
	public void saveToPNG(String fileName,Rectangle screenArea){
		this.saveToFile(fileName, ImageType.PNG,screenArea);
	}
	public void saveToPNG(String fileName,int x, int y , int width, int height){
		this.saveToFile(fileName, ImageType.PNG,x,y,width,height);
	}
	
	public void saveToGIF(String fileName){
		throw new UnsupportedOperationException("不支持保存到GIF文件");
		//this.saveToFile(fileName, ImageType.GIF);
	}	


	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		for(int i = 0 ; i < 5 ; i ++)
			ScreenDumpHelper.getDefaultScreenDumpHelper().saveToJPG(null,i*150,i*150,400,300);
	}
	
	
	

}

 

 

截取图片如下:

  • 大小: 27.6 KB
  • 大小: 25.4 KB
  • 大小: 29.2 KB
  • 大小: 26.3 KB
  • 大小: 14 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值