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));
}
Showing posts with label Robot Class. Show all posts
Showing posts with label Robot Class. Show all posts
Tuesday, October 16, 2012
Screen shot capturing in Java
This code snippet captures the screen shots automatically
Monday, September 17, 2012
Java Robot Example
On executing this program, the system will automatically search for the keyword that is passed in the java file and open that program in the system. For example this program will open notepad direclty on executing if notepad is the keywor passed.
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
public class RobotExample {
public static void main(String[] args) throws Exception{
String str = "Hie Sam";
Robot r = new Robot();
r.mouseMove(20, 890);
r.mousePress(InputEvent.BUTTON1_MASK);
r.delay(20);
r.mouseRelease(InputEvent.BUTTON1_MASK);
typeKeys("Notepad",r);
r.delay(50);
r.keyPress(KeyEvent.VK_ENTER);
r.delay(20);
typeKeys(str, r);
}
public static void typeKeys(String str,Robot r)
{
for(int i=0;i<str.length();i++)
{
typeCharacter(r, ""+str.charAt(i));
}
}
public static void typeCharacter(Robot robot, String letter)
{
try
{
boolean upperCase = Character.isUpperCase( letter.charAt(0) );
String variableName = "VK_" + letter.toUpperCase();
Class clazz = KeyEvent.class;
Field field = clazz.getField( variableName );
int keyCode = field.getInt(null);
robot.delay(1000);
if (upperCase) robot.keyPress( KeyEvent.VK_SHIFT );
robot.keyPress( keyCode );
robot.keyRelease( keyCode );
if (upperCase) robot.keyRelease( KeyEvent.VK_SHIFT );
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Saturday, September 15, 2012
How to take a screenshot using JAVA code
This is a code snippet in JAVA through which you can implement the functionality of taking screenshot.
//You will need to import the following packages first.
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;
//Embbed this method in your code to implement the screenshot capturing utility.
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));
}
Thursday, September 13, 2012
Take a Screenshot of your Screen | Java Example
This example uses java.awt.Robot class to capture the screen pixels and returns a BufferedImage. Java.awt.Robot class is used to take the control of mouse and keyboard. Once you get the control , you can do any type of operation related to mouse and keyboard through your java code.
/*
Copy and paste following code in your Java class and invoke the method captureScreen()
with file name as argument. The screen shot will be stored in the file that you specified in argument.
*/
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;
// Method to capture images of your desktop in png format
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));
}
Subscribe to:
Posts
(
Atom
)