Raspberry Pi USB drive-free camera error libv4l2: error setting pixformat: Device or resource busy, etc.

The article directories
Learn to record the causes of errors and their solutions

Learning record
I have searched many times on the Internet before, but I haven’t found a solution yet. Suddenly, I came up with a solution.
error

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
VIDEOIO ERROR: libv4l unable to ioctl VIDIOCSPICT

What you see: Sometimes the camera doesn’t work, so you have to plug it in.
Reasons and solutions
I didn’t know why there was such an error. Recently, I read some relevant books and found that I was missing a key line of code.

int main( int argc, const char** argv )
{

	VideoCapture cap; 
	cap.open(0);
    if(!cap.isOpened())
        return -1;
        
    while(1)
    {
        Mat frame;
        cap >> frame; 
        imshow("Video", frame);
        if(waitKey(30) >= 0) 
        	break;
    }
    cap.release();
	return 0;
}

Where, the key sentence cap.release(); is used to release the cap of the camera.
will cause the above error if it is not released.


Read More: