AllJava

Capturing Screen in Java, Your Own printscreen

I happened to visit a cyber cafe for some reasons, and I had to take the screenshot of the screen and Alas, for some (security :P) reasons, print screen was not working. Grrrr..
Luckily, the guy had JDK installed on his machine and I had some good 10 – 15 minutes to pull this up. So I decided to quickly use Java to capture the screen and thought to share the Utility with you. Offcourse! I have now added Swing help everyone out here.
You can skip the entire post and can play around with the code / utility with section downloads below –
Downloads –



So, first we need to create a simple program that can capture your Screen. Java AWT provides Robot class for this purpose.
If you just need the Java Code, the snippet below is all what you need.

package com.roadtobe.supaldubey.tools.screenshot;
import java.awt.AWTException;
...
/**
* @author Supal Dubey
*/
public class ScreenCapture {
private static Robot robot ;
static
{
try
{
robot = new Robot();
}
catch (AWTException e)
{
throw new RuntimeException("Unable to Initialize", e);
}
}
public static BufferedImage capture()
{
return robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
}

 
Its pretty simple, all you need to do is to call createScreenCapture and provide it the dimensions of your screen. I used AWT Toolkit class to fetch the Screen resolution.
Now, how do we convert buffered image and place it into a file ?
 

BufferedImage image = ScreenCapture.capture();
try
{
File path = ... ;//Path to your file
if(path.getName().indexOf(".jpg") == -1)
{
path = new File(path.getPath() + ".jpg");
}
ImageIO.write(image, "jpg", path);
}
catch (IOException e1)
{
e1.printStackTrace();
}

 
ImageIO class takes care of conversion and writing to the file, you could use any of the supported extensions like jpg / png etc. I used jpg because it has quite small size requirements.
In case you need the Image only to be placed on the clipbord just like print screen, boy you are in for some typing. Nevertheless, I have got the exact same thing for you. Java directly supports placing text and files to the clipbord, for Images, you need to implement Transferable. This interface helps java understand what data has to be put, what support has to be provided and how to do it.Here is how it looks –
 

package com.roadtobe.supaldubey.tools.screenshot;
import java.awt.Image;
...
/**
* @author Supal Dubey
*/
class TransferableClipbordImage implements Transferable {
privateĀ  Image image;
public TransferableClipbordImage( Image image ) {
this.image = image;
}
@Override
public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException, IOException {
if ( flavor.equals( DataFlavor.imageFlavor ) && image != null ) {
return image;
}
else {
throw new UnsupportedFlavorException( flavor );
}
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.imageFlavor};
}
@Override
public boolean isDataFlavorSupported( DataFlavor flavor ) {
DataFlavor[] flavors = getTransferDataFlavors();
for (DataFlavor dataFlavor: flavors) {
if ( flavor.equals( dataFlavor ) ) {
return true;
}
}
return false;
}
}

 
Also, you would need to update your ScreenCapture class to implement ClipboardOwner interface, this is required as you are handling the System clipbord.
Now our capture method looks like the one below to support writing data to System clipbord.

Image capturedImage = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
TransferableClipbordImage image = new TransferableClipbordImage(capturedImage);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(image, this);
System.out.println("Captured Image");
return capturedImage ;

 
Thats it, this will take care of everything you need. You can now have the same function call to save captured Image directly to a file as well as to place it on the clipbord.
You can download the entire project from downloads link above. I have renamed the runnable Jar as .zip before uploading so as to avoid any security warnings.
Last but not least Don’t be evil. šŸ™‚

6 thoughts on “Capturing Screen in Java, Your Own printscreen

Leave a Reply

Your email address will not be published. Required fields are marked *