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.get
DefaultToolkit().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.}