java远程桌面监视,Java实现远程桌面监控

Java实现远程桌面监控

2012/6/1 16:46:38  李建辉  程序员俱乐部  我要评论(0)

摘要:java里面的Robot类可以完成截图的功能,借助于这点,我尝试着做了一个简陋的桌面监控程序,运行了下,感觉速度还可以,还有很大的优化空间的,比如用udp协议取代tcp等。代码也写的不是很优雅,只在娱乐了。实现原理其实很简单,在被监视者的机器上,运行一个线程,每隔一段时间就自动截图,并把截图压缩发送到指定的机器上;在监视机器上,也是运行一个线程,接收发送过来的图片包,解压,并绘制到当前的窗口上。这样就基本完成了。Java代码1.publicclassServerextendsThread{2

java里面的Robot类可以完成截图的功能,借助于这点,我尝试着做了一个简陋的桌面监控程序,运行了下,感觉速度还可以,还有很大的优化空间的,比如用udp协议取代tcp等。代码也写的不是很优雅,只在娱乐了。

实现原理其实很简单,在被监视者的机器上,运行一个线程,每隔一段时间就自动截图,并把截图压缩发送到指定的机器上;在监视机器上,也是运行一个线程,接收发送过来的图片包,解压,并绘制到当前的窗口上。这样就基本完成了。

Java代码

1.public class Server extends Thread {

2.    private Dimension screenSize;

3.    private Rectangle rectangle;

4.    private Robot robot;

5.

6.    public Server() {

7.        screenSize = Toolkit.getDefaultToolkit().getScreenSize();

8.        rectangle = new Rectangle(screenSize);// 可以指定捕获屏幕区域

9.        try {

10.            robot = new Robot();

11.        } catch (Exception e) {

12.            e.printStackTrace();

13.            System.out.println(e);

14.        }

15.    }

16.

17.    public void run() {

18.        ZipOutputStream os = null;

19.        Socket socket = null;

20.        while (true) {

21.            try {

22.                socket = new Socket("192.168.1.100", 5001);// 连接远程IP

23.                BufferedImage image = robot.createScreenCapture(rectangle);// 捕获制定屏幕矩形区域

24.                os = new ZipOutputStream(socket.getOutputStream());// 加入压缩流

25.                // os = new ZipOutputStream(new FileOutputStream("C:/1.zip"));

26.

27.                os.setLevel(9);

28.                os.putNextEntry(new ZipEntry("test.jpg"));

29.                JPEGCodec.createJPEGEncoder(os).encode(image);// 图像编码成JPEG

30.                os.close();

31.                Thread.sleep(50);// 每秒20帧

32.            } catch (Exception e) {

33.                e.printStackTrace();

34.            } finally {

35.                if (os != null) {

36.                    try {

37.                        os.close();

38.                    } catch (Exception ioe) {

39.                    }

40.                }

41.                if (socket != null) {

42.                    try {

43.                        socket.close();

44.                    } catch (IOException e) {

45.                    }

46.                }

47.            }

48.        }

49.    }

50.

51.    public static void main(String[] args) {

52.        new Server().start();

53.    }

54.}

Java代码

1.public class Client extends JFrame {

2.    private static final long serialVersionUID = 1L;

3.    Dimension screenSize;

4.

5.    public Client() {

6.        super();

7.        screenSize = Toolkit.getDefaultToolkit().getScreenSize();

8.        this.setSize(800, 640);

9.        Screen p = new Screen();

10.        Container c = this.getContentPane();

11.        c.setLayout(new BorderLayout());

12.        c.add(p, SwingConstants.CENTER);

13.        new Thread(p).start();

14.        SwingUtilities.invokeLater(new Runnable(){

15.            public void run() {

16.                setVisible(true);

17.            }});

18.    }

19.

20.    public static void main(String[] args) {

21.        new Client();

22.    }

23.

24.    class Screen extends JPanel implements Runnable {

25.

26.        private static final long serialVersionUID = 1L;

27.        private Image cimage;

28.

29.        public void run() {

30.            ServerSocket ss = null;

31.            try {

32.                ss = new ServerSocket(5001);// 探听5001端口的连接

33.                while (true) {

34.                    Socket s = null;

35.                    try {

36.                        s = ss.accept();

37.                        ZipInputStream zis = new ZipInputStream(s

38.                                .getInputStream());

39.                        zis.getNextEntry();

40.                        cimage = ImageIO.read(zis);// 把ZIP流转换为图片

41.                        repaint();

42.                    } catch (Exception e) {

43.                        e.printStackTrace();

44.                    } finally {

45.                        if (s != null) {

46.                            try {

47.                                s.close();

48.                            } catch (IOException e) {

49.                                e.printStackTrace();

50.                            }

51.                        }

52.                    }

53.                }

54.            } catch (Exception e) {

55.            } finally {

56.                if (ss != null) {

57.                    try {

58.                        ss.close();

59.                    } catch (IOException e) {

60.                        e.printStackTrace();

61.                    }

62.                }

63.            }

64.        }

65.

66.        public Screen() {

67.            super();

68.            this.setLayout(null);

69.        }

70.

71.        public void paint(Graphics g) {

72.            super.paint(g);

73.            Graphics2D g2 = (Graphics2D) g;

74.            g2.drawImage(cimage, 0, 0, null);

75.        }

76.    }

77.}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值