Capture and save an image of the screen

EDDYGATE 0 Tallied Votes 234 Views Share

The code below captures the screen and saves it.
Depending on your need, you can modify it to do just what you want

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);
  }
}
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);
  }
}
jael 0 Newbie Poster

will you please explain every codes..here please

amos wang 0 Newbie Poster

can you tell me where the capiture saves?

jmaat7 0 Light Poster

Hey thanks alot, I went about doing something similar in a much longer way. this is much more efficient, I wasn't aware of the getScreenSize method.

I'm gonna try and make something in Android with this

firstsmilepk 0 Newbie Poster

Hey,

Nice Example.

Can I use it as Java Applet and store the image on the web server?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.