Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QCameraImageCapture captures an overexposed image
Forum Updated to NodeBB v4.3 + New Features

QCameraImageCapture captures an overexposed image

Scheduled Pinned Locked Moved Solved General and Desktop
qcameraqwidgetmultimediaqcameraimagecap
4 Posts 2 Posters 563 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fama_lab3d
    wrote on 19 Nov 2022, 18:47 last edited by
    #1

    Hello,
    my application uses a camera to both capture photos and display a real-time view like a video.
    In particular, I'm using QGraphicsVideoItem as a viewfinder for the QCamera.
    The video is rendered perfectly, but whenever I try to capture a single frame via QCameraImageCapture, the image is overexposed, so it's unusable.

    I'll give you the code I use:

    void image_acquisition_service::set_camera(QCameraInfo const &info)
    {
        if(camera)
            camera->stop();
    
        camera = std::make_unique<QCamera>(info);
    }
    
    void image_acquisition_service::acquire_video(QGraphicsVideoItem *viewfinder, QCameraViewfinderSettings const &settings)
    {
        if(camera)
        {
            camera->stop();
            camera->setCaptureMode(QCamera::CaptureVideo);
            camera->setViewfinder(viewfinder);
            camera->setViewfinderSettings(settings);
            camera->start();
        }
    }
    
    QImage image_acquisition_service::acquire_image()
    {
        if(!camera)
            return {};
    
        camera->stop();
    
        QCameraImageCapture imagecapture(camera.get());
        imagecapture.setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
        // Uses the highest resolution
        QList<QSize> resolutions = imagecapture.supportedResolutions();
        std::sort(resolutions.begin(), resolutions.end(), [](QSize const &a, QSize const &b) {
            return a.width() < b.width() && a.height() < b.height();
        });
    
        QImageEncoderSettings imageSettings;
        imageSettings.setResolution(resolutions.back());
        imageSettings.setQuality(QMultimedia::VeryHighQuality);
        imagecapture.setEncodingSettings(imageSettings);
    
        QImage img; // the image we'll capture
    
        // https://stackoverflow.com/questions/3556421/blocked-waiting-for-a-asynchronous-qt-signal
        QEventLoop loop;
        loop.connect(&imagecapture, &QCameraImageCapture::imageAvailable, &loop, [&img, &loop](int, QVideoFrame const &frame) { img = frame.image(); loop.quit(); });
        QObject::connect(camera.get(), &QCamera::errorOccurred, [&loop](QCamera::Error e) { qCritical() << "Error while acquiring the image: " << e; loop.quit(); });
    
        camera->setViewfinder(new QCameraViewfinder()); // use a fake viewfinder or QCamera::unbind will fail when re-acquiring the video
        camera->setCaptureMode(QCamera::CaptureStillImage);
        camera->start();
        camera->searchAndLock();
        imagecapture.capture();
        camera->unlock();
    
        loop.exec();
    
        camera->stop();
    
        return img;
    }
    

    So, my application chooses a camera and calls acquire_video(viewfinder, settings).
    Then, when the users asks for a particular operation, I acquire a frame by calling acquire_image(), giving me an overexposed:
    image.base.png

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 19 Nov 2022, 20:24 last edited by
      #2

      Hi and welcome to devnet,

      I am not convinced about your code logique to acquire a still image. Do you have the same issue if using the official QCamera example ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply 20 Nov 2022, 12:00
      0
      • S SGaist
        19 Nov 2022, 20:24

        Hi and welcome to devnet,

        I am not convinced about your code logique to acquire a still image. Do you have the same issue if using the official QCamera example ?

        F Offline
        F Offline
        fama_lab3d
        wrote on 20 Nov 2022, 12:00 last edited by
        #3

        @SGaist thanks for the welcome :)
        The logic I wrote is based on the code I've found on the docs regarding QCamera/QCameraImageCapture.
        I'll do a test tomorrow morning at work with the code from the QCamera example.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fama_lab3d
          wrote on 21 Nov 2022, 08:53 last edited by
          #4

          @SGaist looking at the QCamera example gave me a lot of info to fix my issue.
          I was using the QCamera wrongly.
          Too many start/stop followed by lock/unlock etc.
          Now the image is captured correctly.

          1 Reply Last reply
          1

          4/4

          21 Nov 2022, 08:53

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved