How to take screenshot with Java.

There is nothing new in taking a screenshot . In all software or hardware there is something known as  frame buffer which stores all the currently pixel on the displayed screen as numbers. Screenshot are the main output of the numbers into a file to interpreter as png ,  jpg .

There are many screenshot utilities resources

  1. Scrot, a simple command line program which takes screenshots using imlib2 implemented in C.

2. Chrome Screen Capture app by Google, implemented in JS/C++.
This articles shows how to take a screenshot with Java and saving the output with the system’s current date and time

// FOR FULL SCREEN SHOT
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ScreenShot
{
    public String dateAndTime() throws Exception
    {
        return String.format("%s",LocalDateTime.now().format(DateTimeFormatter.ofPattern("MM-dd-YY  hh-mm-ss.")));
    }
    public String fullScreenShot() throws Exception {
        String format = "png";
        String fileName = dateAndTime() + format;
        try {
            Robot robot = new Robot();
            Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
            ImageIO.write(screenFullImage, format, new File("F:\\amuda-adeolu-badmus\\images\\"+fileName));
        } catch (IOException ex)
        {
            System.err.println(ex);
        }
        catch (AWTException ex) {
            System.err.println(ex);
        }
        return fileName +" Saved sucessfully.Please check the destination folder";
    }

    public static void main ( String[] args) throws Exception
    {
        ScreenShot take = new ScreenShot();
        System.out.println(take.fulllScreenShot());
    }
}

Output

2017-05-06 at 08-17-47

//FOR PARTIAL SCREEN SHOT

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ScreenShot
{
    public String dateAndTime() throws Exception
    {
        return String.format("%s",LocalDateTime.now().format(DateTimeFormatter.ofPattern("MM-dd-YY  hh-mm-ss.")));
    }
    public String partialScreenShot()  throws Exception
    {
        String format = "jpg";
        String fileName = dateAndTime() + format;
        try {
            Robot robot = new Robot();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle capture = new Rectangle(0, 0, screenSize.width / 2, screenSize.height / 2);
            BufferedImage screenFullImage = robot.createScreenCapture(capture);
            ImageIO.write(screenFullImage, format, new File("F:\\amuda-adeolu-badmus\\images\\"+fileName));
        } catch (IOException ex) {
            System.err.println(ex);
        }
        catch (AWTException ex) {
            System.err.println(ex);
        }
        return fileName +" Saved sucessfully.Please check the destination folder";
    }
    public static void main ( String[] args) throws Exception
    {
        ScreenShot take = new ScreenShot();
        System.out.println(take.partialScreenShot());
    }
}

Output

2017-05-06 at 08-21-29

Now let’s use these two methods to create a simple Java EE text editor app with full and partial screenshot functionality.

            #{user.name} #{user.textName}#{user.textOption}                                 

Welcome… ${user.name}

 

 
 
#{user.textName}#{user.textOption}

 

File Name 2017-05-09 at 00-05-33

package com.amudabadmus.texteditor;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.imageio.ImageIO;
import javax.inject.Named;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Named
@RequestScoped
@ManagedBean(name="screenShot")
public class ScreenShot
{
 private final String savedPath ="D:\\amuda-adeolu-badmus\\images\\";
 public String dateAndTime() throws Exception
 {
 return String.format("%s",LocalDateTime.now().format(DateTimeFormatter.ofPattern("MM-dd-YY hh-mm-ss.")));
 }
 public String fullScreenShot() throws Exception {
 String format = "png";
 String fileName = dateAndTime() + format;
 try {
 Robot robot = new Robot();
 Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
 BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
 ImageIO.write(screenFullImage, format, new File(savedPath+fileName));
 } catch (IOException ex)
 {
 System.err.println(ex);
 }
 catch (AWTException ex) {
 System.err.println(ex);
 }
 return fileName +" Screen shotted sucessfully...";
 }
 public String partialScreenShot() throws Exception
 {
 String format = "png";
 String fileName = dateAndTime() + format;
 try {
 Robot robot = new Robot();
 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 Rectangle capture = new Rectangle(0, 0, screenSize.width / 2, screenSize.height / 2);
 BufferedImage screenFullImage = robot.createScreenCapture(capture);
 ImageIO.write(screenFullImage, format, new File(savedPath+fileName));
 } catch (IOException ex) {
 System.err.println(ex);
 }
 catch (AWTException ex) {
 System.err.println(ex);
 }
 return fileName +" Screen shotted sucessfully...";
 }

 public static void main (String[] args) throws Exception
 {
 ScreenShot screenShot = new ScreenShot();
 System.out.println(screenShot.fullScreenShot());
 }
}

For more information, please check the demo and the source code.

That’s all. Thanks for reading

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: