Friday, November 07, 2008

Capturing a Screenshot in Java (Using AWT)

Taking a screen shot in Java, sounds interesting right. Suddenly, While Searching to internet I came across this.


Here is the piece of code with which you can do this task:

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ScreenCapture {

public static void main(String[] args) {
try {
Robot robot = new Robot();

//The Size of Rectangle is Similar to Screen Resolution.
BufferedImage bi=robot.createScreenCapture(new Rectangle(1280,1024));

//This will write the image to this particular location
ImageIO.write(bi, "jpg", new File("C:/Ashish Docs/ScreenShot.jpg"));

} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception ex){
ex.printStackTrace();
}
}
}


Don't worry guys the code is working and tested.
Here is the same output of this program:


I hope this will help. Cheers.....

Ashish Mishra

"There are some people who live in a Dream World, And There are some who face Reality; And Then there are those who turn One into the Other "



2 Comments:

Vaibhavi said...

Hi all,

I am working for a software integrator company. My projects includes working on Java and Ruby on Rails and Ajax. I think Web Services is really cool. We also recently have to now work on REST and they are talking about mashups and Struts. Can anyone tell me if there are some good training or conferences so that me and my team members can get to speed with these technologies. Learning from books is not my cup of tea, even not when I was doing engineering ;)

All the help that group members can provide in this regard is much appreciated.

Thanks,
Vaibhavi

anaz said...

Hi Vaibhavi,

There are several online resources available that you just google for. If any of your team like to read then quality books from wiley and oreilly cover such technologies in detail.

I also highly recommend you could attend the upcoming Great Indian Developer Summit (http://www.developersummit.com) that is covering Java, Agile, REST, JAX-RS, mashups, .NET, Rich Web, JPA, SOA, rich user experiences, Spring, Groovy and more. They have most of the creators of these technologies as speakers. My team is attending this summit 22-25 apr at IISc campus where we are attending the web conference on April 23 and java on April 24. We have been able to get very good discounts. Maybe all those who are interested from your group can sign up together and get a good bargain from them. what say? I also attended last year's conference and had a really cool time.

In Hyderabad there is Sun Tech Days with some sun speakers.

Thanks,
Anaz