forked from WPIRoboticsProjects/GRIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPCameraFrameGrabber.java
228 lines (198 loc) · 7.83 KB
/
IPCameraFrameGrabber.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (C) 2013 Greg Perry
* Modified for use in GRIP to reduce heap allocations
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
* the Free Software Foundation (subject to the "Classpath" exception),
* either version 2, or any later version (collectively, the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/
* http://www.gnu.org/software/classpath/license.html
*
* or as provided in the LICENSE.txt file that accompanied this code.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package edu.wpi.grip.core.sources;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameConverter;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.OpenCVFrameConverter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvDecodeImage;
// This is here because FrameGrabber has an exception called Exception which triggers PMD
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
public class IPCameraFrameGrabber extends FrameGrabber {
/*
* excellent reference - http://www.jpegcameras.com/ foscam url
* http://host/videostream.cgi?user=username&pwd=password
* http://192.168.0.59:60/videostream.cgi?user=admin&pwd=password android ip
* cam http://192.168.0.57:8080/videofeed
*/
private static Exception loadingException = null;
public static void tryLoad() throws Exception {
if (loadingException != null) {
throw loadingException;
} else {
try {
Loader.load(org.bytedeco.javacpp.opencv_highgui.class);
} catch (Throwable t) {
throw loadingException = new Exception("Failed to load " + IPCameraFrameGrabber.class, t);
}
}
}
private URL url;
private URLConnection connection;
private DataInputStream input;
private byte[] pixelBuffer = new byte[1024];
private Map<String, List<String>> headerfields;
private String boundryKey;
private IplImage decoded = null;
private FrameConverter<IplImage> converter = new OpenCVFrameConverter.ToIplImage();
public IPCameraFrameGrabber(String urlstr) throws MalformedURLException {
url = new URL(urlstr);
}
@Override
public void start() throws Exception {
try {
connection = url.openConnection();
headerfields = connection.getHeaderFields();
if (headerfields.containsKey("Content-Type")) {
List<String> ct = headerfields.get("Content-Type");
for (int i = 0; i < ct.size(); ++i) {
String key = ct.get(i);
int j = key.indexOf("boundary=");
if (j != -1) {
boundryKey = key.substring(j + 9); // FIXME << fragile
}
}
}
input = new DataInputStream(connection.getInputStream());
} catch (IOException e) {
// Make sure we rethrow the IO exception https://github.com/bytedeco/javacv/pull/300
throw new Exception(e.getMessage(), e);
}
}
@Override
public void stop() throws Exception {
// Our fix. This ensures that restart doesn't null pointer.
// https://github.com/bytedeco/javacv/issues/299
if(input != null) {
try {
input.close();
input = null;
connection = null;
// Don't set url to null
// https://github.com/bytedeco/javacv/pull/300
if (decoded != null) {
cvReleaseImage(decoded);
}
} catch (IOException e) {
throw new Exception(e.getMessage(), e);
}
}
}
@Override
public void trigger() throws Exception {
}
@Override
public Frame grab() throws Exception {
try {
byte[] b = readImage();
CvMat mat = cvMat(1, b.length, CV_8UC1, new BytePointer(b));
if (decoded != null) {
cvReleaseImage(decoded);
}
return converter.convert(decoded = cvDecodeImage(mat));
} catch (IOException e) {
throw new Exception(e.getMessage(), e);
}
}
public BufferedImage grabBufferedImage() throws IOException {
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(readImage()));
return bi;
}
byte[] readImage() throws IOException {
StringBuffer sb = new StringBuffer();
int c;
// read http subheader
while ((c = input.read()) != -1) {
if (c > 0) {
sb.append((char) c);
if (c == 13) {
sb.append((char) input.read());// '10'+
c = input.read();
sb.append((char) c);
if (c == 13) {
sb.append((char) input.read());// '10'
break; // done with subheader
}
}
}
}
// find embedded jpeg in stream
final String subheader = sb.toString();
//log.debug(subheader);
// if (boundryKey == null)
// {
// Yay! - server was nice and sent content length
int c0 = subheader.indexOf("Content-Length: ");
int c1 = subheader.indexOf('\r', c0);
if (c0 < 0) {
//log.info("no content length returning null");
throw new EOFException("The camera stream ended unexpectedly");
}
c0 += 16;
final int contentLength = Integer.parseInt(subheader.substring(c0, c1).trim());
//log.debug("Content-Length: " + contentLength);
// adaptive size - careful - don't want a 2G jpeg
ensureBufferCapacity(contentLength);
input.readFully(pixelBuffer, 0, contentLength);
input.read();// \r
input.read();// \n
input.read();// \r
input.read();// \n
return pixelBuffer;
}
/**
* Grow the pixel buffer if necessary. Using this method instead of allocating a new buffer every time a frame
* is grabbed improves performance by reducing the frequency of garbage collections. In a simple test, the
* unmodified version of IPCameraFrameGrabber caused about 200MB of allocations within 13 seconds. In this
* version, almost no additional heap space is typically allocated per frame.
* <p>
* The downside to this is that the returned frames can't really be modified, so this probably won't go upstream,
* but it's useful for us because in GRIP we don't operate on images in-place.
*/
private void ensureBufferCapacity(int desiredCapacity) {
int capacity = pixelBuffer.length;
while (capacity < desiredCapacity) {
capacity *= 2;
}
if (capacity > pixelBuffer.length) {
pixelBuffer = new byte[capacity];
}
}
@Override
public void release() throws Exception {
}
}