stop and restart the video grabber

Hi,

I have a timer that after a certain amount of millis should close the videograbber and then reinitialize. At the moment it just crashed and pauses it. Any ideas on how to make it close and then re-open?

  
 float timer2= ofGetElapsedTimeMillis()-time;  
    if (timer2>=endtime2){  
          
        bTimer2Reached=true;  
          
    }  
      
    if (bTimer2Reached) {  
        cam.close();  
        cam.initGrabber(w, h);  
         cam.update();  
        if (cam.isFrameNew()){  
            colorImg.setFromPixels(cam.getPixels(),w,h);  
        }  
       // grayDiff.absDiff(grayBg, grayimg);  
         
       cout << "timer2 reached " << endl;  
  
    }  
      
      
    }  

It’s probably going to take a few frames to reinitialize the camera, so you can check to see if the camera has initialized correctly with:

  
if(cam.initGrabber(w, h)) {  

There’s a good amount of clearing memory and letting go of device handles that goes on when you close the camera so reinitializing it right away seems kind of odd. Can you explain what you’re trying to do?

I have a program that is going to run for quite a bit of time so I would like to give it a break intermittently.

Well if you’re going to give it a break turning it off and then turning it back on right away might not be the best way to do that :slight_smile:

  
if (timer2>=endtime2){  
          
        bTimer2Reached=true;  
          
    }  
      
    
      
       while(bTimer2Reached) {  
           cam.close();  
         break;  
            
           cam.initGrabber(w, h);  
             
           }  
  

Still crashes but maybe this is getting there? Any other ideas? please:)

This works fine for me:

  
  
void testApp::update()  
{  
	vidGrabber.update();  
	if(vidGrabber.isFrameNew()) {  
		// something  
	}  
}  
  
void testApp::mousePressed(int x, int y, int button){  
  
    if(running) {  
        vidGrabber.close();  
        running = false;  
    } else {  
        vidGrabber.initGrabber(320, 240);  
        running = true;  
    }  
}  

It might be down to a timing problem on your system/video driver?