gpt4 book ai didi

Java 屏幕截图应用程序 - 如何?

转载 作者:行者123 更新时间:2023-12-01 12:22:56 24 4
gpt4 key购买 nike

我的母语不是英语,所以首先对语法感到抱歉。

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

我的问题是:

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

看看How to Integrate with the Desktop Class

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

看看Writing/Saving an Image

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 ?

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

看看How to Use Menus , How to Use Buttons, Check Boxes, and Radio Buttons , How to Write an Action ListenersHow to Use Tool Bars了解更多详情。

顺便说一句,代码只允许您捕获单个屏幕,您可能会考虑类似 Drawing a bounding rectangle to select what area to record 的内容。这将允许您捕获整个虚拟桌面(多个屏幕)

关于Java 屏幕截图应用程序 - 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26538601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com