Java屏幕截图App - 如何?

时间:2014-10-23 22:32:57

标签: java image pdf screenshot bufferedimage

我不是母语为英语的人,所以首先抱歉语法。

我想要一个应用程序来捕获屏幕的选定区域并保存它。我做了一些研究,我在下面做了代码。

我的问题是:

1 - 如何在此应用中打开pdf文件? (我尝试使用一种方法,但它没有用。我不确切地知道将它放在代码上的位置)

2 - 如何将所选区域保存在新文件中? (图像文件:JPEG,JPG,png)

3 - [复杂部分]现在,仅代码"保存"每次选择一个区域。我想捕获屏幕的很多部分并将其保存在同一个图像文件中。一个在另一个旁边。我怎样才能做到这一点 ?

Java代码:

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

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

1 个答案:

答案 0 :(得分:1)

  

1 - 如何在此应用中打开pdf文件? (我尝试使用一种方法,但它没有用。我不确切地知道将它放在代码上的位置)

查看How to Integrate with the Desktop Class

  

2 - 如何将所选区域保存在新文件中? (图像文件:JPEG,JPG,png)

查看Writing/Saving an Image

  

3 - [复杂部分]现在,代码每次只“保存”一个选定区域。我想捕获屏幕的很多部分并将其保存在同一个图像文件中。一个在另一个旁边。我怎么能这样做?

正如你所说,是一个更复杂的问题。您必须修改代码,以便在panel中显示JOptionPane,而不是在JFrame中显示,然后您需要能够监控{{1}或者提供某种动作,可以是工具栏或菜单选项,允许用户保存选择。

有关详细信息,请查看How to Use MenusHow to Use Buttons, Check Boxes, and Radio ButtonsHow to Write an Action ListenersHow to Use Tool Bars

作为旁注,代码只允许您捕获单个屏幕,您可能会考虑使用Drawing a bounding rectangle to select what area to record这样的内容来捕获整个虚拟桌面(多个屏幕)