Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Capture your Screen using Java

It will help you to capture a screenshot of your screen and save it in a JPG image. This logic can be extended to implement a Screen Recorder.

Hi Friends, Today I will tell you how to make a program which will help you obtain screenshots of your screen and store them as an image .This logic can be extended to implement a Screen Recorder (You may make the program take screenshots at a regular interval using concepts of loop and sleep method in Thread class. After you obtain a sequence of image you may combine them in one video file).

First of all you need to import necessary classes:

import java.awt.Dimension;
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;

Dimension class is used for storing the screen coordinates, rectangle is used for defining a rectangle which will take the screen size with help of dimension object, robot class is responsible for taking of screenshots and toolkit helps to obtain the screen size.

  • BufferedImage will store the captured screen image.
  • File is used obtaining the reference to output file
  • ImageIO is used to write the screenshot to the output file.

Now, we define the class and the main function:

public class ScreenCapture {
public static void main(String args[])
{
	try
	{		

After this first we will obtain the size of the screen say “x” so that we may tell the robot class that it has to capture a screen of size “x”.

Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

Now when we have obtained the screen size we have stored that in variable named size. Now we will define a Robot class which will take the screenshot.

Robot robot = new Robot();
     BufferedImage img = robot.createScreenCapture(new Rectangle(size));

Here we took a screenshot with help of robot.createScreenCapture(new Rectangle(size)).Rectangle object is made to tell the robot class the size of the screen.We store this in a BufferedImage object so that it can be written to a file.

Now we will write this to an external file:

File save_path=new File("screen.jpg");
     ImageIO.write(img, "JPG", save_path);
	}
	catch(Exception e)
	{
		System.out.println(“Problem”);
	}
}
}

Here we will make a file object which will make a new file called screen.jpg .Now we write the image from the buffered image object and take the file format for saving as JPG.

After we run the program it will capture a screenshot and store that in a file called screen.jpg

Output :

Image 1

Now you can see the full source code of the article:

import java.awt.Dimension;
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;

public class ScreenCapture {
public static void main(String args[])
{
	try
	{		
     Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
     Robot robot = new Robot();
     BufferedImage img = robot.createScreenCapture(new Rectangle(size));
     File save_path=new File("screen.jpg");
     ImageIO.write(img, "JPG", save_path);
	}
	catch(Exception e)
	{
		
	}
}
}


My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login