October 04, 2013

Java code to get screen shot

How to get screen shot in java



 /**
 * @author Jim Brown
 *
 */

import java.awt.AWTException;
import java.awt.Dimension;
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 javax.imageio.ImageIO;
public class ScreenShotCapture {
   
    private static final String DIR_NAME = "SouthPark";

    public static void main(String[] shutdown)  {
        ScreenShotCapture captureSS = new ScreenShotCapture();
        captureSS.createDirectory(DIR_NAME);
        try {
            captureSS.getScreen();
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void createDirectory(String dirName) {
        File newDir = new File("D:\\"+dirName);
        if(!newDir.exists()){
            boolean isCreated = newDir.mkdir();
        }
}

    private void getScreen()throws AWTException, IOException {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimensions = toolkit.getScreenSize();
        Robot robot = new Robot();  // Robot class
        BufferedImage screenshot = robot.createScreenCapture(new Rectangle(dimensions));
        ImageIO.write(screenshot, "png", new File("D:\\"+DIR_NAME+"\\Gravity.png"));
   
   }
}

Hi Friends,

         This is a small program named Xkeycode  that enables you to capture system screen. Once you run the code it will capture the screen and save  and png image in your system. so please try it out..




import java.awt.AWTException;
import java.awt.Dimension;
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.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;


import javax.imageio.ImageIO;


/**
 * @author Bithesh
 *
 */
public class ScreenTimer implements Runnable {

    private static long nextTime = 0;
    private static final String DIR_NAME = "Austincollie";
    private static ScreenTimer timer = null;
    private int slno = 0;
    private Date date = null;
   
    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
       
        timer = new ScreenTimer();
        timer.createDirectory(DIR_NAME);
        timer.getNextFreq();
        Thread thread = new Thread(timer);
        thread.start();
       
    }

    private void getNextFreq() {
        long currentTime = System.currentTimeMillis();
        Random random = new Random();
        long value = random.nextInt(3600); //3600000
        nextTime = currentTime + value;
        System.out.println("current time "+currentTime + " Next Time : : "+nextTime);
        //return currentTime+value;
    }

    @Override
    public void run() {
        while(true){
            //System.out.println(" Next time  ::: "+nextTime);
            if(nextTime < System.currentTimeMillis()){
                System.out.println(slno+" ) get screen shot ");
                try {
                    timer.getScreen();
                    slno++;
                    timer.getNextFreq();
                } catch (AWTException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch(Exception e){
                    e.printStackTrace();
                }
               
            }
            //System.out.println(" sachin tendulkar ....");
        }
       
    }

    private void createDirectory(String dirName) {
       
        File newDir = new File("D:\\"+dirName);
        if(!newDir.exists()){
            boolean isCreated = newDir.mkdir();
        }
    }
   
    private void getScreen()throws AWTException, IOException {
        date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("_yyMMdd_HHmmss");
        String fileName = System.getenv("COMPUTERNAME")+dateFormat.format(date);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimensions = toolkit.getScreenSize();
        Robot robot = new Robot();  // Robot class
        BufferedImage screenshot = robot.createScreenCapture(new Rectangle(dimensions));
        ImageIO.write(screenshot, "png", new File("D:\\"+DIR_NAME+"\\"+fileName+".png"));
   
   }
}

The above code have edited . you can use this code to take screen shot at intervals. intervals have choose as random. so one cant predict when the application will take screenshots. The screenshots will be saved in the folder named austincolle (In D drive)

Do you wish to sent the screenshot from your client machine to server? then see this java code

http://belazy.blog.com/

Facebook comments