I feel a bit dumb asking this silly questing. I am trying to switch between 2 states, and the event that determines which state i am in is based on the key i've pressed.
so in this case i want the webcam to start automatically, and when key is pressed it then turns it off.
bool webcam_on()
{
int c = 0;
VideoCapture cap(0);
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
while(c==0){
c=cin.get()
Mat cameraFrame;
cap.read(cameraFrame);
imshow("cam", cameraFrame);
if(c==1)
{
cap.release();
}
if (waitKey(30) >= 0)
break;
}
}
The problem with this method is that cin.get() blocks the video stream.. I wanted something that the webcam turned on until i press some key, which would trigger the release.. How can this be done?
while(true){
if( cap.isOpened() ){ //avoid reading from an unopened device
Mat cameraFrame;
cap.read(cameraFrame);
imshow("cam", cameraFrame);
}
int keypress = waitKey(1); //saving the pressed key
if (keypress == 27) //if pressed Esc key
cap.release(); //turn off camera
if(keypress == 32) //if pressed Espace key
cap.open(0); //turn on camera
}