How to detect CameraBin warning: "Resource busy or not available."
-
Proceed with camera capture through "QCameraImageCapture". But sometimes CameraBin warning: "Resource busy or not available." Memory usage increases with the error: The full code is below.
camera.cpp
#include "camera.h" #include <QBuffer> #include <QCoreApplication> #include <QDateTime> #include <QJsonDocument> #include <QJsonObject> #include <QFile> #include <QCameraViewfinder> camera::camera(QObject *parent):QThread(parent) { const QList<QCameraInfo> cameras = QCameraInfo::availableCameras(); for (const QCameraInfo &cameraInfo : cameras)// { qDebug() << cameraInfo.deviceName(); if(CameraV != nullptr) { delete CameraV; CameraV = nullptr; } CameraV = new QCamera(cameraInfo); if(CameraV != nullptr) { CameraImageCapture = new QCameraImageCapture(CameraV); CameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);//Not File Only To Buffer connect(CameraImageCapture, &QCameraImageCapture::readyForCaptureChanged, this, &camera::CaptureStatusChanged); connect(CameraImageCapture, &QCameraImageCapture::imageCaptured, this, &camera::processCapturedImage); connect(this, SIGNAL(RequestCapture()), this, SLOT(ImageCapture())); CameraV->setCaptureMode(QCamera::CaptureStillImage); CameraV->start(); this->start(); } break; } } void camera::run() { while(true) { //emit ReqCurrentSave(); emit RequestCapture(); //QThread::sleep(1); //QThread::msleep(1); } } void camera::ImageCapture() { if (CameraImageCapture != nullptr && Camera_Available == true && Camera_IsBusy == false) { //CameraImageCapture->cancelCapture(); CameraImageCapture->capture(); } } camera::~camera() { if (CameraImageCapture != nullptr) { delete CameraImageCapture; CameraImageCapture = nullptr; } if (CameraV != nullptr) { CameraV->stop(); delete CameraV; CameraV = nullptr; } } void camera::CaptureStatusChanged(bool ready) { qDebug()<<"Chagned to"<<ready; this->Camera_Available = ready; } void camera::processCapturedImage(int requestId, const QImage& img) { Camera_IsBusy = true; Q_UNUSED(requestId); try { if (img.isNull() == false) { Image.clear(); QBuffer buffer(&Image); buffer.open(QIODevice::WriteOnly); img.save(&buffer, "jpg"); buffer.close(); Image = qCompress(Image).toBase64(); id = QDateTime::currentDateTime().toMSecsSinceEpoch(); } else { qDebug()<<"Image is null."; } } catch(std::exception& e) { qDebug() << Q_FUNC_INFO << e.what(); } Camera_IsBusy = false; }camera.h
#ifndef CAMERA_H #define CAMERA_H #include <QThread> #include <QCamera> #include <QCameraInfo> #include <QCameraImageCapture> #include <QMutex> class camera : public QThread { Q_OBJECT private: void run(); public: ~camera(); explicit camera(QObject *parent = nullptr); bool Camera_Available = false; bool Camera_IsBusy = false; QCamera * CameraV = nullptr; QCameraImageCapture * CameraImageCapture = nullptr; QByteArray Image = nullptr; unsigned long long id = 0; QMutex ImageMutex; signals: void imageCaptured(const QImage& image); void RequestCapture(); void ReqCurrentSave(); public slots: void CaptureStatusChanged(bool ready); void processCapturedImage(int requestId, const QImage& img); void ImageCapture(); }; #endif // CAMERA_HI need to continuously capture images, encode and compress the captured images and store them in a public variable.
Therefore, the capture signal must be generated periodically in the while statement.
But sometimes the memory usage increases because of the above error.
What can I do?
CameraImageCapture->cancelCapture(); also doesn't work.