Friday, April 9, 2010

Screen capture with mouse position

Here I am showing, that how can get screen shot with mouse pointer position.
This is the small 10% part of VIDEO RECORDING of your desktop activity.




import java.awt.AWTException;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.io.File;
import javax.imageio.ImageIO;
//    Screen capture with mouse position.
//     Date: 2:22 PM Wednesday Mar/24/2010
class GetScreenShot implements Runnable {
    private Robot robot;
    private Rectangle r;
    private String filename,seq;
    private int num,maxZero;
    public GetScreenShot() {
        try{
            robot = new Robot();
            num=1;
            maxZero = 11;
            seq = "";

        }
        catch(Exception ex1) {
            ex1.printStackTrace();
        }
    }

    public void run() {
        while(true) {
            try {
            System.out.println(num);
            String numSt=num+"";
            int i;
            for(i=1;i<=maxZero -numSt.length();i++) {
                seq=seq+"0";
            }   
            seq=seq+num+".jpg";
            filename=seq;
            r=new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            File file = new File(filename);
            BufferedImage bf=null;
            bf=captureScreen(r);
            ImageIO.write(bf, "JPEG", file);
            num++;
            seq="";
            Thread.sleep(9);
            }
            catch(Exception ee){
                System.out.println(ee.toString());
            }
        }
    }
   
    public BufferedImage captureScreen(Rectangle recordArea) {
        Point mousePosition = MouseInfo.getPointerInfo().getLocation();
        BufferedImage image = robot.createScreenCapture(recordArea);
        Polygon pointer = new Polygon(new int[]{0,16,10,8},new int[]{0,8,10,16},4);
        Polygon pointerShadow = new Polygon(new int[]{6,21,16,14},new int[]{1,9,11,17},4);
        Graphics2D gr12D = image.createGraphics();
        gr12D.translate(mousePosition.x,mousePosition.y);
        gr12D.setColor(new Color(100,100,100,100) );
        gr12D.fillPolygon(pointerShadow);
        gr12D.setColor(new Color(100,100,255,200) );
        gr12D.fillPolygon(pointer);
        gr12D.setColor(Color.red);
        gr12D.drawPolygon(pointer);
        gr12D.dispose();
        return image;
    }   
}
public class ScreenCapWithMousePos {
    public static void main(String ss[]) {
        GetScreenShot gs=new GetScreenShot();
        Thread thread1 = new Thread(gs);
        thread1.start();
    }
}