Screenshot of our program from our Program
Lets try to take Screenshot of our program from our Program. It sound weired right. Thats what we will try to attempt in this post. Always operating system tries to take a screenshot when we press PrtScn in keyboard. This time lets make our Java program to take the screenshot.
As usual we will just brush up few library's and methods required to create this java program.
1) getScreenSize method in Java.awt.toolkit : Gets the size of the screen. On systems with multiple displays, the primary display is used. Multi-screen aware display dimensions are available from
GraphicsConfiguration
and GraphicsDevice
.2) java.awt.Robot :- This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.
Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example,
Robot.mouseMove
will actually move the mouse cursor instead of just generating mouse move events. Note that some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an
AWTException
will be thrown when trying to construct Robot objects. For example, X-Window systems will throw the exception if the XTEST 2.2 standard extension is not supported (or not enabled) by the X server. Applications that use Robot for purposes other than self-testing should handle these error conditions gracefully.
3) java.awt.Rectange :- A
Rectangle
specifies an area in a coordinate space that is enclosed by the Rectangle
object's upper-left point (x,y)
in the coordinate space, its width, and its height.A
Rectangle
object's width
and height
are public
fields. The constructors that create a Rectangle
, and the methods that can modify one, do not prevent setting a negative value for width or height. A
Rectangle
whose width or height is exactly zero has location along those axes with zero dimension, but is otherwise considered empty. The isEmpty() method will return true for such a Rectangle
. Methods which test if an empty Rectangle
contains or intersects a point or rectangle will always return false if either dimension is zero. Methods which combine such a Rectangle
with a point or rectangle will include the location of the Rectangle
on that axis in the result as if the addPoint(method) method were being called. A
Rectangle
whose width or height is negative has neither location nor dimension along those axes with negative dimensions. Such a Rectangle
is treated as non-existant along those axes. Such a Rectangle
is also empty with respect to containment calculations and methods which test if it contains or intersects a point or rectangle will always return false. Methods which combine such a Rectangle
with a point or rectangle will ignore the Rectangle
entirely in generating the result. If two Rectangle
objects are combined and each has a negative dimension, the result will have at least one negative dimension. Methods which affect only the location of a
Rectangle
will operate on its location regardless of whether or not it has a negative or zero dimension along either axis. Note that a
Rectangle
constructed with the default no-argument constructor will have dimensions of 0x0
and therefore be empty. That Rectangle
will still have a location of (0,0)
and will contribute that location to the union and add operations. Code attempting to accumulate the bounds of a set of points should therefore initially construct the Rectangle
with a specifically negative width and height or it should use the first point in the set to construct the Rectangle
. For example:
Rectangle bounds = new Rectangle(0, 0, -1, -1);for (int i = 0; i < points.length; i++) { bounds.add(points[i]); }
or if we know that the points array contains at least one point:
Rectangle bounds = new Rectangle(points[0]);for (int i = 1; i < points.length; i++) { bounds.add(points[i]); }
This class uses 32-bit integers to store its location and dimensions. Frequently operations may produce a result that exceeds the range of a 32-bit integer. The methods will calculate their results in a way that avoids any 32-bit overflow for intermediate results and then choose the best representation to store the final results back into the 32-bit fields which hold the location and dimensions. The location of the result will be stored into the x and y fields by clipping the true result to the nearest 32-bit value. The values stored into the width and height dimension fields will be chosen as the 32-bit values that encompass the largest part of the true result as possible. Generally this means that the dimension will be clipped independently to the range of 32-bit integers except that if the location had to be moved to store it into its pair of 32-bit fields then the dimensions will be adjusted relative to the "best representation" of the location. If the true result had a negative dimension and was therefore non-existant along one or both axes, the stored dimensions will be negative numbers in those axes. If the true result had a location that could be represented within the range of 32-bit integers, but zero dimension along one or both axes, then the stored dimensions will be zero in those axes.
4) delay method :- Sleeps for the specified time. If you want more time to capture image, ie to move the screen to specified page and then take image, just give more milliseconds.
5) createScreenCapture method :- Creates an image containing pixels read from the screen. This image does not include the mouse cursor.
6) createGraphics method :- Creates a
Graphics2D
, which can be used to draw into this BufferedImage
Now lets see the real program :-
package client;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
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 java.util.Scanner;
import javax.imageio.ImageIO;
public class ScreenCapture {
public static void main(String args[]) {
System.out.println("Enter the name which you want to give it to your sceenshot");
Scanner kb=new Scanner(System.in);
String screenShotName=kb.next();
String output="C:\\"+screenShotName+".jpeg";
Robot robot = null;
try {
robot = new Robot();
robot.delay(1000);
} catch (AWTException e) {
e.printStackTrace();
}
Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
int width = (int) d.getWidth();
int height = (int) d.getHeight();
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.createGraphics();
Rectangle rectangle=new Rectangle(d);
Image image = robot.createScreenCapture(rectangle);
g.drawImage(image, 0, 0, width, height, null);
try {
ImageIO.write( bufferedImage, "jpeg",new File(output));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("You can find screenshot in c:\\"+screenShotName+".jpeg");
}
}
Let me show you the output :-
Comments
Post a Comment