Created
October 11, 2013 05:17
-
-
Save obullxl/6929915 to your computer and use it in GitHub Desktop.
Java 截屏代码 截取电脑屏幕 并保存为图片
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ScreenSnapshot { | |
public static void main(String[] args) { | |
try { | |
int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(); //要截取的宽度 | |
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight(); //要截取的高度 | |
Robot robot = new Robot(); | |
BufferedImage image = robot.createScreenCapture(new Rectangle(width,height)); | |
image = image.getSubimage(0, 0, 200, 500); | |
ImageIO.write (image, "png" , new File("c:/1.png")); //保存在C盘 图片名为1.png | |
} catch (AWTException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
image = image.getSubimage(0, 0, width, height);
截取整个桌面图片。