|
1 |
| -package edu.wpi.grip.core.sources; |
2 |
| - |
3 |
| -import edu.wpi.cscore.CameraServerJNI; |
4 |
| -import edu.wpi.cscore.HttpCamera; |
5 |
| -import org.bytedeco.javacpp.Loader; |
6 |
| -import org.bytedeco.javacpp.opencv_core.Mat; |
7 |
| -import org.bytedeco.javacv.Frame; |
8 |
| -import org.bytedeco.javacv.FrameConverter; |
9 |
| -import org.bytedeco.javacv.FrameGrabber; |
10 |
| -import org.bytedeco.javacv.OpenCVFrameConverter; |
11 |
| - |
12 |
| -import java.io.IOException; |
13 |
| -import java.util.concurrent.TimeUnit; |
14 |
| - |
15 |
| -import static com.google.common.base.Preconditions.checkNotNull; |
16 |
| - |
17 |
| -// This is here because FrameGrabber has an exception called Exception which triggers PMD |
18 |
| -@SuppressWarnings({"PMD.AvoidThrowingRawExceptionTypes", "all"}) |
19 |
| -public class CSCameraFrameGrabber extends FrameGrabber { |
20 |
| - |
21 |
| - private static Exception loadingException = null; |
22 |
| - |
23 |
| - private final String url; |
24 |
| - private final double readTimeout; |
25 |
| - |
26 |
| - private Mat decoded = null; |
27 |
| - private FrameConverter<Mat> converter = new OpenCVFrameConverter.ToMat(); |
28 |
| - |
29 |
| - private JavaCvSink javaCvSink; |
30 |
| - private HttpCamera httpCamera; |
31 |
| - |
32 |
| - public CSCameraFrameGrabber(String urlstr, int readTimeout, TimeUnit unit) { |
33 |
| - super(); |
34 |
| - this.url = checkNotNull(urlstr, "urlstr"); |
35 |
| - this.readTimeout = TimeUnit.MILLISECONDS.convert(readTimeout, unit) / 1000.0; |
36 |
| - } |
37 |
| - |
38 |
| - public static void tryLoad() throws Exception { |
39 |
| - if (loadingException != null) { |
40 |
| - throw loadingException; |
41 |
| - } else { |
42 |
| - try { |
43 |
| - Loader.load(org.bytedeco.javacpp.opencv_highgui.class); |
44 |
| - CameraServerJNI.Helper.setExtractOnStaticLoad(false); |
45 |
| - CameraServerJNI.forceLoad(); |
46 |
| - } catch (Throwable t) { |
47 |
| - throw loadingException = new Exception("Failed to load " + CSCameraFrameGrabber.class, t); |
48 |
| - } |
49 |
| - } |
50 |
| - } |
51 |
| - |
52 |
| - @Override |
53 |
| - public void start() throws Exception { |
54 |
| - javaCvSink = new JavaCvSink("InputCamera"); |
55 |
| - httpCamera = new HttpCamera("InputHttp", url); |
56 |
| - javaCvSink.setSource(httpCamera); |
57 |
| - } |
58 |
| - |
59 |
| - public void stop() throws Exception { |
60 |
| - if (javaCvSink != null) { |
61 |
| - javaCvSink.close(); |
62 |
| - } |
63 |
| - |
64 |
| - if (httpCamera != null) { |
65 |
| - httpCamera.close(); |
66 |
| - } |
67 |
| - |
68 |
| - if (decoded != null) { |
69 |
| - decoded.close(); |
70 |
| - } |
71 |
| - } |
72 |
| - |
73 |
| - @Override |
74 |
| - public void trigger() throws Exception { |
75 |
| - } |
76 |
| - |
77 |
| - @Override |
78 |
| - public Frame grab() throws Exception { |
79 |
| - try { |
80 |
| - if (decoded == null) { |
81 |
| - decoded = new Mat(); |
82 |
| - } |
83 |
| - long frameTime = javaCvSink.grabFrame(decoded, readTimeout); |
84 |
| - if (frameTime > 0) { |
85 |
| - return converter.convert(decoded); |
86 |
| - } else { |
87 |
| - throw new IOException("Frame not read: " + frameTime); |
88 |
| - } |
89 |
| - } catch (IOException e) { |
90 |
| - throw new Exception(e.getMessage(), e); |
91 |
| - } |
92 |
| - } |
93 |
| - |
94 |
| - @Override |
95 |
| - public void release() throws Exception { |
96 |
| - } |
97 |
| -} |
| 1 | +package edu.wpi.grip.core.sources; |
| 2 | + |
| 3 | +import edu.wpi.cscore.CameraServerJNI; |
| 4 | +import edu.wpi.cscore.HttpCamera; |
| 5 | +import org.bytedeco.javacpp.Loader; |
| 6 | +import org.bytedeco.javacpp.opencv_core.Mat; |
| 7 | +import org.bytedeco.javacv.Frame; |
| 8 | +import org.bytedeco.javacv.FrameConverter; |
| 9 | +import org.bytedeco.javacv.FrameGrabber; |
| 10 | +import org.bytedeco.javacv.OpenCVFrameConverter; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | + |
| 15 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 16 | + |
| 17 | +// This is here because FrameGrabber has an exception called Exception which triggers PMD |
| 18 | +@SuppressWarnings({"PMD.AvoidThrowingRawExceptionTypes", "all"}) |
| 19 | +public class CSHttpCameraFrameGrabber extends FrameGrabber { |
| 20 | + |
| 21 | + private static Exception loadingException = null; |
| 22 | + |
| 23 | + private final String url; |
| 24 | + private final double readTimeout; |
| 25 | + |
| 26 | + private Mat decoded = null; |
| 27 | + private FrameConverter<Mat> converter = new OpenCVFrameConverter.ToMat(); |
| 28 | + |
| 29 | + private JavaCvSink javaCvSink; |
| 30 | + private HttpCamera httpCamera; |
| 31 | + |
| 32 | + public CSHttpCameraFrameGrabber(String urlstr, int readTimeout, TimeUnit unit) { |
| 33 | + super(); |
| 34 | + this.url = checkNotNull(urlstr, "urlstr"); |
| 35 | + this.readTimeout = TimeUnit.MILLISECONDS.convert(readTimeout, unit) / 1000.0; |
| 36 | + } |
| 37 | + |
| 38 | + public static void tryLoad() throws Exception { |
| 39 | + if (loadingException != null) { |
| 40 | + throw loadingException; |
| 41 | + } else { |
| 42 | + try { |
| 43 | + Loader.load(org.bytedeco.javacpp.opencv_highgui.class); |
| 44 | + CameraServerJNI.Helper.setExtractOnStaticLoad(false); |
| 45 | + CameraServerJNI.forceLoad(); |
| 46 | + } catch (Throwable t) { |
| 47 | + throw loadingException = new Exception("Failed to load " + CSHttpCameraFrameGrabber.class, t); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public HttpCamera getCamera() { |
| 53 | + return this.httpCamera; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void start() throws Exception { |
| 58 | + javaCvSink = new JavaCvSink("InputCamera"); |
| 59 | + httpCamera = new HttpCamera("InputHttp", url); |
| 60 | + javaCvSink.setSource(httpCamera); |
| 61 | + } |
| 62 | + |
| 63 | + public void stop() throws Exception { |
| 64 | + if (javaCvSink != null) { |
| 65 | + javaCvSink.close(); |
| 66 | + } |
| 67 | + |
| 68 | + if (httpCamera != null) { |
| 69 | + httpCamera.close(); |
| 70 | + } |
| 71 | + |
| 72 | + if (decoded != null) { |
| 73 | + decoded.close(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void trigger() throws Exception { |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Frame grab() throws Exception { |
| 83 | + try { |
| 84 | + if (decoded == null) { |
| 85 | + decoded = new Mat(); |
| 86 | + } |
| 87 | + long frameTime = javaCvSink.grabFrame(decoded, readTimeout); |
| 88 | + if (frameTime > 0) { |
| 89 | + return converter.convert(decoded); |
| 90 | + } else { |
| 91 | + throw new IOException("Frame not read: " + frameTime); |
| 92 | + } |
| 93 | + } catch (IOException e) { |
| 94 | + throw new Exception(e.getMessage(), e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void release() throws Exception { |
| 100 | + } |
| 101 | +} |
0 commit comments