@Override
  protected void setup() {
    // open camera and set resolution
    cam = new VideoCapture();
    cam.open(camId);
    cam.set(Videoio.CV_CAP_PROP_FRAME_WIDTH, camWidth);
    cam.set(Videoio.CV_CAP_PROP_FRAME_HEIGHT, camHeight);

    // check if camera is open
    if (!cam.isOpened()) {
      throw new IllegalStateException("Couldn't open cam: " + camId);
    }
  }
 @Override
 protected void task() {
   if (count++ > 100) {
     System.gc();
     count = 0;
   }
   Mat buffer = new Mat();
   // grab a frame
   cam.grab();
   // save the grabbed frame in the buffer
   cam.retrieve(buffer);
   frame = buffer;
   // take a time stamp for latency measurement
   timeStamp = System.nanoTime();
 }
 @Override
 protected void shutdown() {
   System.out.println("Shutdown cam " + camId);
   cam.release();
   while (cam.isOpened()) {}
 }