Blogs

Capture Your Computer Screen with Java
Author:Dixanta Bahadur Shrestha

Java awt package has a lot of libraries and with the help of few of its libraries you can actually capture your screen movement and store into image file.

I have implemented thread class in order to work the application on given interval.


/**
 * String compare Algorithm
 * Author Dixanta Bahadur Shrestha
 * Creators Institute of Business & Technology
 * Lalitpur, Nepal
 */


import java.awt.Color;
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 App{
    public static void main(String[] args)throws Exception{
        System.out.println("Capturing start");
        int i=0;
        File file=new File("capture");
        if(!file.exists() || !file.isDirectory()){
            file.mkdir();
        }
        while(true){
            i++;
            Thread thread=new Thread(new CaptureThread(i));
            thread.start();
            Thread.sleep(5000);
            
            
        }
    }
}

/**
 * String compare Algorithm
 * Author Dixanta Bahadur Shrestha
 * Creators Institute of Business & Technology
 * Lalitpur, Nepal
 */

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;

import javax.imageio.ImageIO;

public class CaptureThread implements Runnable{
    private int counter;

    public CaptureThread(int counter){
        this.counter=counter;
    }
    @Override
    public void run() {
       try{
        
        Rectangle region = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage capturedImage = new Robot().createScreenCapture(region);

        // Save as PNG
        String fileName="capture/"+counter + "_capture.png" ;
        File imageFile = new File(fileName);
        ImageIO.write(capturedImage, "png", imageFile);
        System.out.println(fileName + " captured");
        
       }catch(Exception ex){
           System.out.println(ex.getMessage());
       }
    }
}

Happy Coding !!

Join Now at #CIBT for more learning.

Previous