|
1 | 1 | #include "libcameracamera.h" |
2 | 2 | #include <QDebug> |
3 | 3 | #include <QThread> |
| 4 | +#include <QTimer> |
4 | 5 | #include <cerrno> |
5 | 6 | #include <chrono> |
6 | 7 | #include <cstring> |
@@ -86,6 +87,12 @@ void LibcameraDevice::onErrorOccurred(const QString &error) { |
86 | 87 | } |
87 | 88 |
|
88 | 89 | LibCameraWorker::LibCameraWorker(QObject *parent) : QObject(parent) { |
| 90 | + // Connect captureCompleted to resumeViewfinder with QueuedConnection |
| 91 | + // This ensures resumeViewfinder runs on the Qt event loop, not libcamera's thread |
| 92 | + connect(this, &LibCameraWorker::captureCompleted, |
| 93 | + this, &LibCameraWorker::resumeViewfinder, |
| 94 | + Qt::QueuedConnection); |
| 95 | + |
89 | 96 | initCameraManager(); |
90 | 97 | } |
91 | 98 |
|
@@ -322,27 +329,21 @@ void LibCameraWorker::captureImage() { |
322 | 329 | void LibCameraWorker::processCaptureComplete(Request *request) { |
323 | 330 | qDebug() << "[DEBUG] processCaptureComplete called, status:" << request->status(); |
324 | 331 |
|
325 | | - if (request->status() == Request::RequestCancelled) { |
326 | | - // Defer resumeViewfinder to run outside this callback |
327 | | - QMetaObject::invokeMethod(this, "resumeViewfinder", Qt::QueuedConnection); |
328 | | - return; |
329 | | - } |
330 | | - |
331 | | - // Convert buffer to image |
332 | | - if (!request->buffers().empty()) { |
333 | | - QImage img = convertBufferToImage(request->buffers()); |
334 | | - if (!img.isNull()) { |
335 | | - Q_EMIT imageCaptured(img); |
| 332 | + if (request->status() != Request::RequestCancelled) { |
| 333 | + if (!request->buffers().empty()) { |
| 334 | + QImage img = convertBufferToImage(request->buffers()); |
| 335 | + if (!img.isNull()) { |
| 336 | + Q_EMIT imageCaptured(img); |
| 337 | + } else { |
| 338 | + Q_EMIT errorOccurred("libcamera: failed to convert capture buffer"); |
| 339 | + } |
336 | 340 | } else { |
337 | | - Q_EMIT errorOccurred("libcamera: failed to convert capture buffer"); |
| 341 | + Q_EMIT errorOccurred("libcamera: capture request has no buffers"); |
338 | 342 | } |
339 | | - } else { |
340 | | - Q_EMIT errorOccurred("libcamera: capture request has no buffers"); |
341 | 343 | } |
342 | 344 |
|
343 | | - // Defer resumeViewfinder to run outside this callback |
344 | | - // This is critical - we cannot call mCamera->stop() from within the requestCompleted callback |
345 | | - QMetaObject::invokeMethod(this, "resumeViewfinder", Qt::QueuedConnection); |
| 345 | + // Emit signal to trigger resume on Qt thread |
| 346 | + Q_EMIT captureCompleted(); |
346 | 347 | } |
347 | 348 |
|
348 | 349 | void LibCameraWorker::resumeViewfinder() { |
|
0 commit comments