Friday, August 8, 2008

Java class to capture frame in JPG format

Here is the thing that enables java developers to capture the frame into a picture. To capture a frame, you need to use BufferedImage class of the java.awt.image package.

The BufferedImage subclass describes an Image with an accessible buffer of image data. A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0). Any Raster used to construct a BufferedImage must therefore have minX=0 and minY=0.

You can go to the following link to see the API of BufferedImage class.
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/BufferedImage.html

A demo code for capturing the frame into picture is given below.

Source code:

import java.awt.AWTException;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;


public class Fig extends javax.swing.JFrame implements ActionListener{

private JButton jButton1 = null;
private JLabel jLabel1 = null;
public Fig(){
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
setLayout(new FlowLayout());
jLabel1 = new JLabel("Demo");
jButton1 = new JButton("Demo");

this.add(jLabel1);
this.add(jButton1);
jButton1.setActionCommand("Demo");
jButton1.addActionListener(this);
this.setTitle("Figure");
this.setSize(200,100);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}

public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand().equals("Demo")){
try {
this.createImage(this,"C:\\Demo.jpg");
} catch (AWTException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
// Methods to create Image correspoding to the frame.
public static BufferedImage createImage(Component component, String fileName)
throws AWTException, IOException {
Point p = new Point(0, 0);
SwingUtilities.convertPointToScreen(p, component);
Rectangle region = component.getBounds();
region.x = p.x;
region.y = p.y;
return Fig.createImage(region, fileName);
}

public static BufferedImage createImage(Rectangle region, String fileName)
throws AWTException, IOException {
BufferedImage image = new Robot().createScreenCapture( region );
Fig.writeImage(image, fileName);
return image;
}

public static void writeImage(BufferedImage image, String fileName)
throws IOException {
if (fileName == null) return;
int offset = fileName.lastIndexOf( "." );
String type = offset == -1 ? "jpg" : fileName.substring(offset + 1);

ImageIO.write(image, type, new File( fileName ));
}
// Methods to create Images
//End
public static void main(String args[]){
Fig fig = new Fig();
}
}


The highlighted portion of the code is responsible for capturing the frame into picture. There are two createImage methods in the code.

The first one converts a point from the component’s coordinate system to screen coordinates. Then it gets the bound of the component in the form of rectangle object. The bound specify the width, height and location relative to its parents.

It passes the location/region and size details to the second createImage method. The second one creates the image containing the pixels read from the screen corresponding to the region.

The writeImage method writes it to the specific file in jpg format with the help of write method of ImageIO class.




Practical Use:

It is quite useful in standalone applications.

The application is also useful, whenever you want some frame to be shown to a particular group of people. If you want some chart frame or any status frame which indicates some record over the past few months, you can easily capture it in jpg format and send it to concerned persons.



Hope you all will find it useful.

No comments:

Total Pageviews