Help capturing an image
-
wrote on 6 May 2024, 21:03 last edited by
Hello,
I'm using multimedia to try and capture an image from my camera. I believe my connections are correct (they're similar to the example that is provided) but whenever I try to capture() a readyForCaptureChanged signal is emitted and isReadyForCapture changes from true to false. I made this a few months ago, I forgot which version I was on, and it was working fine, but recently I updated the components and I'm unable to capture an image. -
Since there is no code posted, we don't know what you are actually trying.
Maybe just read this, and if it doesn't solve your problem - post your code. -
wrote on 7 May 2024, 18:39 last edited by
Thanks for getting back to me, unfortunately the link that you sent didn't help me. Here's the code that I'm using
from another file I'm calling getAvailableDevices(), then initDevice with a valid device index, then I'm setting the settings in captureSetting, and lastly I call captureFrame() where the image is supposed to be captured. The problem though is that whenever capture() is called readyForCapture is called again but ready is set to false.
VideoCapture::VideoCapture(QImage *image)
{
savedImage = image;
}void VideoCapture::getAvailableDevices()
{
m_devices = QMediaDevices::videoInputs();
}void VideoCapture::initDevice(const int deviceIdx )
{
int cnt = 0;for (const QCameraDevice &cameraDevice : m_devices) { qDebug() << cnt << ". " << cameraDevice.description(); if(cameraDevice.isNull()) { qDebug() << "\t"<< cnt++ << " is null\n"; } } qDebug() << "Initializing device " << deviceIdx << " for capturing an image" << Qt::endl; /*Initialize a capture device*/ m_targetDevice.reset(new QCamera(m_devices[deviceIdx])); m_captureSession.setCamera(m_targetDevice.data()); /*Initialize a QImageCapture instance*/ m_capture = new QImageCapture; m_captureSession.setImageCapture(m_capture); /*Connections for updating the camera*/ connect(m_targetDevice.data(), &QCamera::activeChanged, this, &VideoCapture::updateCameraActive, Qt::DirectConnection); connect(m_targetDevice.data(), &QCamera::errorOccurred, this, &VideoCapture::cameraError, Qt::DirectConnection); /*Conenctions for capturing an image*/ connect(m_capture, &QImageCapture::readyForCaptureChanged, this, &VideoCapture::readyForCapture, Qt::DirectConnection); connect(m_capture, &QImageCapture::imageCaptured, this, &VideoCapture::imageCaptured, Qt::DirectConnection); connect(m_capture, &QImageCapture::errorOccurred, this, &VideoCapture::captureError, Qt::DirectConnection);
};
void VideoCapture::updateCameraActive(bool ready)
{
if (ready)
{
qDebug() << "Camera is active ";
}
else
{
qDebug() << "Camera is not active";
}
}void VideoCapture::captureSetting(int width, int height)
{
m_capture->setFileFormat(QImageCapture::PNG);
m_capture->setQuality(QImageCapture::VeryHighQuality);
m_capture->setResolution(width, height);
}void VideoCapture::captureFrame()
{
m_targetDevice->start();
updateCameraActive(m_targetDevice->isActive());
checkReadyForCapture();
readyForCapture(m_capture->isReadyForCapture());
}void VideoCapture::imageCaptured(int requestId, const QImage &img)
{
Q_UNUSED(requestId);
Q_UNUSED(img);
qDebug() << "Image Captured!!";
*savedImage = img.copy();
m_targetDevice->stop();
}void VideoCapture::readyForCapture(bool ready)
{
if(ready)
{
qDebug() << "Capture ready";
m_capture->capture();
}
else
{
qDebug() << "Capture not ready";
}
}void VideoCapture::captureError(int id, const QImageCapture::Error error, const QString &errorString)
{
Q_UNUSED(id);
Q_UNUSED(error);
qDebug() << "Capture Error: " << errorString;
m_targetDevice->stop();
}void VideoCapture::cameraError()
{
if (m_targetDevice->error() != QCamera::NoError)
{
qDebug() << "Camera error: " << m_targetDevice->errorString();
}
}void VideoCapture::checkReadyForCapture()
{
QString boolStr = m_capture->isReadyForCapture() ? "True" : "False";
qDebug() << "isReadyForCapture: " << boolStr;
} -
Lifetime Qt Championwrote on 7 May 2024, 19:23 last edited by SGaist 5 Jul 2024, 19:23
Hi,
There several strange (as in wrong) stuff in your code:
- Why are you forcing the connection type to be
DirectConnection
? There's rarely a need to specify the type of connection. - Why is
savedImage
a pointer rather than a QImage object ? There's usually no need for that
Additional questions:
- Which version of Qt ?
- On which OS ?
- Why are you forcing the connection type to be
-
wrote on 7 May 2024, 19:45 last edited by
For DirectConnection, I added that because I thought I needed it; Ill take it off. savedImage is used in the file that calls the VideoCapture functions. I'm running QT on 6.6.3 and I'm on windows
-
You should rather emit the new image from your VideoCapture class rather than fiddling with a variable that it should not know about.
-
wrote on 8 May 2024, 13:18 last edited by
Ok, ill look into emitting the image that it captures, but I still have the problem where capture() wont capture an image
-
Did you try to save the image in a file to see how it looks ?
-
wrote on 8 May 2024, 20:15 last edited by John1123 5 Aug 2024, 20:27
An image can't be captured in the first place, whenever I call capture it redirects to readyForCapture and isReadyForCapture changes from true to false. This also applies to captureToFile()
-
Which version of Qt are you using ?
On which OS ?
With which device ? -
wrote on 9 May 2024, 19:59 last edited by
I'm running QT on 6.6.3, I'm on windows, and I'm using my webcam
-
Which version of Windows ?
What is the model of your webcam ? -
wrote on 10 May 2024, 12:35 last edited by
my webcam is a Logi C615 HD WebCam, and im on windows 10
-
Can you check again with 6.7 ?
-
wrote on 13 May 2024, 15:32 last edited by
I've checked again in 6.7 and it still doesn't work.
-
Ok, then can you switch the backend to use the native rather than ffmpeg ?
-
wrote on 13 May 2024, 19:22 last edited by
How would I do that?
-
It's described here.
-
wrote on 15 May 2024, 14:41 last edited by John1123
So I've tried it, and now isReadyForCapture doesn't change to false after capture() is called but it still doesn't capture an image
-
Can you provide a minimal compilable example that shows this behaviour ?