Util.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include <QtCore/QCoreApplication>
  2. #include <QTime>
  3. #include <QGridLayout>
  4. #include <QFile>
  5. #include <QElapsedTimer>
  6. #include "Util.h"
  7. Util *Util::m_instance = 0;
  8. void Util::showMessage(const QString &message)
  9. {
  10. emit messageSignals(message);
  11. }
  12. Util *Util::instance()
  13. {
  14. static QMutex mutex;
  15. if (!m_instance) {
  16. QMutexLocker locker(&mutex);
  17. if (!m_instance)
  18. m_instance = new Util;
  19. }
  20. return m_instance;
  21. }
  22. void Util::notify(const QString &message)
  23. {
  24. Util::instance()->showMessage(message);
  25. }
  26. QMainWindow *Util::getMainWindow()
  27. {
  28. return Util::instance()->mainWin;
  29. }
  30. void Util::setMainWindow(QMainWindow *mainWindows)
  31. {
  32. Util::instance()->mainWin=mainWindows;
  33. }
  34. QImage Util::HObject2Qimage(const HalconCpp::HObject &hobj)
  35. {
  36. using namespace HalconCpp;
  37. HTuple pointer, htype, hwidth, hheigth;
  38. HTuple nchannels;
  39. CountChannels(hobj, &nchannels);
  40. QImage qimg;
  41. QElapsedTimer time;
  42. if (nchannels==1)
  43. {
  44. time.start();
  45. double time_Start = (double)clock(); //开始时间
  46. GetImagePointer1(hobj, &pointer, &htype, &hwidth, &hheigth);
  47. char *p = (char *)pointer[0].L();
  48. int height = (Hlong)hheigth;
  49. int width = (Hlong)hwidth;
  50. qimg=QImage(width, height, QImage::Format_Indexed8);
  51. //memcpy(img1, p, width*height*sizeof(char));
  52. QVector<QRgb> colorTable;
  53. for (int i = 0; i < 256; i++)
  54. {
  55. colorTable.append(qRgb(i, i, i));
  56. }
  57. uchar *data1;
  58. data1=qimg.bits();
  59. //当前测试结果是在release环境下循环和复制内存用时相差不大,debug状态下复制内存效率更高
  60. int nSize = width * height * sizeof(char);
  61. memcpy(data1, p, nSize);
  62. //for (int i = 0; i < height; i++)
  63. //{
  64. // data = qimg.scanLine(i);
  65. // //memcpy(data, &p[i*width ], width);
  66. // for (int j = 0; j < width; j++)
  67. // {
  68. // data[j] = p[i*width + j];
  69. // }
  70. //}
  71. qimg.setColorTable(colorTable);
  72. double timr_Finish = (double)clock(); //结束时间
  73. int time_Diff = time.elapsed(); //返回从上次start()或restart()开始以来的时间差,单位ms
  74. //QString str = "running time=" + (timr_Finish - time_Start);
  75. Util::notify(QString(QString::fromUtf8("处理时间1=%1 ms")).arg(timr_Finish - time_Start));
  76. Util::notify(QString(GBK("处理时间2=%1 ms")).arg(time_Diff));
  77. }
  78. else if (nchannels == 3)
  79. {
  80. HTuple ptrR, ptrG, ptrB;
  81. GetImagePointer3(hobj, &ptrR, &ptrG, &ptrB, &htype, &hwidth, &hheigth);
  82. char *pR = (char *)ptrR[0].L();
  83. char *pG = (char *)ptrG[0].L();
  84. char *pB= (char *)ptrB[0].L();
  85. int height = (Hlong)hheigth;
  86. int width = (Hlong)hwidth;
  87. qimg = QImage(width, height, QImage::Format_RGB888);
  88. for (int i = 0; i < height; i++)
  89. {
  90. uchar *data;
  91. data = qimg.scanLine(i);
  92. for (int j = 0; j < width; j++)
  93. {
  94. data[j * 3] = pR[i*width + j];
  95. data[j * 3 + 1] = pG[i*width + j];
  96. data[j * 3 + 2] = pB[i*width + j];
  97. }
  98. }
  99. }
  100. return qimg;
  101. }
  102. void Util::ShowUnitInWidget(QWidget *parent, QWidget *child)
  103. {
  104. QGridLayout *layout1 = new QGridLayout(parent);
  105. layout1->setContentsMargins(0, 0, 0, 0);
  106. layout1->addWidget(child,0,0,1,1);
  107. }
  108. void Util::WriteLog(QtMsgType type, const char *msg)
  109. {
  110. QString txt;
  111. switch (type) {
  112. //调试信息提示
  113. case QtDebugMsg:
  114. txt = QString("Debug: %1").arg(msg);
  115. break;
  116. //一般的warning提示
  117. case QtWarningMsg:
  118. txt = QString("Warning: %1").arg(msg);
  119. break;
  120. //严重错误提示
  121. case QtCriticalMsg:
  122. txt = QString("Critical: %1").arg(msg);
  123. break;
  124. //致命错误提示
  125. case QtFatalMsg:
  126. txt = QString("Fatal: %1").arg(msg);
  127. abort();
  128. default:
  129. break;
  130. }
  131. QFile outFile1("debuglog1.txt");
  132. QFile outFile2("debuglog2.txt");
  133. outFile1.open(QIODevice::WriteOnly | QIODevice::Append);
  134. if(outFile1.size() >= 1024*10 )
  135. {
  136. outFile1.close();
  137. outFile2.remove();
  138. QFile::copy("debuglog1.txt","debuglog2.txt");
  139. outFile1.remove();
  140. QFile outFile3("debuglog1.txt");
  141. outFile3.open(QIODevice::WriteOnly | QIODevice::Append);
  142. QTextStream ts(&outFile3);
  143. ts << txt << endl;
  144. }
  145. else
  146. {
  147. QTextStream ts(&outFile1);
  148. ts << txt << endl;
  149. }
  150. }
  151. QString Util::getHalconErrMessage(int errCode)
  152. {
  153. QString message=GBK("未定义代码:");
  154. switch (errCode)
  155. {
  156. case H_ERR_WIPV1:
  157. message=GBK("参数1数据异常");
  158. break;
  159. case H_ERR_WIPV2:
  160. message=GBK("参数2数据异常");
  161. break;
  162. case H_ERR_WIPV3:
  163. message=GBK("参数3数据异常");
  164. break;
  165. case H_ERR_WIPV4:
  166. message=GBK("参数4数据异常");
  167. break;
  168. case H_ERR_WIPV5:
  169. message=GBK("参数5数据异常");
  170. break;
  171. case H_ERR_WIPV6:
  172. message=GBK("参数6数据异常");
  173. break;
  174. case H_ERR_WIPV7:
  175. message=GBK("参数7数据异常");
  176. break;
  177. case H_ERR_WIPV8:
  178. message=GBK("参数8数据异常");
  179. break;
  180. case H_ERR_WIPV9:
  181. message=GBK("参数9数据异常");
  182. break;
  183. case H_ERR_WIPV10:
  184. message=GBK("参数10数据异常");
  185. break;
  186. case H_ERR_WIPV11:
  187. message=GBK("参数11数据异常");
  188. break;
  189. case H_ERR_WIPV12:
  190. message=GBK("参数12数据异常");
  191. break;
  192. case H_ERR_WIPV13:
  193. message=GBK("参数13数据异常");
  194. break;
  195. case H_ERR_WIPV14:
  196. message=GBK("参数14数据异常");
  197. break;
  198. case H_ERR_WIPV15:
  199. message=GBK("参数15数据异常");
  200. break;
  201. case H_ERR_WIPV16:
  202. message=GBK("参数16数据异常");
  203. break;
  204. case H_ERR_WIPV17:
  205. message=GBK("参数17数据异常");
  206. break;
  207. case H_ERR_WIPV18:
  208. message=GBK("参数18数据异常");
  209. break;
  210. case H_ERR_WIPV19:
  211. message=GBK("参数19数据异常");
  212. break;
  213. case H_ERR_WIPV20:
  214. message=GBK("参数20数据异常");
  215. break;
  216. case H_ERR_JPGLIB_INFORMAT:
  217. message=GBK("jpg图像格式异常.");
  218. break;
  219. default:
  220. message+=QString::number(errCode);
  221. break;
  222. }
  223. return message;
  224. }
  225. Util::Util() :
  226. QObject(qApp)
  227. {
  228. mainWin = nullptr;
  229. }