Today I was thinking to create screen recorder application in Java then I googled and found that Screen recorder codes captures still images of screen then convert still image to video. We can also use windows movie maker application to create video from still images. So I am going to share screen capturing code which is in JAVA Programming Language. This program will keep capturing still images until you not press q or Q on command prompt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.util.Scanner; import javax.imageio.ImageIO; public class ScreenCapture { static boolean stop = false; //Image width same as screen width static int imageWidth = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth(); //Image height same as screen height static int imageHeight = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight(); public static void main(String args[]){ Scanner scn = new Scanner(System.in); System.out.println("Screen Capturing... press q to quit"); startCapturing(); do{ String response = scn.next(); if(response.equalsIgnoreCase("q")){ stopCapturing(); break; } }while(true); System.out.println("Screen Capturing Stopped!"); } public static void startCapturing(){ new Thread(){ @Override public void run(){ Robot robot; try{ robot = new Robot(); File f = new File("temp"); if(!f.exists()){ f.mkdir(); //create temp directory on current location in which all images will be stored if not exists } while(!stop){ BufferedImage img = robot.createScreenCapture(new Rectangle(imageWidth,imageHeight)); ImageIO.write(img, "jpeg", new File("temp/"+System.currentTimeMillis() + ".jpeg")); // interval between each image captured Thread.sleep(50);//50 milliseconds that means 20 frames per second } } catch(Exception ex){ ex.printStackTrace(); } } }.start(); } public static void stopCapturing(){ stop = true; } } |
Output
Download Code
Screen Capture
1 file(s) 2.72 KB
Thanks
Share with you friends here
THAT IS GREAT NICE CODE FOR CAPTURING THE SCREEN.
Thanks Santosh