Yalantis/uCrop · error · CImgIOException

load_camera(): Failed to initialize camera #%u.

Error message

load_camera(): Failed to initialize camera #%u.

What it means

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.

Solutions

  1. Verify the camera index is valid and the device exists (ls /dev/video*; test with a v4l2 tool or a minimal OpenCV script).
  2. Close other applications using the camera, or release previous captures before reopening.
  3. Fix device permissions (add user to 'video' group) or enable webcam passthrough in the container/VM.
  4. Check that the OpenCV build includes the needed video backend (V4L2, MSMF, AVFoundation).

Example fix

// before: unconditional open
img.load_camera(cameraId);
// after
try { img.load_camera(cameraId); }
catch (CImgIOException&) { std::cerr << "camera " << cameraId << " unavailable"; }
// and pre-check: cv::VideoCapture cap(cameraId); cap.isOpened()
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-check with OpenCV directly before using CImg
cv::VideoCapture probe(idx);
bool ok = probe.isOpened();
probe.release();

Try / catch

try { img.load_camera(idx); }
catch (CImgIOException& e) { fprintf(stderr, "camera %u unavailable: %s", idx, e.what()); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/aaa65679a6844a72. Report an issue: GitHub.

Appendix: source

Thrown at ucrop/src/main/jni/CImg.h:60103

      static unsigned int captures_w[64], captures_h[64];
      if (release_camera) {
        cimg::mutex(9);
        if (captures[camera_index]) captures[camera_index]->release();
        delete captures[camera_index];
        captures[camera_index] = 0;
        captures_w[camera_index] = captures_h[camera_index] = 0;
        cimg::mutex(9,0);
        return *this;
      }
      if (!captures[camera_index]) {
        cimg::mutex(9);
        captures[camera_index] = new cv::VideoCapture(camera_index);
        captures_w[camera_index] = captures_h[camera_index] = 0;
        if (!captures[camera_index]->isOpened()) {
          delete captures[camera_index];
          captures[camera_index] = 0;
          cimg::mutex(9,0);
          throw CImgIOException(_cimg_instance
                                "load_camera(): Failed to initialize camera #%u.",
                                cimg_instance,
                                camera_index);
        }
        cimg::mutex(9,0);
      }
      cimg::mutex(9);
      if (capture_width!=captures_w[camera_index]) {
        captures[camera_index]->set(_cimg_cap_prop_frame_width,capture_width);
        captures_w[camera_index] = capture_width;
      }
      if (capture_height!=captures_h[camera_index]) {
        captures[camera_index]->set(_cimg_cap_prop_frame_height,capture_height);
        captures_h[camera_index] = capture_height;
      }
      for (unsigned int i = 0; i<skip_frames; ++i) captures[camera_index]->grab();
      cv::Mat cvimg;
      captures[camera_index]->read(cvimg);

View on GitHub (pinned to f788b534b4)