screen logger - capture screen periodically and save to folder - java

Screen Capture Logger Java Application : Capture screen in fixed interval and save jpeg image to a folder.
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 javax.imageio.ImageIO;

public class ScreenLogger {
    private static final String saveLocation = "c:\\logs\\";
    private static final int timeInterval = 1000 * 30;
    private static Rectangle rect;
    private static Robot robot;

    public static void main(String[] args) throws Exception {
        init();
        new Thread(new CaptureThread()).start();
    }

    private static void init() throws Exception {
        Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
        rect = new Rectangle(0, 0, scr.width, scr.height);
        robot = new Robot();
        final File f = new File(saveLocation);
        f.mkdirs();
    }

    private static class CaptureThread implements Runnable {
        @Override
        public void run() {
            while (true) {
                try {
                    saveScreen();
                    Thread.sleep(timeInterval);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static boolean saveScreen() {
        try {
            BufferedImage image = robot.createScreenCapture(rect);
            ImageIO.write(image, "jpeg", new File(saveLocation + "img-" + System.currentTimeMillis() + ".jpg"));
            return true;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return false;
        }
    }
}


This is simple extension in my previous code : Screen Capture using Robot and save

No comments :

Post a Comment

Your Comment and Question will help to make this blog better...