home.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "home.h"
  2. #include "ui_home.h"
  3. #include <QDateTime>
  4. #include <QMessageBox>
  5. #include <QScreen>
  6. #include <QTimer>
  7. #include <QDebug>
  8. #include <QNetworkRequest>
  9. #include <QHttpMultiPart>
  10. #include <QNetworkAccessManager>
  11. #include <QFile>
  12. #include <QProcess>
  13. #include <QVBoxLayout>
  14. //#include <QtWin>
  15. //#include <QtWinExtras/qwinfunctions.h>
  16. #pragma comment(lib, "user32.lib")
  17. #include "httpapi.h"
  18. #include "qresource.h"
  19. #include <windows.h>
  20. #include <tlhelp32.h>
  21. #include <stdio.h>
  22. Home::Home(QWidget *parent) :
  23. QMainWindow(parent),
  24. ui(new Ui::Home)
  25. {
  26. ui->setupUi(this);
  27. myTimer = new QTimer(this);
  28. //start timer for screen capture
  29. connect(myTimer,&QTimer::timeout,[this](){
  30. captureScreen();
  31. });
  32. myTimer->start(60000);
  33. //set ui data
  34. webWidget = new QWebEngineView(this);
  35. QNetworkProxyFactory::setUseSystemConfiguration(false);
  36. QVBoxLayout *mainLayout = new QVBoxLayout;
  37. mainLayout->addWidget(webWidget);
  38. // setLayout(mainLayout);
  39. QWidget* widget = new QWidget(this);
  40. widget->setLayout(mainLayout);
  41. this->setCentralWidget(widget);
  42. setWindowState(Qt::WindowMaximized);//最大化
  43. QString str = "http://118.190.47.230:9095/#/login";
  44. loadNavigate(str);
  45. HANDLE hCurrentCursor = GetForegroundWindow();
  46. qDebug()<<hCurrentCursor<<endl;
  47. // EnumWindows(StaticEnumWindowsProc,reinterpret_cast<LPARAM>(this));
  48. PROCESSENTRY32 pe32;
  49. //再使用这个结构体前先定义它的大小
  50. pe32.dwSize=sizeof(pe32);
  51. //给系统内的所有进程拍一个快照
  52. HANDLE hProcessSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  53. if (hProcessSnap==INVALID_HANDLE_VALUE)
  54. {
  55. printf("Create ToolHelp32Snaphhot调用失败!\n");
  56. }
  57. BOOL bMore=::Process32First(hProcessSnap,&pe32);
  58. ::CloseHandle(hProcessSnap);
  59. }
  60. BOOL Home::StaticEnumWindowsProc(HWND hwnd, LPARAM lParam)
  61. {
  62. //窗口是否可视
  63. if (!::IsWindowVisible(hwnd))
  64. return TRUE;
  65. //窗口是否可激活
  66. if (!::IsWindowEnabled(hwnd))
  67. return TRUE;
  68. //确定给定窗口是否是最小化(图标化)的窗口。
  69. //if(IsIconic(hwnd))
  70. // return TRUE;
  71. //窗口是否具有父窗口?
  72. HWND hParent = (HWND)GetWindowLong(hwnd, -8);
  73. //父窗口是否可激活?
  74. //据 Spy++ 观察,如“运行”对话框等被应列入列表的程序有一个隐藏的,具有 WS_DISABLED 的父窗口
  75. if (IsWindowEnabled(hParent))
  76. return TRUE;
  77. //父窗口是否可视?
  78. if (IsWindowVisible(hParent))
  79. return TRUE;
  80. LONG gwl_style = GetWindowLong(hwnd, GWL_STYLE);
  81. if ((gwl_style & WS_POPUP) && !(gwl_style & WS_CAPTION))
  82. return TRUE;
  83. CHAR caption[256];
  84. memset(caption, 0, sizeof(caption));
  85. ::GetWindowTextA(hwnd, caption, 255);
  86. if (strcmp(caption, ""))
  87. {
  88. if (GetClassLong(hwnd, -14))
  89. {
  90. HICON hIcon =(HICON)GetClassLong(hwnd, -14);
  91. if(hIcon)
  92. {
  93. // handleIcon.push_back(hIcon);
  94. // handleWindow.push_back(hwnd);
  95. qDebug()<<"hIcon="<<hIcon<<", title="<<GetClassLong(hwnd, -8);
  96. }
  97. }
  98. } else {
  99. qDebug() <<"title=="<< caption;
  100. }
  101. return TRUE;
  102. }
  103. void Home::captureScreen() {
  104. QScreen *screen = QGuiApplication::primaryScreen();
  105. QString filePathName = "timeManager_screencap.jpg";
  106. QFile oldFile(filePathName);
  107. if (oldFile.exists()) {
  108. QImage oldImg(filePathName);
  109. uchar* data = oldImg.bits();
  110. int height = oldImg.height();
  111. int width = oldImg.width();
  112. screen->grabWindow(0).save(filePathName, "jpg"); // 0值为整个电脑屏幕WId
  113. //判断图像是否和之前的那个一样
  114. QImage curImg = QImage(filePathName);
  115. int curHeight = curImg.height();
  116. int curWidth = curImg.width();
  117. uchar* curData = curImg.bits();
  118. int similarPercent = 0;
  119. if (height == curHeight && width == curWidth) {//必须先满足尺寸一致
  120. unsigned char r,g,b,curR, curG, curB;
  121. int totalPixel = 0;
  122. int samePixel = 0;
  123. for (int i=0;i<height;i++)
  124. {
  125. for (int j=0;j<width;j++)
  126. {
  127. totalPixel++;
  128. r = *(data+2);
  129. g = *(data+1);
  130. b = *data;
  131. //判断像素点的值, 一致的数量
  132. curR = *(curData+2);
  133. curG = *(curData+1);
  134. curB = *curData;
  135. if (r == curR && g == curG && b == curB) {
  136. samePixel++;
  137. }
  138. data+=4;
  139. curData +=4;
  140. }
  141. }
  142. similarPercent = samePixel *100/totalPixel;
  143. qDebug() <<"similar percent = "<<similarPercent<<"%";
  144. } else {
  145. similarPercent = 0;
  146. }
  147. if (similarPercent >= 95) {//相似度90%以上,认为是一样的,不要上传
  148. qDebug() << "不上传!!!";
  149. return;
  150. }
  151. } else {
  152. //直接保存
  153. screen->grabWindow(0).save(filePathName, "jpg");
  154. }
  155. //start upload file
  156. QNetworkRequest request;
  157. request.setUrl(QUrl(SERVER_URL + "/imageProcessing/saveAndProcessImage"));
  158. QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
  159. QHttpPart imagePart;
  160. imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));//如果是png图片填image/png
  161. imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"image.jpg\""));
  162. // imagePart.setRawHeader("Content-ID", "my@content.id"); // 添加任何你喜欢的 headers
  163. QFile *file = new QFile(filePathName);
  164. file->open(QIODevice::ReadOnly);
  165. imagePart.setBodyDevice(file);
  166. multiPart->append(imagePart);
  167. //text paramter
  168. QHttpPart textPart;
  169. textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"uid\""));
  170. textPart.setBody("2");
  171. multiPart->append(textPart);
  172. QHttpPart textPart2;
  173. textPart2.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"indate\""));
  174. QString indate = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
  175. textPart2.setBody(indate.toUtf8());
  176. multiPart->append(textPart2);
  177. QString processList = getProcessList();
  178. // qDebug() << processList ;
  179. QHttpPart textPart3;
  180. textPart3.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"processList\""));
  181. textPart3.setBody(processList.toUtf8());
  182. multiPart->append(textPart3);
  183. QNetworkAccessManager *m_pNetWorkManager = new QNetworkAccessManager();
  184. m_pNetWorkManager->post(request, multiPart);
  185. }
  186. QString Home::getProcessList() {
  187. QProcess process;
  188. process.start("tasklist.exe");
  189. if(process.waitForFinished())
  190. {
  191. QByteArray result = process.readAll();
  192. return result;
  193. }
  194. return NULL;
  195. }
  196. void Home::loadNavigate(QString sUrl)
  197. {
  198. webWidget->load(sUrl);
  199. }
  200. Home::~Home()
  201. {
  202. delete ui;
  203. }