Java开发截图小程序主要分为两种:基于Java的桌面应用程序和基于Java的Web应用程序。
一、基于Java的桌面应用程序
1. Java AWT
Java AWT(Abstract Window Toolkit)是Java提供的一套基础界面库,可以用于控制图形、按钮、文本和其他界面元素。它也可以用来截取屏幕和窗口,虽然它的功能有限,但是对于一些小需求来说已经足够。以下是使用Java AWT实现截图的简单示例代码:
```java
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ScreenShot {
public static void main(String[] args) {
String fileName = "screenshot.jpg";
try {
Robot robot = new Robot();
BufferedImage screenshot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenshot, "jpg", new File(fileName));
} catch (AWTException | IOException ex) {
System.err.println(ex);
}
}
}
```
2. Java Swing
Java Swing是Java提供的一套用户界面组件库,它可以为GUI编程提供更多的功能。在Swing中,可以使用Java AWT的截图功能来实现截图。以下是使用Java Swing实现截图的示例代码:
```java
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;
public class ScreenShot extends JFrame {
private final JFileChooser fileChooser;
public ScreenShot() {
this.setSize(100, 100);
this.setVisible(true);
this.fileChooser = new JFileChooser();
this.fileChooser.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));
this.fileChooser.setMultiSelectionEnabled(false);
this.fileChooser.setAcceptAllFileFilterUsed(false);
this.fileChooser.setSelectedFile(new File("screenshot.jpg"));
this.fileChooser.setDialogTitle("Save Screenshot");
this.fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
this.fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
this.fileChooser.setApproveButtonText("Save");
}
public void captureScreen() {
String fileName = fileChooser.getSelectedFile().getAbsolutePath();
try {
Robot robot = new Robot();
BufferedImage screenshot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenshot, "jpg", new File(fileName));
} catch (AWTException | IOException ex) {
System.err.println(ex);
}
}
public static void main(String[] args) {
ScreenShot screenShot = new ScreenShot();
screenShot.captureScreen();
screenShot.fileChooser.showSaveDialog(screenShot);
}
}
```
二、基于Java的Web应用程序
1. Java Applet
Java Applet是基于Java的Web应用程序,可以在网页中运行。由于Java Applet已经被官方压制,使用Java Applet开发截图应用程序已经不再被推荐。
2. Java Servlet
Java Servlet是Java Web应用程序中的一种组件,它可以接收HTTP的请求并响应相应的结果。对于截图应用程序来说,可以使用Java Servlet接收请求并将截图发送给客户端。以下是使用Java Servlet实现截图的示例代码:
```java
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ScreenShotServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/jpeg");
String fileName = "screenshot.jpg";
try {
Robot robot = new Robot();
BufferedImage screenshot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenshot, "jpg", response.getOutputStream());
} catch (AWTException | IOException ex) {
System.err.println(ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
```
以上就是基于Java的桌面应用程序和Web应用程序实现截图的简要介绍,开发者可以根据自己的需求来选择适合自己的方法。