QCameraImageCapture captures an overexposed image
-
Hello,
my application uses a camera to both capture photos and display a real-time view like a video.
In particular, I'm usingQGraphicsVideoItem
as a viewfinder for the QCamera.
The video is rendered perfectly, but whenever I try to capture a single frame viaQCameraImageCapture
, 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 callingacquire_image()
, giving me an overexposed:
image. -
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 ?
-
@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.