VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
The above snippet will fail if invalid parameter is passed to the constructor.
The error returned exits the program.
How do I validate the camera ID prior to executing the VideoCapture?
I need to control the program flow and NOT exit the program via VideoCapture class.
Revision
OK , I have better understanding how to access video camera, however,
I still need some more assistance.
Here is my my debugging code
for (int iVideoID = 0; iVideoID != 9; iVideoID ++) {
if (cap.open(iVideoID)) {
cerr << "success - opened video cam ID " << iVideoID
<< endl;
//return 15;
break; // return -1;
} else {
cerr << "Cannot open the video cam " << iVideoID
<< endl;
if(iVideoID != 8)
cerr << " try next ID " << endl;
else
{
cerr << "Failed to access any video camera " << iVideoID
<< endl;
ifdef TRACE
cerr << "Failed to access any video camera " << iVideoID
<< endl;
cout << __func__ << endl;
cerr << __LINE__ << endl;
cerr << "EXIT "<< endl;
return 42 ;
endif
It does do what I want, but...
When there are NO USB cameras on the system , it correctly exists after ID is > 8 . No problem there.
But when there is a USB camera and it is not accessible I get ton of library errors.
I like to gracefully exit the application instead of collecting all these however valid errors.
Here is a copy of my outputs:
int main(int argc, char **argv)
main
libv4l2: error setting pixformat: Device or resource busy
VIDEOIO ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
main
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
VIDEOIO ERROR: libv4l unable to ioctl VIDIOCSPICT
Cannot open the video cam 0
try next ID
VIDEOIO ERROR: V4L: index 1 is not correct!
Cannot open the video cam 1
try next ID
VIDEOIO ERROR: V4L: index 2 is not correct!
Cannot open the video cam 2
try next ID
VIDEOIO ERROR: V4L: index 3 is not correct!
Cannot open the video cam 3
try next ID
VIDEOIO ERROR: V4L: index 4 is not correct!
Cannot open the video cam 4
try next ID
VIDEOIO ERROR: V4L: index 5 is not correct!
Cannot open the video cam 5
try next ID
VIDEOIO ERROR: V4L: index 6 is not correct!
Basically - how do I access / process the lib4l2 errors ?
I did some search on this and found all of such problems are blamed on application.
However, one discussion was about "USB bandwidth" or lack of it .
Please NOTE that when my code works , when I can access the camera , it works.
So if the USB bandwidth is the issue , I just want to recover from it. Essentially looking for a function which would return the error , not just true / false as cap.open(iVideoID) does.
As it stands - "open" just sort of hides the errors and returns false, which it suppose to do.
I hope I am making sense.
Any help will be appreciated.
PS
I have added another copy of the output because I Cannot figure out how my code got formated in this post and how to do same for the output. Sorry it is so messy.
int main(int argc, char **argv)
main
libv4l2: error setting pixformat: Device or resource busy
VIDEOIO ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
main
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
VIDEOIO ERROR: libv4l unable to ioctl VIDIOCSPICT