{"record":{"id":"aaa65679a6844a72","repo":"Yalantis/uCrop","slug":"load-camera-failed-to-initialize-camera-u","errorCode":null,"errorMessage":"load_camera(): Failed to initialize camera #%u.","messagePattern":"load_camera\\(\\): Failed to initialize camera #%u\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":60103,"sourceCode":"      static unsigned int captures_w[64], captures_h[64];\n      if (release_camera) {\n        cimg::mutex(9);\n        if (captures[camera_index]) captures[camera_index]->release();\n        delete captures[camera_index];\n        captures[camera_index] = 0;\n        captures_w[camera_index] = captures_h[camera_index] = 0;\n        cimg::mutex(9,0);\n        return *this;\n      }\n      if (!captures[camera_index]) {\n        cimg::mutex(9);\n        captures[camera_index] = new cv::VideoCapture(camera_index);\n        captures_w[camera_index] = captures_h[camera_index] = 0;\n        if (!captures[camera_index]->isOpened()) {\n          delete captures[camera_index];\n          captures[camera_index] = 0;\n          cimg::mutex(9,0);\n          throw CImgIOException(_cimg_instance\n                                \"load_camera(): Failed to initialize camera #%u.\",\n                                cimg_instance,\n                                camera_index);\n        }\n        cimg::mutex(9,0);\n      }\n      cimg::mutex(9);\n      if (capture_width!=captures_w[camera_index]) {\n        captures[camera_index]->set(_cimg_cap_prop_frame_width,capture_width);\n        captures_w[camera_index] = capture_width;\n      }\n      if (capture_height!=captures_h[camera_index]) {\n        captures[camera_index]->set(_cimg_cap_prop_frame_height,capture_height);\n        captures_h[camera_index] = capture_height;\n      }\n      for (unsigned int i = 0; i<skip_frames; ++i) captures[camera_index]->grab();\n      cv::Mat cvimg;\n      captures[camera_index]->read(cvimg);","sourceCodeStart":60085,"sourceCodeEnd":60121,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L60085-L60121","documentation":"After creating a cv::VideoCapture(camera_index), CImg checks isOpened(); if the camera could not be opened the handle is deleted and a CImgIOException is thrown. This means OpenCV could not initialize the capture device at that index.","triggerScenarios":"load_camera(n) where no device exists at index n, the device is in use by another process, permissions block access (/dev/video* not readable), or the driver/backend fails to initialize.","commonSituations":"Running in a container/VM without webcam passthrough; camera already held by another app (Zoom, browser); Android/CI environments with no camera hardware; wrong camera index.","solutions":["Verify the camera index is valid and the device exists (ls /dev/video*; test with a v4l2 tool or a minimal OpenCV script).","Close other applications using the camera, or release previous captures before reopening.","Fix device permissions (add user to 'video' group) or enable webcam passthrough in the container/VM.","Check that the OpenCV build includes the needed video backend (V4L2, MSMF, AVFoundation)."],"exampleFix":"// before: unconditional open\nimg.load_camera(cameraId);\n// after\ntry { img.load_camera(cameraId); }\ncatch (CImgIOException&) { std::cerr << \"camera \" << cameraId << \" unavailable\"; }\n// and pre-check: cv::VideoCapture cap(cameraId); cap.isOpened()","handlingStrategy":"try-catch","validationCode":"// pre-check with OpenCV directly before using CImg\ncv::VideoCapture probe(idx);\nbool ok = probe.isOpened();\nprobe.release();","typeGuard":null,"tryCatchPattern":"try { img.load_camera(idx); }\ncatch (CImgIOException& e) { fprintf(stderr, \"camera %u unavailable: %s\", idx, e.what()); }","preventionTips":["Probe isOpened() before committing to load_camera","Release cameras deterministically; avoid double-open of the same index","Grant video-group permissions and enable webcam passthrough in containers/VMs","Handle environments without cameras (CI, Android emulators)"],"tags":["cimg","opencv","camera","device-init"],"backgroundTag":"device-init-failed","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}