BowlerKernel
OpenCVManager.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.opencv;
2 
3 import java.util.ArrayList;
4 
5 import org.opencv.videoio.VideoCapture;
6 
7 import com.neuronrobotics.sdk.common.DeviceManager;
8 import com.neuronrobotics.sdk.common.NonBowlerDevice;
9 
10 public class OpenCVManager extends NonBowlerDevice {
11  private static boolean libLoaded = false;
12  private int camerIndex;
13  private VideoCapture capture;
14  static {
15  try {
16  nu.pattern.OpenCV.loadLocally();
17  libLoaded = true;
18  } catch (Throwable t) {
19  t.printStackTrace();
20  }
21  }
22 
23  private OpenCVManager(int camerIndex) {
24  this.camerIndex = camerIndex;
25  if (!libLoaded)
26  throw new RuntimeException("OpenCV library failed to load");
27  connect();
28  }
29 
30  public static OpenCVManager get(int index) {
31  return (OpenCVManager)DeviceManager.getSpecificDevice("opencv_"+index, () -> new OpenCVManager(index));
32 
33  }
34 
35  @Override
36  public void disconnectDeviceImp() {
37  if (getCapture() != null)
38  getCapture().release();
39  }
40 
41  @Override
42  public boolean connectDeviceImp() {
43  try {
44  setCapture(new VideoCapture(camerIndex));
45  getCapture().open(camerIndex);
46  return true;
47  } catch (Throwable t) {
48  t.printStackTrace();
49  }
50  return false;
51  }
52 
53  @Override
54  public ArrayList<String> getNamespacesImp() {
55  // TODO Auto-generated method stub
56  return null;
57  }
58 
62  public VideoCapture getCapture() {
63  return capture;
64  }
65 
69  private void setCapture(VideoCapture capture) {
70  this.capture = capture;
71  }
72 
73 }
static Object getSpecificDevice(String name, IDeviceProvider provider)