Help capturing an image
-
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. -
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;
} -
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
-
You should rather emit the new image from your VideoCapture class rather than fiddling with a variable that it should not know about.
-
Did you try to save the image in a file to see how it looks ?
-
Which version of Qt are you using ?
On which OS ?
With which device ? -
Which version of Windows ?
What is the model of your webcam ? -
Can you check again with 6.7 ?
-
Ok, then can you switch the backend to use the native rather than ffmpeg ?
-
Can you provide a minimal compilable example that shows this behaviour ?