Making a screencast software in java - java

This is how I plan to make it:
There will be a small JFrame with start and stop buttons to start and stop recording. Clicking start will start a thread that will create a java.awt.Robot that will take repeated screen shots.
Here is where my problem starts. Robot does take a screenshot but
1. The position of the cursor will not be recorded
2. I dont know where the image will be stored. I intend to store them in a folder whose name will be determined using java.util.Date.
3. I dont understand BufferedImage class one bit.
I have finished making JFrame and implemented the thread. I have package that I got from the internet that converts images to avi movies.
Please help me solve the above mentioned three problems.
Edited:
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public void captureScreen(String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(fileName));
}
if you could please explain t he above mentioned code with respect to my problem.

The solution for getting the cursor in the screenshot is to:
figure out the position of the cursor.
java.awt.MouseInfo.getPointerInfo().getLocation()
and place a rendered image of the cursor on top of the screenshot.
Take a look at this: http://docs.oracle.com/javase/tutorial/2d/images/index.html

Related

Is there any way to wait to capture the screen till application/web page open in Java?

I am doing the automation for the application. I want to open the application/web page and try to take capture the screen. But I am facing the issue while doing the same. I am using the Robot class of java to capture the screen. My problem is
Before the application opens, the screen capture function is called which doesn't include the application window I want
Same issue for the web page. Before web page fully loaded, screen shot is taken.
I don't want any hard delay (i.e sleep(2000)). I want to add smart delay for both of the cases. Can somebody help me with this issue? Here is the sample code.
I have already tried it with hard delay but it is working in some cases and not in some. It is hard to predict the exact delay for web page to load.
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;
import javax.imageio.ImageIO;
public class Screenshot
{
public static void main(String[] args)
{
try
{
Runtime.getRuntime().exec("calc");
Robot robot = new Robot();
Thread.sleep(200);
String path = "D:// calc.jpg";
Rectangle capture = new
Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage Image = robot.createScreenCapture(capture);
ImageIO.write(Image, "jpg", new File(path));
System.out.println("Screen Captured);
}
catch (IOException | InterruptedException ex)
{
System.out.println(ex);
}
}
}
It should wait till calc window open and then take the screen shot. Now it depends on the system load how much time the calc window take to open. Same for the web page. I want a smart delay to wait until the application/web page open.

How to take screenshots of my chrome client using java

I am trying to create a program that whenever I run it, it takes a screenshot of Google Chrome. So far, I have figured out how to have it take a screenshot and write it to a file. However, this is just a screenshot of the entire screen. I'd like to take a screenshot of chrome, even if it is just running in the background. Also, I'd like to be able to re-size Google Chrome and run the program to get images with the same dimensions as Google Chrome.
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.awt.Rectangle;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class ProgramDriver {
public static void main(String[] args) {
Rectangle windowScreen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
try {
BufferedImage capture = new Robot().createScreenCapture(windowScreen);
ImageIO.write(capture, "png", new File("screenshot.png"));
} catch (AWTException | IOException e) {
e.printStackTrace();
}
}
}
One way is to use image processing/recognition and recognize a distinctive symbol in your browser.
The other way is to go to the operating system and get the attributes where the window is etc
The third approach is to go with your mouse and check the coordinates/and size "ah now its there" and enter the coordinates into your program, "ah now its there..."
In effect you need the upper left coordinates and the size - any way you can get it
You can put up another question depending on which way you want

Take a screenshot and print it in Java

I am in need of help. I'm not a very proficient coder, so i'll ask directly.
I am looking to create a script that would be initiated through the pressing of a keyboard button. It would then wait randomly between 10 to 30 seconds and then take a screenshot. The screenshot would be then added to the print queue and printed with a printer.
I am aware of two scripts which do two steps of this, but unsure how to connect them to a seamless thing ( along with a delay mechanism, which is important, also would be good to have a 5 second period between possible initiations of the script so you can't make 50 prints in the queue when you mash the button and wreck the system )
( for information it would be taking screenshots of the output of an interactive system working with generative stuff and video feed, im not yet sure on the software i will use for that maybe vvvv )
Take screenshot
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 screen2image
{
public static void main(String[] args) throws Exception
{
Robot robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
}
}
// print image
FileInputStream fin = new FileInputStream("YOurImageFileName.PNG");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
job.print(doc, pras);
Many thanks in advance for any help with this. It would be great for my project.

java console mouselistener

I am trying to make a mouse recorder, I cant seem to get a mouse listener to work with a console, is this possable and how would I go about it Thanks.
Unless you wrote your own console that fired mouse events, I dont' think you're going to be able to do it. What widget are you going to register your mouselistener against otherwise? The console isn't a swing component, therefore, no swing events.
You can do this by using global hooks.
In order to use them you'll need to include some natives or try the same using JNI (see: wikipedia).
Two examples:
http://kra.lc/blog/2011/07/java-global-system-hook/(works well - I would advice to re-indent the c++ content and if you know how to do, merge it into 1-2 files - your eyes will thank you)
http://www.jotschi.de/Technik/2008/01/06/java-global-keyboard-hook-jni.html(never tried, but looks more simple)
Edit:
Example for some playback functionalities:
import java.awt.AWTException;
import java.awt.DisplayMode;
import java.awt.MouseInfo;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.util.Random;
// class instructions
try {
PointerInfo pntInfo = MouseInfo.getPointerInfo();
DisplayMode dispMode = pntInfo.getDevice().getDisplayMode();
int newX = new Random().nextInt( dispMode.getWidth() );
int newY = new Random().nextInt( dispMode.getHeight() );
new Robot( pntInfo.getDevice() ).mouseMove( newX, newY );
} catch ( AWTException exception ) { }
Sorry for my late answer ;)

porting screen shot java app on android

i want your help very badly..bcz i am seeking an answer for this from couple of day's but did not find a bit...
my query is i have a screen shot app written in java...i just want to port it on android emulator and run it..i know i have to rewrite some android specific code but can anyone tell me what changes i should make to the screen shot java app to make it run on android platform..
here is my java screen shot app: (i know for this the device should be rooted i am okay for that)
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
class ScreenCapture {
public static void main(String args[]) throws
AWTException, IOException {
// capture the whole screen
BufferedImage screencapture = new Robot().createScreenCapture(
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
// Save as JPEG
File file = new File("screencapture.jpg");
ImageIO.write(screencapture, "jpg", file);
// Save as PNG
// File file = new File("screencapture.png");
// ImageIO.write(screencapture, "png", file);
}
}
There aren't just a few changes that you have to made. You have to rewrite the whole app and that wouldn't be so easy since making screenshots in Android isn't this easy as in plain Java.
For example you can't use java.awt.Robot because this lib isn't included in Android.
Afaik you also need root rights on a Android phone to make a screenshot. I would recommend that you google for librarys or apps that are already able to do screenshots and use them.
For example the Android Screenshot Library (ASL) is a good point to start with.

Categories

Resources