ivpicview.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "ivpicview.h"
  2. #include <QDateTime>
  3. #include <opencv2/opencv.hpp>
  4. #include <opencv2/core.hpp>
  5. #ifdef NVIDIA_AGX
  6. #include <opencv2/imgcodecs/legacy/constants_c.h> //OpenCV4 use this line
  7. #include <opencv2/imgproc/types_c.h> //OpenCV4 use this line
  8. #endif
  9. #define VIEW_WIDTH 640
  10. #define VIEW_HEIGHT 480
  11. IVPicView::IVPicView()
  12. {
  13. mimagepaint = new QImage(VIEW_WIDTH, VIEW_HEIGHT, QImage::Format_RGB32);//画布的初始化大小设为300*300,使用32位颜色
  14. }
  15. void IVPicView::run()
  16. {
  17. while(!QThread::isInterruptionRequested())
  18. {
  19. if(mnReadIndex != mnWriteIndex)
  20. {
  21. paint();
  22. mnReadIndex = mnWriteIndex;
  23. mbImageUpdate = true;
  24. }
  25. else
  26. {
  27. msleep(10);
  28. }
  29. }
  30. }
  31. void IVPicView::paint()
  32. {
  33. iv::vision::rawpic pic;
  34. mMutex.lock();
  35. pic.CopyFrom(mrawpic);
  36. mMutex.unlock();
  37. cv::Mat mat(pic.height(),pic.width(),pic.mattype());
  38. if(pic.type() == 1)
  39. memcpy(mat.data,pic.picdata().data(),mat.rows*mat.cols*mat.elemSize());
  40. else
  41. {
  42. // qDebug("jpg");
  43. std::vector<unsigned char> buff(pic.picdata().data(),pic.picdata().data()+pic.picdata().size());
  44. mat = cv::imdecode(buff,CV_LOAD_IMAGE_COLOR);
  45. int font = cv::FONT_HERSHEY_DUPLEX;
  46. QDateTime xrecvtime = QDateTime::fromMSecsSinceEpoch(pic.time());
  47. // char strtext[256];
  48. // snprintf(strtext,"%s",xrecvtime.toString("yyyy-MM-dd hh:mm:ss:zzz").toLatin1().data());
  49. std::string strtext = xrecvtime.toString("yyyy-MM-dd hh:mm:ss:zzz").toStdString();
  50. double fratio = pic.width()/640;
  51. cv::putText(mat,strtext,cv::Point(pic.width() - 230*fratio,30*fratio),font,0.5*fratio,cv::Scalar(0,255,0),0.5*fratio);
  52. }
  53. cv::cvtColor(mat, mat, CV_BGR2RGB);
  54. QImage image2 = QImage((uchar*)(mat.data), mat.cols, mat.rows, QImage::Format_RGB888);
  55. mMutexPaint.lock();
  56. // delete mimagepaint;
  57. // mimagepaint = new QImage(mat.cols, mat.rows, QImage::Format_RGB888);
  58. *mimagepaint = image2.copy();
  59. // *mimagepaint = image2;
  60. // mimagepaint = new QImage(image2);
  61. mMutexPaint.unlock();
  62. mat.release();
  63. emit painterupadate();
  64. }
  65. QImage IVPicView::GetImage()
  66. {
  67. mMutexPaint.lock();
  68. // QImage imagertn(*mimagepaint);
  69. // QImage imagertn(mimagepaint->width(), mimagepaint->height(), QImage::Format_RGB32);
  70. QImage imagertn = mimagepaint->copy();
  71. mMutexPaint.unlock();
  72. mbImageUpdate = false;
  73. return imagertn;
  74. }
  75. bool IVPicView::IsHaveNew()
  76. {
  77. return mbImageUpdate;
  78. }
  79. int IVPicView::GetType()
  80. {
  81. return 2;
  82. }
  83. void IVPicView::SetPic(iv::vision::rawpic pic)
  84. {
  85. mMutex.lock();
  86. mrawpic.CopyFrom(pic);
  87. mnWriteIndex++;
  88. mMutex.unlock();
  89. }