Skip to main content

Capturing Screen Shot using java




Capturing screenshot using java

Here i will tell you how to write a program using java which is capable of capturing screenshots(either whole or partial).And save it as an image.

Which java API ?

The java.awt.Robot class provides a useful method for capturing a screenshot.Here is the method prototype.

BufferedImage createScreenCapture(Rectangle screenRect)

It is clear from the prototype that this function returns a BufferedImage and takes a Rectangle class object (portion of the screen to capture) as the argument.The BufferedImage which is returned could be saved using ImageIO class write method.Lets begin with the program.


1) Capturing full screen


In order to capture full screen using createScreenCapture(Rectangle screenRect)
screenRect should have the full screen size and in order to do that we will use Toolkit class of java.

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());


Now here begins the whole program.
/******************************************************************/
/* Author: Shashank sahu                                          */
/*                                                                */
/* Description: Program which captures Screen shot of whole screen*/                                            
/*                                                                */
/******************************************************************/

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 FullScreenCaptureExample {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            String format = "jpg";//can be,“png”, “bmp”,"jpeg" etc
            String fileName = "screenshot." + format;
             
            Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            BufferedImage screenImg = robot.createScreenCapture(screenRect);
            ImageIO.write(screenImg, format, new File(fileName));
             
            System.out.println("screenshot saved!");
        } 
        catch (AWTException | IOException ex) {
            System.err.println(ex);
        }
    }
}







2)Capturing a portion of screen


You can also capture only a portion of screen by defining the rectangle object of createScreenCapture().

Rectangle area=new Rectangle(int startxpos,int startypos,int endxpos,int endypos)
startxpos >>Starting x position in pixels.
startypos>> Starting y position in pixels.
endxpos >> Ending x position in pixels.
endypos >>Ending y position in pixels

Here is the complete code.


/******************************************************************/
/* Author: Shashank sahu                                          */
/*                                                                */
/* Description: Program which captures partial Screen shot        */                                            
/*                                                                */
/******************************************************************/

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 PartialScreenCaptureExample {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            String format = "jpg";
            String fileName = "PartialScreenshot." + format;
             
            
            Rectangle captureRect = new Rectangle(0, 0, 100,100);/*top left corner to 100  pixels down and 100 pixels right*/
             BufferedImage screenFullImage = robot.createScreenCapture(captureRect);
            ImageIO.write(screenFullImage, format, new File(fileName));
             
            System.out.println("partial screenshot saved!");
        } catch (AWTException | IOException ex) {
            System.err.println(ex);
        }
    }
}

Comments

Popular posts from this blog

Create a simple virus using java

What's my idea Before we start talking how we will create a simple flooder virus using java we must be clear about what is  the idea and how to implement it. So the idea is to create a program which will create  thousands of blank files in a drive you will don't believe but it could create file with  a speed of   1060+ files per second . How to implement it Now the problem is how we would do this?? So here is the answer .  First of all we will design a loop which will run  N  number of times where  N  is the number of files which we want to create. Then we will create a new File with the help of  createNewFile()  function of  java.io.File  class. The name of each file is the value of the variable which is used in the loop. Here is the sample virus program. /**  * **********This virus program is just for educational purpose   * author will not be re...

How to setup a .jar file in Blue-J

How to set a new package in Blue-J In this tutorial you will learn how you can setup a jar file in Blue J IDE. Follow the given steps and you could easily set it up. Let me specify that in my case we will install a jar file created my me that is utility.jar .If you are downloading other packages like Free TTS,open cv, j3d or any other then you just need to find the jar files in their folder and rest of the tutorial remain the same. Step 1:- Open blue-J Ide Open your blue j ide in my case it is Blue-J 3.1.7. Step 2:- In the top menu bar click on TOOLS and after that in the drop down menu click on PREFERENCES . Step 3 :- After clicking on preferences you will see a screen like this.      Just click on Libraries Tab.   Step 4 :- After clicking on libraries blue j will show the list of pre loaded jar files that you must be using before.On the right hand side of the window you will see ADD button just click on it. Step 5:- ...