截图小程序

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

下面是最简单的一个小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对象screenshot
                        BufferedImage 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();
        }
}