网络编程屏幕广播之学生端

121 阅读5分钟
字节转换工具类
[AppleScript]
纯文本查看
复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cn.itcast.screenbroadcast;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
public class Util {
//
private static Robot robot ;
//
static{
try {
robot = new Robot();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ץͼ
*/
public static byte[] captureScreen(){
try {
BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 1366, 768));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null ;
}
public static byte[] long2Bytes(long l){
byte[] bytes = new byte[8] ;
bytes[0] = (byte)l ;
bytes[1] = (byte)(l >> 8) ;
bytes[2] = (byte)(l >> 16) ;
bytes[3] = (byte)(l >> 24) ;
bytes[4] = (byte)(l >> 32) ;
bytes[5] = (byte)(l >> 40) ;
bytes[6] = (byte)(l >> 48) ;
bytes[7] = (byte)(l >> 56) ;
return bytes ;
}
public static long byte2Long(byte[] bytes){
return ((long)(bytes[0] & 0xFF))
| ((long)(bytes[1] & 0xFF) << 8)
| ((long)(bytes[2] & 0xFF) << 16)
| ((long)(bytes[3] & 0xFF) << 24)
| ((long)(bytes[4] & 0xFF) << 32)
| ((long)(bytes[5] & 0xFF) << 40)
| ((long)(bytes[6] & 0xFF) << 48)
| ((long)(bytes[7] & 0xFF) << 56);
}
public static byte[] int2Bytes(int l){
byte[] bytes = new byte[8] ;
bytes[0] = (byte)l ;
bytes[1] = (byte)(l >> 8) ;
bytes[2] = (byte)(l >> 16) ;
bytes[3] = (byte)(l >> 24) ;
return bytes ;
}
public static int bytes2Int(byte[] bytes){
return ((bytes[0] & 0xFF))
| ((bytes[1] & 0xFF) << 8)
| ((bytes[2] & 0xFF) << 16)
| ((bytes[3] & 0xFF) << 24);
}
public static int byte2Int(byte[] bytes,int offset){
return ((bytes[0 + offset] & 0xFF))
| ((bytes[1 + offset] & 0xFF) << 8)
| ((bytes[2 + offset] & 0xFF) << 16)
| ((bytes[3 + offset] & 0xFF) << 24);
}
public static byte[] zipData(byte[] rawData) {
try {
//
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
zos.putNextEntry(new ZipEntry("one"));
zos.write(rawData);
zos.close();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] unzipData(byte[] zipData) {
try {
//
ByteArrayInputStream bais = new ByteArrayInputStream(zipData);
ZipInputStream zis = new ZipInputStream(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
zis.getNextEntry();
byte[] buf = new byte[1024];
int len = 0 ;
while((len = zis.read(buf)) != -1){
baos.write(buf, 0, len);
}
zis.close();
bais.close();
baos.close();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
界面
[AppleScript]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package cn.itcast.screenbroadcast;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* StudentUI
*/
public class StudentUI extends JFrame{
private JLabel lblIcon ;
public StudentUI(){
init();
}
private void init() {
this.setTitle("yqw广播");
this.setBounds(0, 0, 1366, 768);
this.setLayout(null);
lblIcon = new JLabel();
lblIcon.setBounds(0, 0, 1366, 768);
ImageIcon icon = new ImageIcon("D:/Koala.jpg");
lblIcon.setIcon(icon);
this.add(lblIcon);
this.setVisible(true);
}
public void updateIcon(byte[] dataBytes){
ImageIcon icon = new ImageIcon(dataBytes);
lblIcon.setIcon(icon);
}
}

数据接收类
[AppleScript]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package cn.itcast.screenbroadcast;
import java.io.ByteArrayOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.HashMap;
import java.util.Map;
public class Receiver extends Thread {
private DatagramSocket sock ;
private DatagramPacket pack ;
private byte[] buf = new byte[Teacher.FRAME_UNIT_MAX + 14];
//
private Map<Integer, FrameUnit> unitMap = new HashMap<Integer,FrameUnit>();
//
private StudentUI ui;
public Receiver(StudentUI ui){
try {
this.ui = ui ;
sock = new DatagramSocket(8888);
pack = new DatagramPacket(buf, buf.length);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
try {
long currentFrameId = 0 ;
while(true){
sock.receive(pack);
int len = pack.getLength();
//ת����֡��Ԫ
FrameUnit funit = convertData2FrameUnit(buf,0,len);
long newFrameId = funit.getFrameId();
//ͬһframe
if(newFrameId == currentFrameId){
unitMap.put(funit.getUnitNo(), funit);
}
//��frame
else if(newFrameId > currentFrameId){
currentFrameId = newFrameId ;
unitMap.clear();
unitMap.put(funit.getUnitNo(), funit);
}
processFrame();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ��������unit����
*/
private void processFrame() {
try {
//�õ�Frame���ܵ�unit����
int unitCont = unitMap.values().iterator().next().getUnitCont();
if(unitMap.size() == unitCont ){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for(int i = 0 ; i < unitCont ; i ++){
FrameUnit fu = unitMap.get(i);
fu.getData();
baos.write(fu.getData());
}
unitMap.clear();
//
ui.updateIcon(Util.unzipData(baos.toByteArray()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ת��ÿ��packΪFrameunit
*/
private FrameUnit convertData2FrameUnit(byte[] buf2, int i, int len) {
byte[] data = new byte[len];
System.arraycopy(buf2, 0, data, 0, len);
FrameUnit unit = new FrameUnit();
unit.setFrameId(Util.byte2Long(data));
unit.setUnitCont(data[8]);
unit.setUnitNo(data[9]);
unit.setDateLen(Util.byte2Int(data,10));
byte[] frameUnitBytes = new byte[unit.getDateLen()];
System.arraycopy(buf2, 14, frameUnitBytes, 0, unit.getDateLen());
unit.setData(frameUnitBytes);
return unit;
}
}

app 程序入口
[AppleScript]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
package cn.itcast.screenbroadcast;
/**
* ���ܷ�
*
*/
public class Student {
public static void main(String[] args) {
StudentUI ui = new StudentUI();
new Receiver(ui).start();
}
}

更多技术咨询可关注:gzitcast