使用 java.awt.Robot 可以抓取屏幕
import java.awt.AWTException; import java.awt.Robot; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; class ScreenCapture { public static void main(String args[]) throws AWTException, IOException { // capture the whole screen BufferedImage screencapture = new Robot().createScreenCapture( new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) ); // Save as JPEG File file = new File("screencapture.jpg"); ImageIO.write(screencapture, "jpg", file); // Save as PNG // File file = new File("screencapture.png"); // ImageIO.write(screencapture, "png", file); } }
可以指定要抓取的区域
BufferedImage screencapture = new Robot().createScreenCapture( new Rectangle( 15, 15, 150, 150));
可以抓取指定控件:
BufferedImage image = new Robot().createScreenCapture( new Rectangle( myframe.getX(), myframe.getY(), myframe.getWidth(), myframe.getHeight() ) );
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/11080.html