Capture system camera frames from thread
-
Hi all
I need to capture frames from a system camera using a separate thread. Follow my code from a derived QThread class:
void CameraMonitor::run() { QScopedPointer<QCamera> camera(new QCamera(cameraDevice)); QScopedPointer<QMediaCaptureSession> captureSession(new QMediaCaptureSession()); QScopedPointer<QVideoSink> videoSink(new QVideoSink()); camera->setFocusMode(QCamera::FocusModeAuto); connect(videoSink.data(), &QVideoSink::videoFrameChanged, this, &CameraMonitor::processVideoFrame); captureSession->setVideoSink(videoSink.data()); captureSession->setCamera(camera.data()); camera->start(); exec(); camera->stop(); captureSession->setCamera(nullptr); captureSession->setVideoSink(nullptr); } void CameraMonitor::processVideoFrame(const QVideoFrame &videoFrame) { //.... }
My problem is that the processVideoFrame slot is nevel called and I don't understand if I made some mystake in the code or simply this is not the correct way to get the result. I can see the camera led on that's mean the code activate the camera but I don't understand if it doesn't acquire or the signal/slot sistem doesn't work also calling exec() inside the thread.
Someone can help me?
Thank you -
Hi all
I need to capture frames from a system camera using a separate thread. Follow my code from a derived QThread class:
void CameraMonitor::run() { QScopedPointer<QCamera> camera(new QCamera(cameraDevice)); QScopedPointer<QMediaCaptureSession> captureSession(new QMediaCaptureSession()); QScopedPointer<QVideoSink> videoSink(new QVideoSink()); camera->setFocusMode(QCamera::FocusModeAuto); connect(videoSink.data(), &QVideoSink::videoFrameChanged, this, &CameraMonitor::processVideoFrame); captureSession->setVideoSink(videoSink.data()); captureSession->setCamera(camera.data()); camera->start(); exec(); camera->stop(); captureSession->setCamera(nullptr); captureSession->setVideoSink(nullptr); } void CameraMonitor::processVideoFrame(const QVideoFrame &videoFrame) { //.... }
My problem is that the processVideoFrame slot is nevel called and I don't understand if I made some mystake in the code or simply this is not the correct way to get the result. I can see the camera led on that's mean the code activate the camera but I don't understand if it doesn't acquire or the signal/slot sistem doesn't work also calling exec() inside the thread.
Someone can help me?
Thank you@FalsinSoft first of all
void QCamera::setViewfinder(QAbstractVideoSurface *surface)You have to create an abstract video surface and add it to the camera using the function setViewfinder.
Then, the video surface will notify you when a frame is present
Finally, call your thread class when the frame is present and give it as an argument the image(QVideoFrame converted to QImage) -
@FalsinSoft first of all
void QCamera::setViewfinder(QAbstractVideoSurface *surface)You have to create an abstract video surface and add it to the camera using the function setViewfinder.
Then, the video surface will notify you when a frame is present
Finally, call your thread class when the frame is present and give it as an argument the image(QVideoFrame converted to QImage)@Ronel_qtmaster Thank you for your reply but I'm developing using Qt6 and there is no method setViewfinder in the "new" QCamera class...
-
@Ronel_qtmaster Thank you for your reply but I'm developing using Qt6 and there is no method setViewfinder in the "new" QCamera class...
This post is deleted! -
@Ronel_qtmaster Thank you for your reply but I'm developing using Qt6 and there is no method setViewfinder in the "new" QCamera class...
@FalsinSoft SORRY I AM not using qt6