使用鼠标选择要捕获的区域

8
我正在制作一个基于Java的屏幕截图应用程序,希望当您在键盘上按下一组组合键时,类似此视频中的操作可以实现:选择屏幕上的某个区域并截取该区域的屏幕截图。

如何使用鼠标选择要截图的区域?


我刚刚完成了添加自定义光标的工作。但是我不知道如何开始使其能够选择屏幕。 - Jonathan Beaudoin
除了发布这个问题,你在解决这个问题方面做了什么努力? - Jeffrey
2
我已经搜索了至少3天,寻找如何做到这一点的提示。没有找到,所以我决定发布这个问题。 - Jonathan Beaudoin
2个回答

18

从这里开始。

屏幕截图矩形

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

/** Getting a Rectangle of interest on the screen.
Requires the MotivatedEndUser API - sold separately. */
public class ScreenCaptureRectangle {

    Rectangle captureRect;

    ScreenCaptureRectangle(final BufferedImage screen) {
        final BufferedImage screenCopy = new BufferedImage(
                screen.getWidth(),
                screen.getHeight(),
                screen.getType());
        final JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
        JScrollPane screenScroll = new JScrollPane(screenLabel);

        screenScroll.setPreferredSize(new Dimension(
                (int)(screen.getWidth()/3),
                (int)(screen.getHeight()/3)));

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(screenScroll, BorderLayout.CENTER);

        final JLabel selectionLabel = new JLabel(
                "Drag a rectangle in the screen shot!");
        panel.add(selectionLabel, BorderLayout.SOUTH);

        repaint(screen, screenCopy);
        screenLabel.repaint();

        screenLabel.addMouseMotionListener(new MouseMotionAdapter() {

            Point start = new Point();

            @Override
            public void mouseMoved(MouseEvent me) {
                start = me.getPoint();
                repaint(screen, screenCopy);
                selectionLabel.setText("Start Point: " + start);
                screenLabel.repaint();
            }

            @Override
            public void mouseDragged(MouseEvent me) {
                Point end = me.getPoint();
                captureRect = new Rectangle(start,
                        new Dimension(end.x-start.x, end.y-start.y));
                repaint(screen, screenCopy);
                screenLabel.repaint();
                selectionLabel.setText("Rectangle: " + captureRect);
            }
        });

        JOptionPane.showMessageDialog(null, panel);

        System.out.println("Rectangle of interest: " + captureRect);
    }

    public void repaint(BufferedImage orig, BufferedImage copy) {
        Graphics2D g = copy.createGraphics();
        g.drawImage(orig,0,0, null);
        if (captureRect!=null) {
            g.setColor(Color.RED);
            g.draw(captureRect);
            g.setColor(new Color(255,255,255,150));
            g.fill(captureRect);
        }
        g.dispose();
    }

    public static void main(String[] args) throws Exception {
        Robot robot = new Robot();
        final Dimension screenSize = Toolkit.getDefaultToolkit().
                getScreenSize();
        final BufferedImage screen = robot.createScreenCapture(
                new Rectangle(screenSize));

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ScreenCaptureRectangle(screen);
            }
        });
    }
}

哇,太感谢了!非常有帮助。只是想知道这是你做的吗? - Jonathan Beaudoin
只有在捕获正在运行的Java应用程序的屏幕截图时,此方法才有效。您将无法截取用户桌面上运行的其他应用程序。我认为您想要一个通用的屏幕截图应用程序,这意味着您不能使用Swing。 - chubbsondubs
@chubbard:“这只有在您捕获正在运行的Java应用程序的屏幕截图时才能起作用。” 在截屏之前,您的应用程序需要被隐藏。我将在回答那个(单独的)问题时讨论这个问题。 - Andrew Thompson
哇!从没想过会这么简单...完美的答案! - Sauer
感谢这个非常有用的片段。它似乎仅在从左上到右下拖动时起作用。我更改了mouseDragged方法,使其可以在所有方向上工作。很容易做到。如果有兴趣,我可以发布代码。此外,传递给构造函数的任何bufferedImage都可以工作-它不必是屏幕截图。那只是主方法的设置方式。 - torwalker
显示剩余4条评论

1

我知道如何截取并保存屏幕截图。我想知道如何使用鼠标选择特定区域。 - Jonathan Beaudoin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接