processingthread.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "processingthread.h"
  2. #include <QDebug>
  3. static const int QUEUE_MAX_LENGTH = 3;
  4. static const int THREAD_SLEEP_MS = 25;
  5. ProcessingThread::ProcessingThread(QObject *parent) :
  6. QThread(parent), m_stopped(false), m_queueMaxLength(QUEUE_MAX_LENGTH)
  7. {
  8. }
  9. ProcessingThread::~ProcessingThread()
  10. {
  11. stop();
  12. }
  13. void ProcessingThread::stop()
  14. {
  15. m_stopped = true;
  16. }
  17. void ProcessingThread::addFrameToProcessingQueue(QImage frame)
  18. {
  19. if (m_queue.length() < m_queueMaxLength) {
  20. QImage threadCopy = frame.copy();
  21. m_queue.enqueue(threadCopy);
  22. } else {
  23. emit queueFull();
  24. }
  25. }
  26. void ProcessingThread::run()
  27. {
  28. // Process until stop() called
  29. while (!m_stopped)
  30. {
  31. if (!m_queue.isEmpty())
  32. {
  33. QImage currentFrame = m_queue.dequeue();
  34. // Here you can do whatever processing you need on the frame, like detect barcodes, etc.
  35. emit frameProcessed();
  36. }
  37. else
  38. {
  39. // No frames in queue, sleep for a short while
  40. msleep(THREAD_SLEEP_MS);
  41. }
  42. }
  43. qDebug() << "Processing thread ending";
  44. exit(0);
  45. }