`
flyPig
  • 浏览: 136991 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

java实现远程桌面监控

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

	public Server() {
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		rectangle = new Rectangle(screenSize);// 可以指定捕获屏幕区域
		try {
			robot = new Robot();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
		}
	}

	public void run() {
		ZipOutputStream os = null;
		Socket socket = null;
		while (true) {
			try {
				socket = new Socket("192.168.1.100", 5001);// 连接远程IP
				BufferedImage image = robot.createScreenCapture(rectangle);// 捕获制定屏幕矩形区域
				os = new ZipOutputStream(socket.getOutputStream());// 加入压缩流
				// os = new ZipOutputStream(new FileOutputStream("C:/1.zip"));

				os.setLevel(9);
				os.putNextEntry(new ZipEntry("test.jpg"));
				JPEGCodec.createJPEGEncoder(os).encode(image);// 图像编码成JPEG
				os.close();
				Thread.sleep(50);// 每秒20帧
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (os != null) {
					try {
						os.close();
					} catch (Exception ioe) {
					}
				}
				if (socket != null) {
					try {
						socket.close();
					} catch (IOException e) {
					}
				}
			}
		}
	}

	public static void main(String[] args) {
		new Server().start();
	}
}

public class Client extends JFrame {
	private static final long serialVersionUID = 1L;
	Dimension screenSize;

	public Client() {
		super();
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(800, 640);
		Screen p = new Screen();
		Container c = this.getContentPane();
		c.setLayout(new BorderLayout());
		c.add(p, SwingConstants.CENTER);
		new Thread(p).start();
		SwingUtilities.invokeLater(new Runnable(){
			public void run() {
				setVisible(true);
			}});
	}

	public static void main(String[] args) {
		new Client();
	}

	class Screen extends JPanel implements Runnable {

		private static final long serialVersionUID = 1L;
		private Image cimage;

		public void run() {
			ServerSocket ss = null;
			try {
				ss = new ServerSocket(5001);// 探听5001端口的连接
				while (true) {
					Socket s = null;
					try {
						s = ss.accept();
						ZipInputStream zis = new ZipInputStream(s
								.getInputStream());
						zis.getNextEntry();
						cimage = ImageIO.read(zis);// 把ZIP流转换为图片
						repaint();
					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						if (s != null) {
							try {
								s.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
				}
			} catch (Exception e) {
			} finally {
				if (ss != null) {
					try {
						ss.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}

		public Screen() {
			super();
			this.setLayout(null);
		}

		public void paint(Graphics g) {
			super.paint(g);
			Graphics2D g2 = (Graphics2D) g;
			g2.drawImage(cimage, 0, 0, null);
		}
	}
}
分享到:
评论
3 楼 xds2008 2013-02-26  
写的不错,值得表扬,测了一下,还不错,就是有点慢,但是最起码远程监视的原理实现了,呵呵
2 楼 exp111 2011-10-28  
这个确实很强 但是能不能连接一次不停的传呢 这个好像是必须连一次传一张
1 楼 nickevin 2009-06-19  
有点意思

相关推荐

Global site tag (gtag.js) - Google Analytics