123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #include <QtCore/QCoreApplication>
- #include <QTime>
- #include <QGridLayout>
- #include <QFile>
- #include <QElapsedTimer>
- #include "Util.h"
- Util *Util::m_instance = 0;
- void Util::showMessage(const QString &message)
- {
- emit messageSignals(message);
- }
- Util *Util::instance()
- {
- static QMutex mutex;
- if (!m_instance) {
- QMutexLocker locker(&mutex);
- if (!m_instance)
- m_instance = new Util;
- }
- return m_instance;
- }
- void Util::notify(const QString &message)
- {
- Util::instance()->showMessage(message);
- }
- QMainWindow *Util::getMainWindow()
- {
- return Util::instance()->mainWin;
- }
- void Util::setMainWindow(QMainWindow *mainWindows)
- {
- Util::instance()->mainWin=mainWindows;
- }
- QImage Util::HObject2Qimage(const HalconCpp::HObject &hobj)
- {
- using namespace HalconCpp;
- HTuple pointer, htype, hwidth, hheigth;
- HTuple nchannels;
- CountChannels(hobj, &nchannels);
- QImage qimg;
- QElapsedTimer time;
- if (nchannels==1)
- {
- time.start();
- double time_Start = (double)clock(); //开始时间
- GetImagePointer1(hobj, &pointer, &htype, &hwidth, &hheigth);
- char *p = (char *)pointer[0].L();
- int height = (Hlong)hheigth;
- int width = (Hlong)hwidth;
- qimg=QImage(width, height, QImage::Format_Indexed8);
- //memcpy(img1, p, width*height*sizeof(char));
- QVector<QRgb> colorTable;
- for (int i = 0; i < 256; i++)
- {
- colorTable.append(qRgb(i, i, i));
- }
- uchar *data1;
- data1=qimg.bits();
- //当前测试结果是在release环境下循环和复制内存用时相差不大,debug状态下复制内存效率更高
- int nSize = width * height * sizeof(char);
- memcpy(data1, p, nSize);
- //for (int i = 0; i < height; i++)
- //{
- // data = qimg.scanLine(i);
- // //memcpy(data, &p[i*width ], width);
- // for (int j = 0; j < width; j++)
- // {
- // data[j] = p[i*width + j];
- // }
- //}
- qimg.setColorTable(colorTable);
- double timr_Finish = (double)clock(); //结束时间
- int time_Diff = time.elapsed(); //返回从上次start()或restart()开始以来的时间差,单位ms
- //QString str = "running time=" + (timr_Finish - time_Start);
- Util::notify(QString(QString::fromUtf8("处理时间1=%1 ms")).arg(timr_Finish - time_Start));
- Util::notify(QString(GBK("处理时间2=%1 ms")).arg(time_Diff));
- }
- else if (nchannels == 3)
- {
- HTuple ptrR, ptrG, ptrB;
- GetImagePointer3(hobj, &ptrR, &ptrG, &ptrB, &htype, &hwidth, &hheigth);
- char *pR = (char *)ptrR[0].L();
- char *pG = (char *)ptrG[0].L();
- char *pB= (char *)ptrB[0].L();
- int height = (Hlong)hheigth;
- int width = (Hlong)hwidth;
- qimg = QImage(width, height, QImage::Format_RGB888);
- for (int i = 0; i < height; i++)
- {
- uchar *data;
- data = qimg.scanLine(i);
- for (int j = 0; j < width; j++)
- {
- data[j * 3] = pR[i*width + j];
- data[j * 3 + 1] = pG[i*width + j];
- data[j * 3 + 2] = pB[i*width + j];
- }
- }
- }
- return qimg;
- }
- void Util::ShowUnitInWidget(QWidget *parent, QWidget *child)
- {
- QGridLayout *layout1 = new QGridLayout(parent);
- layout1->setContentsMargins(0, 0, 0, 0);
- layout1->addWidget(child,0,0,1,1);
- }
- void Util::WriteLog(QtMsgType type, const char *msg)
- {
- QString txt;
- switch (type) {
- //调试信息提示
- case QtDebugMsg:
- txt = QString("Debug: %1").arg(msg);
- break;
- //一般的warning提示
- case QtWarningMsg:
- txt = QString("Warning: %1").arg(msg);
- break;
- //严重错误提示
- case QtCriticalMsg:
- txt = QString("Critical: %1").arg(msg);
- break;
- //致命错误提示
- case QtFatalMsg:
- txt = QString("Fatal: %1").arg(msg);
- abort();
- default:
- break;
- }
- QFile outFile1("debuglog1.txt");
- QFile outFile2("debuglog2.txt");
- outFile1.open(QIODevice::WriteOnly | QIODevice::Append);
- if(outFile1.size() >= 1024*10 )
- {
- outFile1.close();
- outFile2.remove();
- QFile::copy("debuglog1.txt","debuglog2.txt");
- outFile1.remove();
- QFile outFile3("debuglog1.txt");
- outFile3.open(QIODevice::WriteOnly | QIODevice::Append);
- QTextStream ts(&outFile3);
- ts << txt << endl;
- }
- else
- {
- QTextStream ts(&outFile1);
- ts << txt << endl;
- }
- }
- QString Util::getHalconErrMessage(int errCode)
- {
- QString message=GBK("未定义代码:");
- switch (errCode)
- {
- case H_ERR_WIPV1:
- message=GBK("参数1数据异常");
- break;
- case H_ERR_WIPV2:
- message=GBK("参数2数据异常");
- break;
- case H_ERR_WIPV3:
- message=GBK("参数3数据异常");
- break;
- case H_ERR_WIPV4:
- message=GBK("参数4数据异常");
- break;
- case H_ERR_WIPV5:
- message=GBK("参数5数据异常");
- break;
- case H_ERR_WIPV6:
- message=GBK("参数6数据异常");
- break;
- case H_ERR_WIPV7:
- message=GBK("参数7数据异常");
- break;
- case H_ERR_WIPV8:
- message=GBK("参数8数据异常");
- break;
- case H_ERR_WIPV9:
- message=GBK("参数9数据异常");
- break;
- case H_ERR_WIPV10:
- message=GBK("参数10数据异常");
- break;
- case H_ERR_WIPV11:
- message=GBK("参数11数据异常");
- break;
- case H_ERR_WIPV12:
- message=GBK("参数12数据异常");
- break;
- case H_ERR_WIPV13:
- message=GBK("参数13数据异常");
- break;
- case H_ERR_WIPV14:
- message=GBK("参数14数据异常");
- break;
- case H_ERR_WIPV15:
- message=GBK("参数15数据异常");
- break;
- case H_ERR_WIPV16:
- message=GBK("参数16数据异常");
- break;
- case H_ERR_WIPV17:
- message=GBK("参数17数据异常");
- break;
- case H_ERR_WIPV18:
- message=GBK("参数18数据异常");
- break;
- case H_ERR_WIPV19:
- message=GBK("参数19数据异常");
- break;
- case H_ERR_WIPV20:
- message=GBK("参数20数据异常");
- break;
- case H_ERR_JPGLIB_INFORMAT:
- message=GBK("jpg图像格式异常.");
- break;
- default:
- message+=QString::number(errCode);
- break;
- }
- return message;
- }
- Util::Util() :
- QObject(qApp)
- {
- mainWin = nullptr;
- }
|