Thursday, June 19, 2008

Java Robot Class for accessing your system from a mobile phone

I was doing a simple program for my friend, when I came up across the Robot class in java. His project is accessing a system from a mobile phone. So he needs to get the screen shot, move mouse and right and double click mouse buttons. Just the Robot class is enough for this purpose.

There are methods like mouseMove, mousePress and even for getting screenshots of the system.
Once I complete the coding, i think of releasing the software as a complete package.

I decide to use Tomcat for acting as server and a j2me program for doing the job in the mobile phone.

Below is the code:-

Tomcat web pages for Screenshot, Mouse move and mouse clicks:-

Screenshot:-(mapped as screenshot.html inside tomcat)
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.geom.AffineTransform;

public class screenshot extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
try
{

Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle rect = new Rectangle(0, 0,screenSize.width,screenSize.height);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rect);
File file;
int w = 220;
int h = 280;
BufferedImage image1 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image1.createGraphics();
double xScale = (double)w / image.getWidth();
double yScale = (double)h / image.getHeight();
AffineTransform at = AffineTransform.getScaleInstance(xScale, yScale);
g2d.drawRenderedImage(image, at);
g2d.dispose();
file = new File("C:/Tomcat-4.1.31/webapps/ROOT/screen.jpg");//root folder of the server
ImageIO.write(image1, "jpg", file);
pw.println("ok");
}
catch (Exception e)
{
pw.println("error");
}
}
}

//Here i have tried to reduce the screenshot size to the standard mobile phone screensize

MouseMove:-

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;



public class MouseMove extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
try
{

int x = Integer.parseInt(req.getParameter("x"));
int y = Integer.parseInt(req.getParameter("y"));
Robot robot = new Robot();
robot.mouseMove(x,y);

}
catch (Exception e)
{
pw.println("error"+e.toString());
}
}
}


MouseClick:-
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;



public class MouseClick extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
try
{

int x = Integer.parseInt(req.getParameter("x"));
int y = Integer.parseInt(req.getParameter("y"));
String mode = req.getParameter("type");
Robot robot = new Robot();
if(mode.equals("right")){
robot.mouseMove(x,y);
robot.mousePress(InputEvent.BUTTON3_MASK);
robot.mouseRelease(InputEvent.BUTTON3_MASK);
pw.println("ok");
}
else if(mode.equals("left")){
robot.mouseMove(x,y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
pw.println("ok");
}
else if(mode.equals("left-double")){
robot.mouseMove(x,y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
pw.println("ok");
}
else
{
pw.println("error:wrong mode");
}
}
catch (Exception e)
{
pw.println("error"+e.toString());
}
}
}


And the j2me program:-
SM1.java:-

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import java.lang.*;
import java.util.*;


public class SM1 extends MIDlet implements CommandListener {

public Command exitCommand;
public Command ScreenCommand;
public Command MouseCommand;
public Command MPressCommand;
public Command MRClickCommand;
public Command MLClickCommand;
public Command MLDClickCommand;
public ntclient nt = null;
public MouseClient mc = null;
public Command BackCommand;
private Display display;
public TextField tx;
public TextField ty;
public Form displayForm;
public Form MouseForm;

public SM1() {

display = Display.getDisplay(this);
exitCommand =
new Command("Exit", Command.SCREEN, 1);
ScreenCommand =
new Command("ScreenShot", Command.SCREEN, 1);
MouseCommand =
new Command("Mouse", Command.SCREEN, 1);
MPressCommand =
new Command("Move", Command.SCREEN, 1);
BackCommand =
new Command("Back", Command.SCREEN, 2);
MRClickCommand =
new Command("RightClick", Command.SCREEN, 2);
MLClickCommand =
new Command("LeftClick", Command.SCREEN, 2);
MLDClickCommand =
new Command("LeftDoubleClick", Command.SCREEN, 2);
}

public void startApp() {
displayForm = new Form("Exchange Rate");
displayForm.addCommand(exitCommand);
displayForm.setCommandListener(this);
displayForm.addCommand(ScreenCommand);
displayForm.setCommandListener(this);
displayForm.addCommand(MouseCommand);
displayForm.setCommandListener(this);

display.setCurrent(displayForm);


}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }

