From 61f983d0dcbf5e881aec2278cdf40f765eb4332b Mon Sep 17 00:00:00 2001 From: fuleyi Date: Wed, 27 May 2026 12:06:48 +0800 Subject: [PATCH] fix: prevent capture stuck on null images MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added m_nullCount member variable to track null image captures. Reset m_nullCount on thread start and when capture is stopped. In readyForCapture, added condition to check if capture is not stopped. In processCapturedImage, added logic to handle null previews: retry up to 10 times with sleep, else emit error. Changed first image condition from checking id to using m_bFirst flag. Removed unnecessary capture call after image processing. Influence: 1. Test capture functionality with normal and null image scenarios. 2. Verify that capture does not get stuck when images are null. 3. Test the retry mechanism and error emission after multiple failures. 4. Check that first image handling works correctly with the new flag. 5. Ensure capture stops properly when requested. 6. Test edge cases like rapid stop/start actions. 修复: 防止采集空图片时卡住 添加 m_nullCount 成员变量以跟踪空图片采集次数。 在线程启动和采集停止时重置 m_nullCount。 在 readyForCapture 中添加条件检查采集是否未停止。 在 processCapturedImage 中添加处理空预览的逻辑:最多重试10次并带延迟,否 则发出错误信号。 将首张图片条件从检查 id 改为使用 m_bFirst 标志。 移除处理图片后不必要的采集调用。 Influence: 1. 测试正常和空图片场景下的采集功能。 2. 验证当图片为空时采集不会卡住。 3. 测试重试机制和多次失败后的错误发出。 4. 检查使用新标志的首张图片处理是否正确。 5. 确保在请求时采集能正确停止。 6. 测试快速停止/启动操作等边界情况。 PMS: BUG-352653 Change-Id: I3a6964c991e6c8aef8385195ae9f54eb84455d41 --- workmodule.cpp | 30 +++++++++++++++++++++++++----- workmodule.h | 1 + 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/workmodule.cpp b/workmodule.cpp index 60a28ef..e82c8ea 100644 --- a/workmodule.cpp +++ b/workmodule.cpp @@ -18,6 +18,7 @@ ErollThread::ErollThread(QObject *parent) , m_bFirst(false) , m_stopCapture(false) , m_checkDone(true) + , m_nullCount(0) { } @@ -25,6 +26,7 @@ void ErollThread::Start(QString actionId, int socket) { qDebug() << "ErollThread::Start thread:" << QThread::currentThreadId(); m_stopCapture = false; + m_nullCount = 0; m_actionId = actionId; m_fileSocket = socket; m_bFirst = true; @@ -134,9 +136,9 @@ void ErollThread::sendCapture(QImage &img) void ErollThread::readyForCapture(bool ready) { - if (m_imageCapture && ready) { + if (m_imageCapture && ready && !m_stopCapture) { m_imageCapture->capture(); - qInfo() << "ErollThread::readyForCapture"; + qDebug() << "ErollThread::readyForCapture"; } } @@ -151,8 +153,27 @@ void ErollThread::captureError(int err, QImageCapture::Error, const QString &err void ErollThread::processCapturedImage(int id, const QImage &preview) { - if (m_stopCapture) + if (m_stopCapture) { + m_nullCount = 0; return; + } + + if (preview.isNull()) { + if (++m_nullCount > 10) { + qWarning() << "captured image is null too many times, aborting"; + m_nullCount = 0; + Q_EMIT processStatus(m_actionId, FaceEnrollException); + return; + } + qWarning() << "captured image is null, retrying capture"; + QThread::msleep(100); + if (m_stopCapture) + return; + m_imageCapture->capture(); + return; + } + + m_nullCount = 0; QImage img; if (preview.size() == QSize(800, 600)) { @@ -169,7 +190,7 @@ void ErollThread::processCapturedImage(int id, const QImage &preview) .scaled(800, 600, Qt::IgnoreAspectRatio, Qt::FastTransformation); } - if (1 == id) { + if (m_bFirst) { sendCapture(img); m_bFirst = false; } else { @@ -180,7 +201,6 @@ void ErollThread::processCapturedImage(int id, const QImage &preview) sendCapture(img); } - m_imageCapture->capture(); if (!m_checkDone) { diff --git a/workmodule.h b/workmodule.h index 5d7ff35..0c0de11 100644 --- a/workmodule.h +++ b/workmodule.h @@ -58,6 +58,7 @@ private Q_SLOTS: int m_fileSocket; bool m_bFirst; bool m_checkDone; + std::atomic m_nullCount; };