In this post we shall see how to capture a screenshot with java. Also, we shall see how to capture the GUI (complete with the frame or partial e.g. a graph displayed in a panel) thrown by a Java Program. This may be useful in the case when we have to take a print out of, say, a graph or an image which is displayed on the Graphical User Interface(GUI).
Part 1 : The Screenshot
To do this we shall make use of the Robot, BufferedImage and ImageIO classes. A brief description of how the classes are used follows :
Robot : The Robot class has a function called createScreenCapture() which take as parameter a Rectangle object. The Rectangle object specifies the portion of the screen to capture (through coordinates). It returns a BufferedImage object. For taking a screenshot the rectangle must cover the entire screen. We can get the screen size by using the Toolkit.getDefaultToolkit().getScreenSize() method.
BufferedImage : This class is used to describe an image. We shall store the return value from the call to the createScreenCapture() method of the Robot class in a BufferedImage object. Then this will be used to write a file containing the image using the ImageIO class.
ImageIO : It has a static method called write() which takes three parameters : the image(BufferedImage object), the format of the file to write(e.g. png) and the name of the file.
The CODE :
Part 2 : Cpturing Java GUI
Now, we shall try to capture the GUI displayed by a Java Swing/AWT program. This can be useful when we have to print the whole layout displayed by a program or print a portion of it (say a graph or an image displayed on it). To do this we shall make use of the BufferedImage and the ImageIO classes. The following points explain the process :
1. Instantiate a BufferedImage object (called capture in our example) using the three argument contructor. The arguments passed are the a) width - width of the created image b) height - height of the created image c) imageType - type of the created image.
2. Call the components paint method(the component whose contents are to be captured). Pass as argument capture.getGraphics(). When the getGraphics() method is called on a component, it returns the component's graphics context, which lets you draw on a component. When this is passed as argument to paint method, the contents of the component calling the paint method(pnl2 in our case) are drawn on the object whose Graphic context is passed as argument.
3. Lastly we use the ImageIO.write() method to write the BufferedImage object to a file.
The CODE :
Here we display a simple frame with two panels and capture the contents of pnl2 in an image.
Note that we pass the component to be captured to the captureit() method. So if we need to display the whole frame that is to be displayed, we can simply pass the JFrame object.
Part 1 : The Screenshot
To do this we shall make use of the Robot, BufferedImage and ImageIO classes. A brief description of how the classes are used follows :
Robot : The Robot class has a function called createScreenCapture() which take as parameter a Rectangle object. The Rectangle object specifies the portion of the screen to capture (through coordinates). It returns a BufferedImage object. For taking a screenshot the rectangle must cover the entire screen. We can get the screen size by using the Toolkit.getDefaultToolkit().getScreenSize() method.
BufferedImage : This class is used to describe an image. We shall store the return value from the call to the createScreenCapture() method of the Robot class in a BufferedImage object. Then this will be used to write a file containing the image using the ImageIO class.
ImageIO : It has a static method called write() which takes three parameters : the image(BufferedImage object), the format of the file to write(e.g. png) and the name of the file.
The CODE :
import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; class ScreenCapture { public static void main(String args[]) { try { Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); BufferedImage capture = new Robot().createScreenCapture(screenRect); ImageIO.write(capture, "png", new File("d:\\screencapture.png")); }catch (Exception ex) { System.out.println(ex); } } }
Part 2 : Cpturing Java GUI
Now, we shall try to capture the GUI displayed by a Java Swing/AWT program. This can be useful when we have to print the whole layout displayed by a program or print a portion of it (say a graph or an image displayed on it). To do this we shall make use of the BufferedImage and the ImageIO classes. The following points explain the process :
1. Instantiate a BufferedImage object (called capture in our example) using the three argument contructor. The arguments passed are the a) width - width of the created image b) height - height of the created image c) imageType - type of the created image.
2. Call the components paint method(the component whose contents are to be captured). Pass as argument capture.getGraphics(). When the getGraphics() method is called on a component, it returns the component's graphics context, which lets you draw on a component. When this is passed as argument to paint method, the contents of the component calling the paint method(pnl2 in our case) are drawn on the object whose Graphic context is passed as argument.
3. Lastly we use the ImageIO.write() method to write the BufferedImage object to a file.
The CODE :
Here we display a simple frame with two panels and capture the contents of pnl2 in an image.
import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.*; public class CapturePanelInFrame extends JFrame { JPanel pnl1,pnl2; JLabel lab1,lab2; public CapturePanelInFrame() { //Creating the GUI pnl1=new JPanel(); pnl2=new JPanel(); pnl1.setBackground(Color.blue); pnl2.setBackground(Color.green); lab1=new JLabel("This is the Label in Panel1"); lab2=new JLabel("This is the Label in Panel2"); pnl1.add(lab1); pnl2.add(lab2); setLayout(new GridLayout(2,1)); add(pnl1); add(pnl2); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,200); setVisible(true); captureit(pnl2); } private void captureit(JComponent comp) { Rectangle rec = comp.getBounds(); // Getting the size of Component BufferedImage capture = new BufferedImage(rec.width, rec.height,BufferedImage.TYPE_INT_ARGB); comp.paint(capture.getGraphics());//capturing the components content in the capture object try { ImageIO.write(capture, "png", new File("capturepnl.png")); //writing the captued image to a file } catch (Exception e) { System.out.println(e); } } public static void main(String args[]) { new CapturePanelInFrame(); } }
Note that we pass the component to be captured to the captureit() method. So if we need to display the whole frame that is to be displayed, we can simply pass the JFrame object.
No comments:
Post a Comment