Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QCameraImageCapture resource error on imageSave



  • I'm currently trying to capture a still image with a QCamera Widget, but whenever i try to run the capture() function of QCameraImageCapture, it errors and gives me a QCameraImageCapture::ResourceError "Could not save image to file."

    Ive tried my hardest to copy directly from the QCamera example from QT, whilst cutting out the video recording part of it as i only need still images.

    This is my widget's constructor

    	connect(saveImageButton, SIGNAL(pressed()), this, SLOT(saveImagePressed()));
    	
    
    	if (!checkCameraAvailability()) close();
    
    	QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    
    	availableCameras->setFocusPolicy(Qt::NoFocus);
    
    	QPalette p = palette();
    	p.setBrush(QPalette::Highlight, QBrush(Qt::white));
    	p.setBrush(QPalette::HighlightedText, QBrush(Qt::black));
    	availableCameras->setPalette(p);
    
    	foreach(const QCameraInfo &ci, cameras) {
    		QCheckBox *cam = new QCheckBox(ci.description());
    		QListWidgetItem *lwi = new QListWidgetItem();
    		availableCameras->addItem(lwi);
    		availableCameras->setItemWidget(lwi, cam);
    	}
    
    	m_camera.reset(new QCamera(QCameraInfo::availableCameras()[0]));
    	
    	connect(m_camera.data(), QOverload<QCamera::Error>::of(&QCamera::error), this, &WebCamDialog::displayCameraError);
    	
    	m_camera->setViewfinder(ui->viewFinder);
    	m_camera->viewfinderSettings().setResolution(QSize(400, 400));
    	qDebug() << m_camera->viewfinderSettings().resolution();
    
    	m_camera->setCaptureMode(QCamera::CaptureStillImage);
    
    	m_imageCapture.reset(new QCameraImageCapture(m_camera.data()));
    
    	connect(m_imageCapture.data(), QOverload<int, QCameraImageCapture::Error, const QString &>::of(&QCameraImageCapture::error),
    		this, &WebCamDialog::displayCaptureError);
    
    	connect(m_imageCapture.data(), SIGNAL(imageCaptured(int, const QImage)), this, SLOT(imageCaptured(int, const QImage)));
    	connect(m_imageCapture.data(), SIGNAL(imageSaved(int, const QString)), this, SLOT(imageSaved(int, const QString)));
    
    
    	m_camera->start();
    

    this is my capture function when a button is pressed

    void WebCamDialog::saveImagePressed() {
    	qDebug() << m_imageCapture->isReadyForCapture();
    	qDebug() << m_camera->lockStatus() << m_imageCapture->isReadyForCapture();
    	m_imageCapture->capture();
    }
    

    the above qDebug returns true, QCamera::Unlocked, true

    these are my imageSaved, imageCaptured, and error functions

    void WebCamDialog::imageCaptured(int id, const QImage &i) {
    	QLabel *im = new QLabel();
    	im->setPixmap(QPixmap::fromImage(i));
    	im->show();
    
    }
    
    void WebCamDialog::imageSaved(int id, const QString &s) {
    	qDebug() << s;
    }
    
    void WebCamDialog::displayCaptureError(int id, QCameraImageCapture::Error error, const QString &errorString) {
    	Q_UNUSED(id);
    	qDebug() << error;
    	qDebug() << tr("Image Capture Error") << errorString;
    }
    

    imageCaptured displays the captured image correctly, imageSaved is never reached. I only have one camera available so i've ommited the actual selecting of a camera, and just made it default to the only camera i have



  • Try adding m_imageCapture.setCaptureDestination(QCameraImageCapture::CaptureToFile);.

    P.S.

    QCheckBox *cam = new QCheckBox(ci.description());
    vailableCameras->setItemWidget(lwi, cam);
    

    Hurts my feelings. See https://forum.qt.io/post/486329 it applies also to QListWidgetItem with the only difference that you don't, obviously, specify a column