【问题标题】:Select an area to capture using the mouse使用鼠标选择要捕获的区域
【发布时间】:2012-06-15 21:58:57
【问题描述】:

我正在制作一个基于 Java 的屏幕截图应用程序,并且我想制作它,以便当您按下键盘上的组合键时,this video 之类的东西会发生在您选择屏幕上的区域和区域时,它需要所选区域的屏幕截图。

如何用鼠标选择要捕捉的区域?

【问题讨论】:

  • 我刚刚添加完自定义光标。我不知道如何开始让它能够选择屏幕。
  • 除了发布这个问题之外,您为解决这个问题做了哪些努力?
  • 我一直在寻找至少 3 天的时间来寻找有关如何执行此操作的某种提示。还没有找到,所以我决定发布这个问题。

标签: java image screenshot selected area


【解决方案1】:

从这样的开始。

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);
            }
        });
    }
}

【讨论】:

  • 哇,非常感谢!很有帮助。只是想知道这是你做的吗?
  • 这仅在您捕获正在运行的 Java 应用程序的屏幕截图时才有效。您将无法截屏用户桌面上运行的其他应用程序。我以为你想要一个通用的屏幕截图应用程序,这意味着你不能使用 Swing。
  • @chubbard “这仅在您正在捕获正在运行的 Java 应用程序的屏幕截图时才有效” 在截取屏幕截图之前,您的应用程序需要隐藏。针对那个(单独的)问题,我会在单独的答案中讨论这个问题。
  • 哇!没想到会这么简单……完美的答案!
  • @Andrew Thompson .. 它工作正常.. 如何使用我们自己的预定义图像而不是捕获 Java 应用程序的屏幕截图。
【解决方案2】:

【讨论】:

  • 我知道如何拍摄和保存屏幕截图。我不知道如何用鼠标选择特定区域。
猜你喜欢
  • 1970-01-01
  • 2014-09-04
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 2012-08-16
相关资源
最近更新 更多