DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Migrating From Lombok to Records in Java
  • Simplifying Data Entities in Spring Data With Java Records
  • Java 21 Record and Pattern Matching: Master Data-Oriented Programming[Video]
  • Introduction to Apache Kafka With Spring

Trending

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Ethical AI in Agile
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  1. DZone
  2. Coding
  3. Java
  4. Screen Record & Play Using Java

Screen Record & Play Using Java

By 
Senthil Balakrishnan user avatar
Senthil Balakrishnan
·
Feb. 17, 10 · Interview
Likes (2)
Comment
Save
Tweet
Share
50.6K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Java (programming language) Record (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Migrating From Lombok to Records in Java
  • Simplifying Data Entities in Spring Data With Java Records
  • Java 21 Record and Pattern Matching: Master Data-Oriented Programming[Video]
  • Introduction to Apache Kafka With Spring

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!