public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
else if(c == ScreenCommand){
nt = new ntclient(this);


}
else if(c == MouseCommand){
MouseForm = new Form("Mouse Movement");
tx = new TextField("Enter x","",4,TextField.NUMERIC);
ty = new TextField("Enter y","",4,TextField.NUMERIC);
MouseForm.append(tx);
MouseForm.append(ty);
MouseForm.addCommand(BackCommand);
MouseForm.setCommandListener(this);
MouseForm.addCommand(MPressCommand);
MouseForm.setCommandListener(this);
MouseForm.addCommand(MRClickCommand);
MouseForm.setCommandListener(this);
MouseForm.addCommand(MLClickCommand);
MouseForm.setCommandListener(this);
MouseForm.addCommand(MLDClickCommand);
MouseForm.setCommandListener(this);
Display.getDisplay(this).setCurrent(MouseForm);
}
else if(c==BackCommand){
Display.getDisplay(this).setCurrent(displayForm);
}
else if(c==MPressCommand){
mc = new MouseClient(this,"http://127.0.0.1:8080/MouseMove.html?x="+tx.getString()+"&y="+ty.getString());
}
else if(c==MRClickCommand){
mc = new MouseClient(this,"http://127.0.0.1:8080/MouseClick.html?x="+tx.getString()+"&y="+ty.getString()+"&type=right");
}
else if(c==MLClickCommand){
mc = new MouseClient(this,"http://127.0.0.1:8080/MouseClick.html?x="+tx.getString()+"&y="+ty.getString()+"&type=left");
}
else if(c==MLDClickCommand){
mc = new MouseClient(this,"http://127.0.0.1:8080/MouseClick.html?x="+tx.getString()+"&y="+ty.getString()+"&type=left-double");
}
}

String getViaHttpConnection(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
StringBuffer str = new StringBuffer();

try {
c = (HttpConnection)Connector.open(url);
String type = c.getType();
is = c.openInputStream();
int len = (int)c.getLength();
int ch;
while ((ch = is.read()) != -1) {
str.append((char)ch);
}

} finally {
if (is != null)
is.close();
if (c != null)
c.close();
}
return str.toString();
}
}


class ntclient implements Runnable{
private SM1 parent = null;
public Thread processorThread = null;
public ntclient(SM1 parent)
{
this.parent = parent;
processorThread = new Thread(this);
processorThread.start();
}
public void run(){
try
{

String result = parent.getViaHttpConnection("http://127.0.0.1:8080/screenshot.html");
if(result.indexOf("ok")>=0)
{
parent.displayForm.append("screenshot " + result);
System.out.println(result);

MyCanvas canvas = new MyCanvas(parent);
Display.getDisplay(parent).setCurrent(canvas);

}
else
{
parent.displayForm.append("error taking screenshot");
}
}
catch (Exception exc)
{
exc.printStackTrace();
}
}
}

class MouseClient implements Runnable
{
private SM1 parent= null;
public Thread pThread = null;
public String url = null;
public MouseClient(SM1 parent,String url)
{
this.url = url;
this.parent = parent;
pThread = new Thread(this);
pThread.start();
}
public void run()
{
try{
String result = parent.getViaHttpConnection(url);
parent.displayForm.append("MouseEvent "+result);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}

class MyCanvas extends Canvas implements CommandListener
{
private Command exit;
private SM1 parent;
private Image image=null;
private Display display;
public Image getImageFromUrl(String url)
{
InputStream is = null;
HttpConnection hc = null;
Image img = null;
try {
hc = (HttpConnection)Connector.open(url);
if (hc.getResponseCode() == HttpConnection.HTTP_OK) {
is = hc.openInputStream();
int len = (int)hc.getLength();
byte[] data = new byte[len];
int actual = is.read(data);
img = Image.createImage(data, 0, len);
}
} catch (Exception e) {
System.out.println("IO Exception+"+e);
} finally {
if (is != null) {
try {
is.close();
}
catch (Exception e) { }
}
if (hc != null)
{
try {
hc.close();
}
catch (Exception e) { }
}
return img;
}
}
public MyCanvas(SM1 parent)
{
this.parent = parent;
exit=new Command("Exit", Command.EXIT,1);
addCommand(exit);
setCommandListener(this);

try
{
image = getImageFromUrl("http://127.0.0.1:8080/screen.jpg");
}
catch(Exception err)
{
Alert alert=new Alert("Failure","Can't open the image file",null,null);
err.printStackTrace();
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(parent).setCurrent(alert);
}
}
protected void paint(Graphics g)
{
if(image!=null)
{
g.drawImage(image,0,0,Graphics.TOP|Graphics.LEFT);
}
}
public void commandAction(Command command,Displayable display)
{
if(command==exit)
{
Display.getDisplay(parent).setCurrent(parent.displayForm);
}
}
}

/*by Arun Srinivasan*/


Hope to finish this one day.

No comments: