Sr. Software Architect, Passionate about Open source Tools & Technologies. Expert in Enterprise Web Application Development using Java/J2ee Platform. Worked significantly on Enterprise Data Grid, Transactional Data Management, B2B Integration and Performance & Code Instrumentation. Senthil has posted 8 posts at DZone. You can read more from them at their website. View Full User Profile

Screen Record & Play Using Java

02.17.2010
| 22271 views |
  • submit to reddit

This tip shows how to create a custom movie maker using the Java Media Framework. First the sample code below shows how to capture your screen and creates a nice .jpeg or .gif image of your screen content.

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 ScreenCapture {

public static void main(String[] args) throws Exception {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Robot rt = new Robot();
BufferedImage img = rt.createScreenCapture(new Rectangle((int) screen
.getWidth(), (int) screen.getHeight()));
ImageIO.write(img, "jpeg", new File(System.currentTimeMillis()
+ ".jpeg"));
}
}

The Java Media Framework provides support to put all these images together to make a movie of your screen shot. Download the full custom movie maker code I have put together :).

The ScreenRecorder code: 

package com.easycapture.recorder;

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 java.net.MalformedURLException;
import java.util.Scanner;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.media.MediaLocator;

/**
* Main class that starts the Recording process of EasyCapture.
*
* @author Senthil Balakrishnan
*/
public class Recorder {

/**
* Screen Width.
*/
public static int screenWidth = (int) Toolkit.getDefaultToolkit()
.getScreenSize().getWidth();

/**
* Screen Height.
*/
public static int screenHeight = (int) Toolkit.getDefaultToolkit()
.getScreenSize().getHeight();

/**
* Interval between which the image needs to be captured.
*/
public static int captureInterval = 50;

/**
* Temporary folder to store the screenshot.
*/
public static String store = "tmp";

/**
* Status of the recorder.
*/
public static boolean record = false;

/**
*
*/
public static void startRecord() {
Thread recordThread = new Thread() {
@Override
public void run() {
Robot rt;
int cnt = 0;
try {
rt = new Robot();
while (cnt == 0 || record) {
BufferedImage img = rt
.createScreenCapture(new Rectangle(screenWidth,
screenHeight));
ImageIO.write(img, "jpeg", new File("./"+store+"/"
+ System.currentTimeMillis() + ".jpeg"));
if (cnt == 0) {
record = true;
cnt = 1;
}
// System.out.println(record);
Thread.sleep(captureInterval);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
recordThread.start();
}

/**
* @throws MalformedURLException
*
*/
public static void makeVideo(String movFile) throws MalformedURLException {
System.out
.println("#### Easy Capture making video, please wait!!! ####");
JpegImagesToMovie imageToMovie = new JpegImagesToMovie();
Vector<String> imgLst = new Vector<String>();
File f = new File(store);
File[] fileLst = f.listFiles();
for (int i = 0; i < fileLst.length; i++) {
imgLst.add(fileLst[i].getAbsolutePath());
}
// Generate the output media locators.
MediaLocator oml;
if ((oml = imageToMovie.createMediaLocator(movFile)) == null) {
System.err.println("Cannot build media locator from: " + movFile);
System.exit(0);
}
imageToMovie.doIt(screenWidth, screenHeight, (1000 / captureInterval),
imgLst, oml);

}

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println("######### Starting Easy Capture Recorder #######");
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("Your Screen [Width,Height]:" + "["
+ screen.getWidth() + "," + screen.getHeight() + "]");
Scanner sc = new Scanner(System.in);
System.out.println("Rate 20 Frames/Per Sec.");
System.out
.print("Do you wanna change the screen capture area (y/n) ? ");
if (sc.next().equalsIgnoreCase("y")) {
System.out.print("Enter the width:");
screenWidth = sc.nextInt();
System.out.print("Enter the Height:");
screenHeight = sc.nextInt();
System.out.println("Your Screen [Width,Height]:" + "["
+ screen.getWidth() + "," + screen.getHeight() + "]");
}
System.out
.print("Now move to the screen you want to record");
for(int i=0;i<5;i++){
System.out.print(".");
Thread.sleep(1000);
}
File f = new File(store);
if(!f.exists()){
f.mkdir();
}
startRecord();
System.out
.println("\nEasy Capture is recording now!!!!!!!");

System.out.println("Press e to exit:");
String exit = sc.next();
while (exit == null || "".equals(exit) || !"e".equalsIgnoreCase(exit)) {
System.out.println("\nPress e to exit:");
exit = sc.next();
}
record = false;
System.out.println("Easy Capture has stopped.");
makeVideo(System.currentTimeMillis()+".mov");
}
}

And in order to make the movie, you will need the JpegImagesToMovie class.

AttachmentSize
Recorder.java4.03 KB
JpegImagesToMovie.java13.74 KB
4
Your rating: None Average: 4 (1 vote)
Published at DZone with permission of its author, Senthil Balakrishnan.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Kumar Mettu replied on Wed, 2010/02/17 - 4:07am

Nice Article. I wrote about something similar on my Blog a while ago:

http://javaswamy.blogspot.com/2003/09/j2se-taking-screen-snapshots-in-java.html

Avi Yehuda replied on Wed, 2010/02/17 - 8:29am

Thanks for the article and code.

Very cool indeed.

 Avi.

Senthil Balakrishnan replied on Sun, 2010/03/07 - 7:42am in response to: Avi Yehuda

Thanks guys!!

Ehsan Haq replied on Thu, 2010/12/09 - 12:48am

hi

 

this code is very usefull but where is audio recoding is it missing ?

and  how we can run it in our web server 

 

Prateek Singh replied on Thu, 2013/11/14 - 4:52am in response to: Senthil Balakrishnan

This code is very useful..

But tell me how to use this in web application.I want to call screen recorder applet in Asp.Net Apllication

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.