Yalantis/uCrop · error · CImgIOException

load_camera(): Failed to retrieve a %ux%u frame from camera…

Error message

load_camera(): Failed to retrieve a %ux%u frame from camera #%u.

What it means

CImg opens the camera and reads a frame with captures[camera_index]->read(cvimg); if the resulting cv::Mat is empty, no frame could be retrieved at the requested resolution, the camera is released and CImgIOException is thrown.

Solutions

  1. Pass capture_width=0 and capture_height=0 to let the camera use its default resolution instead of forcing an unsupported one.
  2. Retry the capture — transient failures are common right after opening; add a few attempts with small sleeps.
  3. Verify the device is still connected and stable (cables, USB power management).
  4. Lower the requested resolution or use fewer simultaneous cameras to free USB bandwidth.

Example fix

// before: forces a possibly unsupported resolution
img.load_camera(0, 3840, 2160);
// after
img.load_camera(0, 0, 0); // camera default
// or retry a few times
for (int i = 0; i < 5 && img.is_empty(); ++i) { cimg::sleep(100); img.load_camera(0,0,0,false); }
Defensive patterns

Strategy: retry

Validate before calling

// verify the device can deliver the requested format before forcing resolution
// prefer load_camera(idx) (0,0 defaults) unless the resolution is known-supported

Try / catch

for (int attempt = 0; attempt < 5; ++attempt) {
  try { img.load_camera(idx, w, h); break; }
  catch (CImgIOException&) { cimg::sleep(200); }
}

Prevention

When it happens

Trigger: load_camera() with capture_width/capture_height the device cannot deliver and the backend returns an empty Mat; device disconnected/errored after successful open; first grab returned nothing (driver still warming up).

Common situations: Requesting unsupported resolutions (e.g. 4K on a 720p webcam with a strict backend); USB bandwidth exhausted with multiple cameras; camera unplugged mid-session; broken driver returning empty frames intermittently.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


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

Appendix: source

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

        }
        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);
      if (cvimg.empty()) {
        cimg::mutex(9,0);
        load_camera(camera_index,0,0,0,true); // Release camera
        throw CImgIOException(_cimg_instance
                              "load_camera(): Failed to retrieve a %ux%u frame from camera #%u.",
                              cimg_instance,
                              capture_width,capture_height,camera_index);
      } else _cvmat2cimg(cvimg).move_to(*this);
      cimg::mutex(9,0);
      return *this;
#else
      cimg::unused(camera_index,skip_frames,release_camera,capture_width,capture_height);
      throw CImgIOException(_cimg_instance
                            "load_camera(): This function requires features from the OpenCV library "
                            "('-Dcimg_use_opencv' must be defined).",
                            cimg_instance);
#endif
    }

    //! Load image from a camera stream, using OpenCV \newinstance.
    static CImg<T> get_load_camera(const unsigned int camera_index=0,
                                   const unsigned int capture_width=0, const unsigned int capture_height=0,

View on GitHub (pinned to f788b534b4)