Ha? Have a Talk. Menu

Viewing all items for tag java

Permalink:

JAVA屏幕截图与水印添加程序-HEHEHEScreenshot

一切都源于这个表情,后面这个表情就以不可预料的情况演化了。那时想着做一个能加苍蝇的截图工具,程序是用JAVA实现的,项目托管在GitHub https://github.com/Bigcaitou/HEHEHEScreenshot

biaoqing

talk is cheap, show you the code.

截取屏幕

    public RectD() {
            snapshot();
            setVisible(true);
            // setSize(d);//最大化窗口
             //用 JDialog 做一个无控制条的窗口,大小设置成满屏,把截取的这个满屏的图片贴到这个 JDiaglog 里
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.addMouseListener(new MouseAdapter() {// 得到鼠标箭头初始坐标
                @Override
                public void mousePressed(MouseEvent e) {
                    orgx = e.getX();
                    orgy = e.getY();
                }
            });
            //鼠标圈定区域, 对圈定的区域截屏
            this.addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    endx = e.getX();
                    endy = e.getY();
                    g = getGraphics();
                    g.drawImage(tempImage, 0, 0, RectD.this);
                    int x = Math.min(orgx, endx);
                    int y = Math.min(orgy, endy);
                    int width = Math.abs(endx - orgx) + 1;
                    int height = Math.abs(endy - orgy) + 1;
                    // 加上1,防止width或height为0
                    g.setColor(Color.BLUE);
                    g.drawRect(x - 1, y - 1, width + 1, height + 1);
                    // 减1,加1都是为了防止图片将矩形框覆盖掉
                    saveImage = image.getSubimage(x, y, width, height);
                    g.drawImage(saveImage, x, y, RectD.this);
                }
            });
            //双击鼠标截图
            this.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    int clickTimes = e.getClickCount();
                    if (clickTimes == 2) {
                        saveToFile();
                    }
                }
            });
        }
        public void snapshot() {
            try {
                Robot robot = new Robot();
                Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
                image = robot.createScreenCapture(new Rectangle(0, 0, d.width, d.height));
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }

利用Robot的createScreenCapture可以截图,但是有个问题是遇到多个屏幕,这个方法只能在默认屏幕截图。
Continue reading…

  • Thanks for leaving a comment, please keep it clean. HTML allowed is strong, code and a href.