Java截图程序


Java AWT(抽象窗口工具包)是一个多功能的包,包含创建用户界面和绘制图形图像所需的所有类。按钮和滚动条之类的实体在AWT术语中称为组件。Component类是所有AWT组件的根。当用户与某些组件交互时,会触发事件。AWTEvent类及其子类表示AWT组件能够触发的事件。在AWTEvent中,解释了AWT事件模型。

容器是一个组件,它也可以包含其他组件和容器。容器的另一种选择是布局管理器,它控制组件在容器内的视觉排列方式。AWT包包含许多布局管理器类以及用于开发您自己的布局管理器的接口。查看LayoutManager和Container以获取更多信息。

值以整数形式存储,因此每个组件对象都有最大大小和位置。平台也可能对最大大小和地理区域施加进一步的限制。平台决定精确的最大值。这些最大值无法更改,无论是在Java代码中还是在本地代码中。组件布局也受到这些限制的约束。如果组件的限制超过平台限制,则无法有效地在容器对象内组织组件对象。特定轴上任何对象的位置和大小共同决定了该对象的限制。

算法

无需构建GUI即可捕获屏幕截图

  • 导入所有必要的Java包。

  • 创建一个“Robot”对象来捕获屏幕截图。

  • 创建一个“Rectangle”对象来指定要捕获的屏幕尺寸。

  • 将屏幕捕获为“BufferedImage”对象。

  • 打印正在捕获屏幕截图的消息。

通过构建GUI并保存到特定路径来捕获屏幕截图

  • 导入所有必要的库。

  • 创建一个“JFrame”对象来容纳GUI组件。

  • 创建一个“JButton”对象来点击并捕获屏幕截图。

  • 创建一个“JFileChooser”对象来容纳“JButton”并将其添加到“JFrame”。

  • 向“JButton”添加“ActionListener”,当单击按钮时,该侦听器捕获屏幕截图并将其保存到特定文件。

  • 使用“Robot”对象将屏幕捕获为“BufferImage”对象。

  • 显示“保存文件”对话框,允许用户选择保存位置和文件名。

  • 使用“ImageIO.write()”方法将“BufferedImage”对象保存到所选文件。

  • 打印屏幕截图已保存的消息。

  • 指定“JFrame”的尺寸和可见性。

方法

  • 无需构建GUI即可捕获屏幕截图。

  • 通过构建GUI并保存到特定路径来捕获屏幕截图。

无需创建GUI即可捕获屏幕截图

示例

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class ScreenCapture {
   public static void main(String[] args) throws Exception {
      // Create a Robot object to capture the screen
      Robot robot = new Robot();

      // Create a Rectangle object to define the area of the screen to capture
      Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

      // Use the robot.createScreenCapture() method to capture the screen as a BufferedImage object
      BufferedImage image = robot.createScreenCapture(rectangle);

      // Save the BufferedImage object to a file using the ImageIO.write() method
      File file = new File("screenshot.png");
      ImageIO.write(image, "png", file);

      // Print a message indicating that the screenshot has been saved
      System.out.println("Screenshot saved to screenshot.png");
   }
}

输出

Screenshot saved to screenshot.png

Java.awt和Javax.imageIO包用于捕获和保存屏幕快照。它在尝试将屏幕捕获为BufferedImage对象之前,首先创建一个Robot对象。要捕获的屏幕部分在稍后创建的Rectangle对象中定义为整个屏幕,该屏幕是使用Toolkit.getDefaultToolkit()函数的getScreenSize收集的。

然后,代码使用robot对象将屏幕捕获为BufferedImage对象。名为createScreenCapture(rectangle)的函数。此项将替换最新的屏幕截图。

在该步骤之后,我们通过ImageIO.write来保存BufferedImage的特定实例——具体使用其参数(“png”和file)。这需要使用两个不同的对象:第一个是我们称为“image”的BufferedImage实例,第二个是称为“file”的对象,它指定了输出将保存到的位置。在本例中,我们选择了PNG格式进行存储。

程序生成的最终消息指出屏幕截图已保存到screenshot.png文件。

通过创建GUI并保存到特定路径来捕获屏幕截图

示例

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ScreenshotCapture {
   public static void main(String[] args) {
      // Create a JFrame object
      JFrame frame = new JFrame("Screenshot Capture");

      // Create a JButton object
      JButton captureButton = new JButton("Capture");

      // Create a JFileChooser object
      JFileChooser fileChooser = new JFileChooser();

      // Create a JPanel object
      JPanel panel = new JPanel();

      // Add the JButton and JPanel objects to the JFrame
      panel.add(captureButton);
      frame.add(panel, BorderLayout.CENTER);

      // Add an ActionListener to the JButton to capture the screenshot and save it to a file
      captureButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            try {
               // Create a Robot object to capture the screen
               Robot robot = new Robot();
               
               // Create a Rectangle object to define the area of the screen to capture
               Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

               // Use the robot.createScreenCapture() method to capture the screen as a BufferedImage object
               BufferedImage image = robot.createScreenCapture(rectangle);

               // Show the save file dialog to allow the user to select the save location and file name
               int result = fileChooser.showSaveDialog(frame);
               if (result == JFileChooser.APPROVE_OPTION) {
                  // Get the selected file and save the BufferedImage object to it using the ImageIO.write() method
                  File file = fileChooser.getSelectedFile();
                  ImageIO.write(image, "png", file);
                  
                  // Print a message indicating that the screenshot has been saved
                  System.out.println("Screenshot saved to " + file.getAbsolutePath());
               }
            } catch (AWTException ex) {
               ex.printStackTrace();
            } catch (Exception ex) {
               ex.printStackTrace();
            }
         }
      });

      // Set the size and visibility of the JFrame
      frame.setSize(new Dimension(300, 100));
      frame.setVisible(true);
   }
}

输出

为了记录屏幕、显示保存文件对话框并保存图像,程序使用了Java.awt、Javax.swing和Javax.imageio包。它首先创建一个名为“Capture”的JButton对象和一个JFrame对象。此外,它还创建一个JFileChooser对象,以允许用户选择保存快照的位置和名称。

然后,程序将JButton和JPanel对象添加到JFrame。此外,它还向JButton对象添加了一个ActionListener,以便每当单击按钮时,都会拍摄当前屏幕的快照并将其保存到文件中。为了将屏幕捕获为BufferedImage对象,ActionListener首先创建一个Robot对象。然后,使用JFileChooser返回的fileChooser.showSaveDialog(frame)函数显示保存文件对话框。如果用户选择文件并在对话框中单击“保存”,则显示APPROVE_OPTION。

当用户选择文件时,程序使用fileChooser获取所选文件。使用ImageIO.write(image, "png", file)函数和getSelectedFile()将BufferedImage对象保存到文件。参数“png”指定图像的格式为PNG。

屏幕截图已保存到file.getAbsolutePath()提供的文件路径,程序以屏幕上的通知结束。

结论

总之,可以使用Robot类捕获屏幕的BufferedImage对象,并使用ImageIO.write()方法将图像保存到文件,从而可以在Java中捕获屏幕截图。用户还可以使用使用Swing组件(例如JFrame、JButton和JFileChooser)构建的GUI应用程序来选择保存位置和文件名。通过向JButton附加ActionListener,可以捕获屏幕截图并将其保存到所选文件。总的来说,Java提供了一种简单且适应性强的用于捕获和保存屏幕截图的方法,适用于各种用途。

更新于:2023年8月1日

2K+ 浏览量

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.