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
Related
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.
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.
How can I capture a screenshot using HtmlUnitDriver? I found that i can capture when I use FirefoxDriver but I don't want to use Firefox. So, please let me know how to capture images using HtmlUnitDriver.
I googled but none of the solutions I found are working for me. Please help!
Flyingsaucer is a java library which lets you take screenshots of a webpage headlessly (without opening the page in a browser). The following code will capture a screenshot of the entire page resized into an image called "screenshot.png". The number 1024 specifies the width of the output screenshot. Find the libraries here http://mvnrepository.com/artifact/org.xhtmlrenderer
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import org.xhtmlrenderer.simple.Graphics2DRenderer;
public class RenderPage {
public static void main(String[] args) throws Exception {
try {
String address = "http://www.w3.org";
BufferedImage buff = Graphics2DRenderer.renderToImageAutoSize(address, 1024);
ImageIO.write(buff, "png", new File("screenshot.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
You can also use the method Graphics2DRenderer.renderToImage(address,1024,1024); to define your own size of screenshot.
You'll find that the screenshots look as if they were rendered on a phone. I don't know why this is, but I'm guessing that you can change Flyingsaucer's browser version somewhere to emulate the page on different browsers.
One more thing is that this code won't screenshot www.google.com but apparently this type of error can be solved by tidying up the page source with a library called JTidy.
HtmlUnitDriver is GUI-less.
see: here
Maybe you can use the following:
BufferedImage image = new Robot().createScreenCapture(newRectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("/screenshot.png"));
source
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
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.