Monday, December 8, 2014

Screener

import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
class KeepLog {
static int secondsBetweenScreenshots = 5;
// static String dir ="C://bkp//notes//";
// static String dirT ="C://Users//abhinavs//Desktop//";
static String controlDir ="";
static String outputDir ="";
public static void main(String args[]) {
    int index = 1000;

    Properties prop = new Properties();
    InputStream input = null;
    try {
        input = new  FileInputStream("config.prop");
        prop.load(input);

        outputDir = prop.getProperty("out.dir");
        controlDir = prop.getProperty("control.dir");
        secondsBetweenScreenshots = Integer.parseInt(prop.getProperty("log.interval").trim());

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

    String yesFile = controlDir+"y.txt";
    String exitFile = controlDir+"ex.txt";

    System.out.println("yesFile :: "+yesFile);
    System.out.println("exitFile :: "+exitFile);

    //String userHome = System.getProperty("user.home");

    while (!fileExist(exitFile)) {

        if(fileExist(yesFile))
            takeScreenshot(outputDir+"BKP" + index++);
        else
            System.out.println("STOPPED!!");

        try {
            TimeUnit.SECONDS.sleep(secondsBetweenScreenshots);
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }
}

private  final static String getDateTime()
{
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hhmmss");
    df.setTimeZone(TimeZone.getTimeZone("PST"));
    return df.format(new Date());
}

public static void takeScreenshot(String filename) {
    Rectangle allScreenBounds = getAllScreenBounds();
    Robot robot;
    try {
        robot = new Robot();
        BufferedImage screenShot = robot.createScreenCapture(allScreenBounds);

        String pathOut = filename + getDateTime()+ ".log";
        System.out.println("Writing: "+pathOut);
        File fo = new File(pathOut);
        ImageIO.write(screenShot, "png", fo);

    } catch (AWTException e) {
        System.err.println("Something went wrong starting the robot");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Something went wrong writing files");
        e.printStackTrace();
    }
}

private static Rectangle getAllScreenBounds() {
    Rectangle allScreenBounds = new Rectangle();
    try{
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] screens = ge.getScreenDevices();

        int farx = 0;
        int fary = 0;
        for (GraphicsDevice screen : screens) {
            Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
            // finding the one corner
            if (allScreenBounds.x > screenBounds.x) {
                allScreenBounds.x = screenBounds.x;
            }
            if (allScreenBounds.y > screenBounds.y) {
                allScreenBounds.y = screenBounds.y;
            }
            // finding the other corner
            if (farx < (screenBounds.x + screenBounds.width)) {
                farx = screenBounds.x + screenBounds.width;
            }
            if (fary < (screenBounds.y + screenBounds.height)) {
                fary = screenBounds.y + screenBounds.height;
        }
            allScreenBounds.width = farx - allScreenBounds.x;
            allScreenBounds.height = fary - allScreenBounds.y;
        }
        }
        catch(Exception e)
        {
            Logger.getGlobal().log(Level.INFO,"ERROR!!",e);
        }
    return allScreenBounds;
}

private static Boolean fileExist(String filePathString)
{
    try{
        File f = new File(filePathString);
        return f.exists();
    } catch (Exception e) {
        //e.printStackTrace();
        return true;
    }
}
}

==================
config.prop
out.dir=C://bkp//notes//
control.dir=C://Users//abhinavs//Desktop//
log.interval=1
================
bat
javaw KeepLog