Java屏幕捕获应用程序-如何? [英] Java screen capture App - How to?

查看:125
本文介绍了Java屏幕捕获应用程序-如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是英语为母语的人,因此,首先对语法感到抱歉.

i am not a native english speaker so, firstly sorry for the grammar.

我想做一个捕获屏幕上选定区域并保存它的应用程序.我做了一些研究,并在下面进行了编码.

I want to do an app that capture a selected area of a screen and save it. I did a few research and i did the code down below.

我的问题是:

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

1 - How can i open a pdf file in this app ? (i tried use a method but it didnt work. I dont know exactly where to put it on the code)

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

2 - How can i save the selected area in a new file ? (a image file : JPEG, JPG,png)

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

3 - [the complex part] right now, the code only "save" one selected area each time. I want to capture a lot of parts of screen and save this in the same image file. one beside the other. How can i do this ?

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-如何在此应用中打开pdf文件? (我尝试使用一种方法,但是没有用.我不知道确切地将其放在代码上的位置)

1 - How can i open a pdf file in this app ? (i tried use a method but it didnt work. I dont know exactly where to put it on the code)

看看如何与桌面类集成 a>

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

2 - How can i save the selected area in a new file ? (a image file : JPEG, JPG,png)

看看编写/保存图像

3-[复杂部分]现在,该代码每次仅保存"一个选定的区域.我想捕获屏幕的很多部分,并将其保存在同一图像文件中.一个在另一个旁边.我该怎么办?

3 - [the complex part] right now, the code only "save" one selected area each time. I want to capture a lot of parts of screen and save this in the same image file. one beside the other. How can i do this ?

正如您所说,这是一个更为复杂的问题.您将需要修改代码,以便与其在JOptionPane中显示panel而不是在JFrame中显示它,然后您需要能够监视mouseReleaseEvent或提供某种操作,可以是工具栏或菜单选项,允许用户保存选择.

Is, as you say, a much more complex question. You will have to modify the code so that instead of displaying the panel in a JOptionPane, it shows it within a JFrame, you then need to be able to either monitor the mouseReleaseEvent or provide some kind of action, may be a toolbar or menu option, that allows the user to save the selection.

看看如何使用菜单如何使用按钮,复选框和单选按钮如何编写动作监听器

Have a look at How to Use Menus, How to Use Buttons, Check Boxes, and Radio Buttons, How to Write an Action Listeners and How to Use Tool Bars for more details.

请注意,该代码仅允许您捕获单个屏幕,您可能会考虑类似

As a side note, the code will only allow you to capture a single screen, you might consider something like Drawing a bounding rectangle to select what area to record which will allow you to capture the entire virtual desktop (multiple screeens)

这篇关于Java屏幕捕获应用程序-如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