以下代码捕获屏幕:
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
public class capture{
public static void main(String args[]) {
try {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
} catch(Exception e) {
}
}
}
有没有办法只捕获屏幕的所需部分(例如矩形,从一个x,y点到另一个点)?
答案 0 :(得分:5)
您可以设置左上角的x
和y
,以及矩形的宽度和高度尺寸,如下所示:
BufferedImage img = robot.createScreenCapture( new Rectangle(x, y, width, height) );
答案 1 :(得分:0)
import java.awt.AWTException;
import java.awt.Component;
import java.awt.Dimension;
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;
/**
* This program demonstrates how to capture screenshot of a portion of screen.
*
*
*/
public class TaskBarCaptureImage {
public static void main(String[] args) {
try {
Robot robot = new Robot();
String format = "jpg";
String fileName = "TaskBar Captured." + format;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("ScreenSize : " + screenSize);
Rectangle captureRect = new Rectangle(0, 728, 1366, 40); // taskbar
// zone
BufferedImage screenFullImage = robot.createScreenCapture(captureRect);
ImageIO.write(screenFullImage, format, new File(fileName));
int no = 1;
ImageIO.write(screenFullImage, format, new File("./imagenes/" + no + ".png"));
System.out.println("TaskBar Captured!");
} catch (AWTException | IOException ex) {
System.err.println(ex);
}
}
}