Java屏幕截图及剪裁 现成的工具类ImageUtils

Java标准API中有个Robot类,该类可以实现屏幕截图,模拟鼠标键盘操作这些功能。这里只展示其屏幕截图。截图的关键方法createScreenCapture(Rectangle rect) ,该方法需要一个Rectangle对象,Rectangle就是定义屏幕的一块矩形区域,构造Rectangle也相当容易:new Rectangle(int x, int y, int width, int height),四个参数分别是矩形左上角x坐标,矩形左上角y坐标,矩形宽度,矩形高度。截图方法返回BufferedImage对象,开袋即食。

 

package com.cma.andy.util;

import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 截图工具类
 * 
 * @author andy
 */
public class ImageUtils {

	public static void main(String[] args) throws Exception {
		
		// 截全屏幕保存成文件
		fullScreenShotAsFile("D:\\Download\\","sceenshot","jpg");
		
		File imageFile = new File("D:\\Download\\sceenshot.jpg");
		 // 自动打开图片
		if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
			Desktop.getDesktop().open(imageFile);
		}

	}
	
	/**
     * 指定屏幕区域截图,返回截图的BufferedImage对象
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public BufferedImage getScreenShot(int x, int y, int width, int height) {
        BufferedImage bfImage = null;
        try {
            Robot robot = new Robot();
            bfImage = robot.createScreenCapture(new Rectangle(x, y, width, height));
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return bfImage;
    }

    /**
     * 全屏幕区域截图,返回截图的BufferedImage对象
     * @return
     */
    public BufferedImage getFullScreenShot() {
        BufferedImage bfImage = null;
        try {
            Robot robot = new Robot();
            // 获取当前显示器屏幕大小
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            bfImage = robot.createScreenCapture(new Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return bfImage;
    }
    
    /**
     * 指定屏幕区域截图,保存到指定目录
     * @param x
     * @param y
     * @param width
     * @param height
     * @param savePath - 文件保存路径
     * @param fileName - 文件保存名称
     * @param format - 文件格式
     */
    public void screenShotAsFile(int x, int y, int width, int height, String savePath, String fileName, String format) {
        try {
            Robot robot = new Robot();
            BufferedImage bfImage = robot.createScreenCapture(new Rectangle(x, y, width, height));
            File path = new File(savePath);
            File file = new File(path, fileName+ "." + format);
            ImageIO.write(bfImage, format, file);
        } catch (AWTException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 全屏幕区域截图,保存到指定目录
     * @param savePath - 文件保存路径
     * @param fileName - 文件保存名称
     * @param format - 文件格式
     */
    public static void fullScreenShotAsFile (String savePath,String fileName,String format) {
       try {
    	   Robot robot = new Robot();
           // 获取当前显示器屏幕大小
           Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
           //拷贝屏幕到一个BufferedImage对象screenshot
           BufferedImage bfImage = robot.createScreenCapture(new Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight()));
           File path = new File(savePath);
           File file = new File(path, fileName+ "." + format);
           ImageIO.write(bfImage, format, file);
       } catch (AWTException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

    
    /**
     * BufferedImage图片剪裁
     * @param srcBfImg - 被剪裁的BufferedImage
     * @param x - 左上角剪裁点X坐标
     * @param y - 左上角剪裁点Y坐标
     * @param width - 剪裁出的图片的宽度
     * @param height - 剪裁出的图片的高度
     * @return 剪裁得到的BufferedImage
     */
    public BufferedImage cutBufferedImage(BufferedImage srcBfImg, int x, int y, int width, int height) {
        BufferedImage cutedImage = null;
        CropImageFilter cropFilter = new CropImageFilter(x, y, width, height);
        Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(srcBfImg.getSource(), cropFilter));
        cutedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = cutedImage.getGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();
        return cutedImage;
    }
}

 

上一篇:Servlet验证码的实现


下一篇:Java创建图片并绘图