How to Capture a Screenshot from a Remote System Using Java

There's a simple Java application that you can use to send a current screenshot over the Internet. You may find this program useful if you need to manage multiple computers, if you own a business and want to monitor the computer use of your employees, or if you want to monitor the computer use of your children. It involves the creation of two Java classes: ScreenServer.java and ScreenClient.java. However, be sure to check the warning at the bottom of the page before putting the application into use.

Instructions

    • 1

      Open your preferred text editor, and immediately save the empty document as "ScreenServer.java." This is the part of the program that must be running on the remote system. It will listen for a connection on an arbitrary port and send the screen dimensions and data over that port. This example uses "5494" as the port, but any port number will do, provided it doesn't conflict with another service running on the computer. Naturally, you will need to have direct access to the remote computer at some point in order to put this program on it and start it running.

    • 2

      Paste the following code into the "ScreenServer":

      import java.net.*;

      import java.awt.*;

      import java.awt.image.*;

      import java.io.*;

      import javax.imageio.ImageIO;

      public class ScreenServer {

      public static void main(String[] args) throws Exception {

      Robot robot = new Robot();

      BufferedImage screen;

      while (true) {

      ServerSocket server = new ServerSocket(5494);

      Socket client = server.accept();

      Rectangle size = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

      screen = robot.createScreenCapture(size);

      int[] rgbData = new int[(int) (size.getWidth()*size.getHeight())];

      screen.getRGB(0,0, (int) size.getWidth(), (int) size.getHeight(), rgbData, 0, (int) size.getWidth());

      OutputStream baseOut = client.getOutputStream();

      ObjectOutputStream out = new ObjectOutputStream(baseOut);

      ImageIO.write(screen, "png", new File("orig_screen.png"));

      out.writeObject(size);

      for (int x = 0; x < rgbData.length; x++) {

      out.writeInt(rgbData[x]);

      }

      out.flush();

      server.close();

      client.close();

      out.close();

      baseOut.close();

      }

      }

      }

      Save your code.

    • 3

      Open a new document in your text editor, and name it "ScreenClient.java." This is the program that will run locally. It simply connects to the remote computer at the address specified and saves the pixel data it receives to the hard disk in PNG format.

    • 4

      Paste the following code, and save your work:

      import java.net.*;

      import java.awt.*;

      import java.awt.image.*;

      import java.io.*;

      import javax.imageio.ImageIO;

      public class ScreenClient {

      public static void main(String[] args) throws Exception {

      Socket server = new Socket(args[0], 5494);

      ObjectInputStream in = new ObjectInputStream(server.getInputStream());

      Rectangle size = (Rectangle) in.readObject();

      int[] rgbData = new int[(int)(size.getWidth() * size.getHeight())];

      for (int x = 0; x < rgbData.length;x++) {

      rgbData[x] = in.readInt();

      }

      in.close();

      server.close();

      BufferedImage screen = new BufferedImage((int) size.getWidth(), (int) size.getHeight(), BufferedImage.TYPE_INT_ARGB);

      screen.setRGB(0,0, (int) size.getWidth(), (int) size.getHeight(), rgbData, 0,(int)size.getWidth());

      ImageIO.write(screen, "png", new File("screen.png"));

      }

      }

Tips & Warnings

  • You can test this program out on your own computer by typing "java ScreenServer" into one command prompt window to start the server, and "java ScreenClient localhost" into another command prompt window to start the client. The order is important: the client will fail without a server. Later, when you have the server installed and running on another computer, simply replace "localhost" with the IP address of that computer.

  • This application should be used only on computers owned and used exclusively by you. Be sure to consult an attorney to find out what the law is in your local jurisdiction before attempting to use the application on a computer used by another person. Your jurisdiction may permit it, may permit it with explicit permission from the other user, or may forbid it outright as a form of wiretapping. Only by consulting a qualified attorney who specializes in privacy matters can you be certain to stay on the right side of the law.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured