`

java实现截图和保存

    博客分类:
  • java
阅读更多
BufferedImage是个好类,和ImageIO和Graphic可以对图片进行很多处理;至于把BufferedImage打到窗口上就是八仙过海,各显神通罗。
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtil{
    
    public static BufferedImage captureScreen(Rectangle screenRect)
            throws AWTException{
        Robot robot = new Robot();
        return robot.createScreenCapture(screenRect);
    }
    
    public static void saveImage(BufferedImage srcImage, String saveImagePath) throws IOException{
        File file = new File(saveImagePath);
        String suffix = saveImagePath.substring(saveImagePath.lastIndexOf('.')+1);
        ImageIO.write(srcImage, suffix, file);
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics