Java小项目之远程桌面显示(摘自蓝桥老师视屏)

  主要是使用了一个Socket套接字,还有输入流输出流,多线程,还有一些其他的。看完就记得老师说过人对动画的感知再0.065秒,觉得编程最有意思的莫过于那种掌控全局的感觉。有点跑偏,继续说用到的东西。

  主要用的是网络编程吧,其实都是很基础的东西,自己来手写代码能理解更多。都是常用到的东西。直接贴代码吧,下一个我会进行自己的修改。

Client类:

 1 package com.rookie.screen;
 2 
 3 import java.awt.Dimension;
 4 import java.awt.FlowLayout;
 5 import java.io.DataInputStream;
 6 import java.net.Socket;
 7 
 8 import javax.swing.ImageIcon;
 9 import javax.swing.JFrame;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12 import javax.swing.JScrollPane;
13 
14 public class Client {
15 
16     public static void main(String[] args) {
17         String host = "127.0.0.1";
18         String post = "16000";
19         
20         try {
21             Socket client = new Socket(host, Integer.parseInt(post));
22             DataInputStream dis = new DataInputStream(client.getInputStream());
23             
24             //界面
25             JFrame jFrame = new JFrame("桌面监视");
26             jFrame.setSize(1024, 768);
27             jFrame.setLocationRelativeTo(null);
28             jFrame.setLayout(new FlowLayout());
29             
30             jFrame.setAlwaysOnTop(true);
31             jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32             
33             //读取分辨率
34             double height = dis.readDouble();
35             double width = dis.readDouble();
36             
37             Dimension ds = new Dimension((int)width, (int)height);
38             
39             jFrame.setSize(ds);
40             
41             JLabel backImage = new JLabel();
42             JPanel panel = new JPanel();
43             //滚动条
44             JScrollPane jp = new JScrollPane(panel);
45             panel.setLayout(new FlowLayout());
46             panel.add(backImage);
47             //设置窗口可见。
48             jFrame.add(panel);
49             jFrame.setVisible(true);
50             
51             while(true) {
52                 int len = dis.readInt();
53                 byte[] data = new byte[len];
54                 dis.readFully(data);
55                 ImageIcon imageIcon = new ImageIcon(data);
56                 backImage.setIcon(imageIcon);
57                 //重新绘制面板。
58                 jFrame.repaint();
59             }
60             
61         } catch (Exception e) {
62             // TODO Auto-generated catch block
63             e.printStackTrace();
64         }
65         
66         
67         
68     }
69     
70     
71 }

Server类:

 1 package com.rookie.screen;
 2 /**
 3  * 
 4  * @author RookieSa
 5  *
 6  */
 7 
 8 import java.awt.Dimension;
 9 import java.awt.Rectangle;
10 import java.awt.Robot;
11 import java.awt.Toolkit;
12 import java.awt.image.BufferedImage;
13 import java.io.ByteArrayOutputStream;
14 import java.io.DataOutputStream;
15 import java.io.OutputStream;
16 import java.net.ServerSocket;
17 import java.net.Socket;
18 
19 import com.sun.image.codec.jpeg.JPEGCodec;
20 import com.sun.image.codec.jpeg.JPEGImageEncoder;
21 
22 
23 public class Server {
24     //建立服务器的监听
25     public static void main(String[] args) {
26         try {
27             ServerSocket ss = new ServerSocket(16000);
28             System.out.println("正在连接服务器>>>>>>");
29             //等待连接
30             Socket client = ss.accept();
31             //服务器连接成功
32             System.out.println("服务器连接成功>>>>>>");
33             //获取输出流
34             OutputStream os = client.getOutputStream();
35             //二进制文件
36             DataOutputStream dos = new DataOutputStream(os);
37             //使用多线程传输
38             SreenThread st = new SreenThread(dos);
39             st.start();
40         }catch(Exception e){
41             e.printStackTrace();
42         }
43     }
44 
45 }
46 
47 class SreenThread extends Thread{
48     //数据输出流
49     DataOutputStream dataOut;
50     
51     public SreenThread(DataOutputStream dataOut) {
52         this.dataOut = dataOut;
53     }
54     
55     public void run() {
56         Toolkit tk = Toolkit.getDefaultToolkit();
57         Dimension dm =tk.getScreenSize();
58         
59         //获取屏幕分辨率,发送
60         try {
61             dataOut.writeDouble(dm.getHeight());
62             dataOut.writeDouble(dm.getWidth());
63             //刷新内存
64             dataOut.flush();
65             
66             //定义一个矩形
67             Rectangle rc = new Rectangle(dm);
68             
69             //定义一个机器人
70             Robot robot = new Robot();
71             
72             while(true) {
73                 BufferedImage bufferedImage = robot.createScreenCapture(rc);
74                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
75                 
76                 //图像压缩处理
77                 JPEGImageEncoder jEncoder = JPEGCodec.createJPEGEncoder(baos);
78                 jEncoder.encode(bufferedImage);
79                 
80                 byte[] data = baos.toByteArray();
81                 //不停输出
82                 dataOut.writeInt(data.length);
83                 dataOut.write(data);
84                 dataOut.flush();
85                 Thread.sleep(0);
86             }
87         } catch (Exception e) {
88             // TODO Auto-generated catch block
89             e.printStackTrace();
90         }
91     }
92 }

 

posted @ 2017-08-21 20:40  TulLing  阅读(2073)  评论(0编辑  收藏  举报