Possible Duplicate:
Is there a way to take a screenshot using Java and save it to some sort of image?
How to take a screenshot in Java?
We use java.It provides method like createScreenCapture which captures the current screen. This method returns captured image as BufferedImage object which can be saved as a file. It also uses ImageIO to save it as PNG image format.
This feature is exclusive to Java Edition. A screenshot taken in-game, using F1 to hide the heads-up display. Screenshots are images taken in-game by pressing the (by default) F2 key or Fn + F2 for Macs and some other keyboards.
Use Robot#createScreenCapture()
.
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); ImageIO.write(image, "png", new File("/screenshot.png"));
You can find this code useful. this code will take screenshot in every 10 seconds
import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; public class screen2image { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd hh mm ss a"); public void robo() throws Exception { Calendar now = Calendar.getInstance(); Robot robot = new Robot(); BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); ImageIO.write(screenShot, "JPG", new File("d:\\"+formatter.format(now.getTime())+".jpg")); System.out.println(formatter.format(now.getTime())); } public static void main(String[] args) throws Exception { screen2image s2i = new screen2image(); while(true) { s2i.robo(); Thread.sleep(10000); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With