截图小程序

来源:互联网 发布:淘宝卖家说给补偿5元 编辑:程序博客网 时间:2024/06/05 17:56

最近的项目中有一个需求开发,情况是这个样子的:将页面上的某一个部分拿下来以后。以图片的形式保存,在网上搜了一下,采取的方式大致分为三种。

下面是最简单的一个小demo

 

public class GuiCamera {/** * 默认的文件前缀为GuiCamera,文件格式为PNG格式 The default construct will use the default * Image file surname "GuiCamera", and default image format "png" */private final static String FORMAT_PNG = "png";private final static String FORMAT_JPG = "jpg";private static String filePath = "";// 存放路径private static String fileName = "GuiCamera_"; // 文件的前缀private static int serialNum = 0;private static String imageFormat = GuiCamera.FORMAT_PNG; // 图像文件的格式private static Dimension d = Toolkit.getDefaultToolkit().getScreenSize();/** * 对屏幕进行拍照 snapShot the Gui once */public static void snapShot() throws Exception {try {GuiCamera.filePath = "D://";// 拷贝屏幕到一个BufferedImage对象screenshotBufferedImage screenshot = (new Robot()).createScreenCapture(new Rectangle(1087, 71, 135,21));serialNum++;// 根据文件前缀变量和文件格式变量,自动生成文件名Calendar c = Calendar.getInstance();SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒");String name = filePath + fileName + sdf.format(c.getTime()) + "." + imageFormat;File f = new File(name);// 将screenshot对象写入图像文件ImageIO.write(screenshot, imageFormat, f);} catch (Exception e) {throw e;}}public static void main(String[] args) throws Exception {GuiCamera.filePath = "D://";GuiCamera.snapShot();}}


 

0 0
原创粉丝点击