123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include "ivpicview.h"
- #include <QDateTime>
- #include <opencv2/opencv.hpp>
- #include <opencv2/core.hpp>
- #ifdef NVIDIA_AGX
- #include <opencv2/imgcodecs/legacy/constants_c.h> //OpenCV4 use this line
- #include <opencv2/imgproc/types_c.h> //OpenCV4 use this line
- #endif
- #define VIEW_WIDTH 640
- #define VIEW_HEIGHT 480
- IVPicView::IVPicView()
- {
- mimagepaint = new QImage(VIEW_WIDTH, VIEW_HEIGHT, QImage::Format_RGB32);//画布的初始化大小设为300*300,使用32位颜色
- }
- void IVPicView::run()
- {
- while(!QThread::isInterruptionRequested())
- {
- if(mnReadIndex != mnWriteIndex)
- {
- paint();
- mnReadIndex = mnWriteIndex;
- mbImageUpdate = true;
- }
- else
- {
- msleep(10);
- }
- }
- }
- void IVPicView::paint()
- {
- iv::vision::rawpic pic;
- mMutex.lock();
- pic.CopyFrom(mrawpic);
- mMutex.unlock();
- cv::Mat mat(pic.height(),pic.width(),pic.mattype());
- if(pic.type() == 1)
- memcpy(mat.data,pic.picdata().data(),mat.rows*mat.cols*mat.elemSize());
- else
- {
- // qDebug("jpg");
- std::vector<unsigned char> buff(pic.picdata().data(),pic.picdata().data()+pic.picdata().size());
- mat = cv::imdecode(buff,CV_LOAD_IMAGE_COLOR);
- int font = cv::FONT_HERSHEY_DUPLEX;
- QDateTime xrecvtime = QDateTime::fromMSecsSinceEpoch(pic.time());
- // char strtext[256];
- // snprintf(strtext,"%s",xrecvtime.toString("yyyy-MM-dd hh:mm:ss:zzz").toLatin1().data());
- std::string strtext = xrecvtime.toString("yyyy-MM-dd hh:mm:ss:zzz").toStdString();
- double fratio = pic.width()/640;
- cv::putText(mat,strtext,cv::Point(pic.width() - 230*fratio,30*fratio),font,0.5*fratio,cv::Scalar(0,255,0),0.5*fratio);
- }
- cv::cvtColor(mat, mat, CV_BGR2RGB);
- QImage image2 = QImage((uchar*)(mat.data), mat.cols, mat.rows, QImage::Format_RGB888);
- mMutexPaint.lock();
- // delete mimagepaint;
- // mimagepaint = new QImage(mat.cols, mat.rows, QImage::Format_RGB888);
- *mimagepaint = image2.copy();
- // *mimagepaint = image2;
- // mimagepaint = new QImage(image2);
- mMutexPaint.unlock();
- mat.release();
- emit painterupadate();
- }
- QImage IVPicView::GetImage()
- {
- mMutexPaint.lock();
- // QImage imagertn(*mimagepaint);
- // QImage imagertn(mimagepaint->width(), mimagepaint->height(), QImage::Format_RGB32);
- QImage imagertn = mimagepaint->copy();
- mMutexPaint.unlock();
- mbImageUpdate = false;
- return imagertn;
- }
- bool IVPicView::IsHaveNew()
- {
- return mbImageUpdate;
- }
- int IVPicView::GetType()
- {
- return 2;
- }
- void IVPicView::SetPic(iv::vision::rawpic pic)
- {
- mMutex.lock();
- mrawpic.CopyFrom(pic);
- mnWriteIndex++;
- mMutex.unlock();
- }
|