我想在java中拍摄桌面屏幕的快照。 我怎么能这样做。
这类似于在ms-paint中按下打印屏幕和粘贴数据, 然后以.png或.jpg格式保存文件。
答案 0 :(得分:4)
像这样:
Robot r = new Robot();
Toolkit t = Toolkit.getDefaultToolkit();
Dimension d = t.getScreenSize();
Image i = r.createScreenCapture(new Rectangle(0,0,d.width,d.height));
答案 1 :(得分:1)
在Java类中复制并粘贴以下代码并调用该方法 captureScreen(),文件名为参数。屏幕截图将是 存储在您在参数中指定的文件中。
import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.File; ... public void captureScreen(String fileName) throws Exception { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle screenRectangle = new Rectangle(screenSize); Robot robot = new Robot(); BufferedImage image = robot.createScreenCapture(screenRectangle); ImageIO.write(image, "png", new File(fileName)); } ...
在以下链接中解释
答案 2 :(得分:1)
从GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration cg // get your screen configuration from above
Rectangle rect = cg.getBounds();
final BufferedImage image;
image = new Robot().createScreenCapture(rect);
file = new File(yourFilePath);
String extension = "png" // or any other image extension.
ImageIO.write(bufferedImage, extension, file);
答案 3 :(得分:0)
试试这个:
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = env.getDefaultScreenDevice();
robot = new Robot(screen);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage img = robot.createScreenCapture(new Rectangle(0,0,d.width,d.height));
答案 4 :(得分:0)
Robot robot = new Robot();
String format = "jpg";
String fileName = "D://FullScreenshot." + format;
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, format, new File(fileName));