-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCameraClient.java
More file actions
171 lines (150 loc) · 6 KB
/
CameraClient.java
File metadata and controls
171 lines (150 loc) · 6 KB
1
2
3
4
5
6
7
8
9
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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CameraClient {
private static final String HOST = "localhost"; // change for actual IP
private static final int PORT = 6000;
private static int type = 0;
public static String videoPath = "";
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGUI());
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Camera Client");
JLabel label = new JLabel("aaaaaa");
frame.add(label);
frame.setSize(640, 580);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton startButton = new JButton("Start Camera");
JButton startVideo = new JButton("Start Video");
JButton stopButton = new JButton("Notification");
JButton locationButton = new JButton("Location");
JButton playButton = new JButton("Play Video");
JButton autoCameraButton = new JButton("Auto Camera(5s)");
JButton shareMsgButton = new JButton("Share Message");
JButton callPhoneButton = new JButton("Call phone");
JButton sendSmsButton = new JButton("Send sms");
JButton flashOnButton = new JButton("Flash on");
JButton flashOffButton = new JButton("Flash off");
startButton.addActionListener(e -> {
sendCommand("action=ACTION_IMAGE_CAPTURE");
type = 1;
});
startVideo.addActionListener(e -> {
sendCommand("action=ACTION_VIDEO_CAPTURE");
type = 2;
});
stopButton.addActionListener(e -> {
sendCommand("action=ACTION_CREATE_NOTIFICATION&name=test&description=test22");
type = 3;
});
locationButton.addActionListener(e -> {
sendCommand("action=ACTION_GET_LOCATION");
type = 4;
});
playButton.addActionListener(e -> {
sendCommand("action=ACTION_PLAY_VIDEO&video_url=" + videoPath);
type = 5;
});
autoCameraButton.addActionListener(e -> {
sendCommand("action=ACTION_IMAGE_CAPTURE&auto_time=5000");
type = 6;
});
shareMsgButton.addActionListener(e -> {
sendCommand("action=ACTION_SHARE_MESSAGE&title=Test&body=share message to app");
type = 7;
});
callPhoneButton.addActionListener(e -> {
sendCommand("action=ACTION_CALL_PHONE&to=+1234567890");
type = 8;
});
sendSmsButton.addActionListener(e -> {
sendCommand("action=ACTION_SEND_SMS&to=+1234567890&body=send msg");
type = 9;
});
flashOnButton.addActionListener(e -> {
sendCommand("action=ACTION_CAMERA_FLASH&type=1");
type = 10;
});
flashOffButton.addActionListener(e -> {
sendCommand("action=ACTION_CAMERA_FLASH&type=0");
type = 11;
});
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 3)); // 每行3个按钮,行数自动计算
panel.add(startButton);
panel.add(startVideo);
panel.add(stopButton);
panel.add(locationButton);
panel.add(playButton);
panel.add(autoCameraButton);
panel.add(shareMsgButton);
panel.add(callPhoneButton);
panel.add(sendSmsButton);
panel.add(flashOnButton);
panel.add(flashOffButton);
frame.add(panel, BorderLayout.SOUTH);
frame.setVisible(true);
new Thread(() -> {
try {
MulticastSocket socket = new MulticastSocket(5000);
InetAddress group = InetAddress.getByName("224.0.0.1");
socket.joinGroup(group);
byte[] buffer = new byte[65536];
while (true) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
if (type == 1 || type == 6) {
ByteArrayInputStream bis = new ByteArrayInputStream(packet.getData());
BufferedImage image = ImageIO.read(bis);
if (image != null) {
System.out.println("Received image: " + image.getWidth() + "x" + image.getHeight());
updateLabelIcon(label, image);
}
} else if (type == 2 || type == 3 || type == 4) {
SwingUtilities.invokeLater(() -> {
if (type == 2) {
videoPath = new String(packet.getData(), StandardCharsets.UTF_8);
}
label.setText(new String(packet.getData(), StandardCharsets.UTF_8));
label.repaint();
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
private static void sendCommand(String command) {
try (Socket socket = new Socket(HOST, PORT);
OutputStream out = socket.getOutputStream()) {
out.write(command.getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void updateLabelIcon(JLabel label, BufferedImage image) {
SwingUtilities.invokeLater(() -> {
label.setText("22222");
label.setIcon(new ImageIcon(image));
label.repaint();
});
}
}