-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
142 lines (71 loc) · 2.63 KB
/
main.java
File metadata and controls
142 lines (71 loc) · 2.63 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
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.imageio.ImageIO;
public class Camera {
private static final int WIDTH = 640;
private static final int HEIGHT = 480;
private ExecutorService executorService;
private BufferedImage image;
public Camera() {
executorService = Executors.newSingleThreadExecutor();
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
}
public void start() {
executorService.submit(new Runnable() {
@Override
public void run() {
while (true) {
try {
takePicture();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
public void stop() {
executorService.shutdownNow();
}
public BufferedImage getImage() {
return image;
}
private void takePicture() throws IOException {
// Get the current frame from the camera
byte[] data = getFrame();
// Decode the frame into a BufferedImage
image = ImageIO.read(new ByteArrayInputStream(data));
}
private byte[] getFrame() throws IOException {
// Open a connection to the camera
java.net.Socket socket = new java.net.Socket("192.168.1.100", 8080);
// Send a request to the camera to take a picture
String request = "GET / HTTP/1.1\r\n" +
"Host: 192.168.1.100:8080\r\n" +
"Connection: close\r\n" +
"\r\n";
socket.getOutputStream().write(request.getBytes());
// Read the response from the camera
byte[] response = new byte[1024];
int bytesRead = socket.getInputStream().read(response);
// Close the connection
socket.close();
// Return the response
return Arrays.copyOf(response, bytesRead);
}
public static void main(String[] args) throws IOException {
Camera camera = new Camera();
camera.start();
// Save the image to a file
ImageIO.write(camera.getImage(), "jpg", new File("image.jpg"));
// Display the image on the screen
javax.swing.JFrame frame = new javax.swing.JFrame();
javax.swing.JLabel label = new javax.swing.JLabel(new javax.swing.ImageIcon(camera.getImage()));
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
}