home.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. QSettings *qs = new QSettings("MyManager",QSettings::IniFormat,this);
  44. qs->beginGroup("user");
  45. QString id = qs->value("id").toString();
  46. qDebug() << "id=="<<id<<", phone="<<qs->value("name").toString();
  47. QString str = "http://118.190.47.230:9092/#/desktop/" + id;
  48. loadNavigate(str);
  49. HANDLE hCurrentCursor = GetForegroundWindow();
  50. qDebug()<<hCurrentCursor<<endl;
  51. // EnumWindows(StaticEnumWindowsProc,reinterpret_cast<LPARAM>(this));
  52. PROCESSENTRY32 pe32;
  53. //再使用这个结构体前先定义它的大小
  54. pe32.dwSize=sizeof(pe32);
  55. //给系统内的所有进程拍一个快照
  56. HANDLE hProcessSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  57. if (hProcessSnap==INVALID_HANDLE_VALUE)
  58. {
  59. printf("Create ToolHelp32Snaphhot调用失败!\n");
  60. }
  61. BOOL bMore=::Process32First(hProcessSnap,&pe32);
  62. ::CloseHandle(hProcessSnap);
  63. }
  64. BOOL Home::StaticEnumWindowsProc(HWND hwnd, LPARAM lParam)
  65. {
  66. //窗口是否可视
  67. if (!::IsWindowVisible(hwnd))
  68. return TRUE;
  69. //窗口是否可激活
  70. if (!::IsWindowEnabled(hwnd))
  71. return TRUE;
  72. //确定给定窗口是否是最小化(图标化)的窗口。
  73. //if(IsIconic(hwnd))
  74. // return TRUE;
  75. //窗口是否具有父窗口?
  76. HWND hParent = (HWND)GetWindowLong(hwnd, -8);
  77. //父窗口是否可激活?
  78. //据 Spy++ 观察,如“运行”对话框等被应列入列表的程序有一个隐藏的,具有 WS_DISABLED 的父窗口
  79. if (IsWindowEnabled(hParent))
  80. return TRUE;
  81. //父窗口是否可视?
  82. if (IsWindowVisible(hParent))
  83. return TRUE;
  84. LONG gwl_style = GetWindowLong(hwnd, GWL_STYLE);
  85. if ((gwl_style & WS_POPUP) && !(gwl_style & WS_CAPTION))
  86. return TRUE;
  87. CHAR caption[256];
  88. memset(caption, 0, sizeof(caption));
  89. ::GetWindowTextA(hwnd, caption, 255);
  90. if (strcmp(caption, ""))
  91. {
  92. if (GetClassLong(hwnd, -14))
  93. {
  94. HICON hIcon =(HICON)GetClassLong(hwnd, -14);
  95. if(hIcon)
  96. {
  97. // handleIcon.push_back(hIcon);
  98. // handleWindow.push_back(hwnd);
  99. qDebug()<<"hIcon="<<hIcon<<", title="<<GetClassLong(hwnd, -8);
  100. }
  101. }
  102. } else {
  103. qDebug() <<"title=="<< caption;
  104. }
  105. return TRUE;
  106. }
  107. void Home::captureScreen() {
  108. QScreen *screen = QGuiApplication::primaryScreen();
  109. QString filePathName = "timeManager_screencap.jpg";
  110. QFile oldFile(filePathName);
  111. if (oldFile.exists()) {
  112. QImage oldImg(filePathName);
  113. uchar* data = oldImg.bits();
  114. int height = oldImg.height();
  115. int width = oldImg.width();
  116. screen->grabWindow(0).save(filePathName, "jpg"); // 0值为整个电脑屏幕WId
  117. //判断图像是否和之前的那个一样
  118. QImage curImg = QImage(filePathName);
  119. int curHeight = curImg.height();
  120. int curWidth = curImg.width();
  121. uchar* curData = curImg.bits();
  122. int similarPercent = 0;
  123. if (height == curHeight && width == curWidth) {//必须先满足尺寸一致
  124. unsigned char r,g,b,curR, curG, curB;
  125. int totalPixel = 0;
  126. int samePixel = 0;
  127. for (int i=0;i<height;i++)
  128. {
  129. for (int j=0;j<width;j++)
  130. {
  131. totalPixel++;
  132. r = *(data+2);
  133. g = *(data+1);
  134. b = *data;
  135. //判断像素点的值, 一致的数量
  136. curR = *(curData+2);
  137. curG = *(curData+1);
  138. curB = *curData;
  139. if (r == curR && g == curG && b == curB) {
  140. samePixel++;
  141. }
  142. data+=4;
  143. curData +=4;
  144. }
  145. }
  146. similarPercent = samePixel *100/totalPixel;
  147. qDebug() <<"similar percent = "<<similarPercent<<"%";
  148. } else {
  149. similarPercent = 0;
  150. }
  151. if (similarPercent >= 95) {//相似度90%以上,认为是一样的,不要上传
  152. qDebug() << "不上传!!!";
  153. return;
  154. }
  155. } else {
  156. //直接保存
  157. screen->grabWindow(0).save(filePathName, "jpg");
  158. }
  159. //start upload file
  160. QNetworkRequest request;
  161. request.setUrl(QUrl(SERVER_URL + "/imageProcessing/saveAndProcessImage"));
  162. QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
  163. QHttpPart imagePart;
  164. imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));//如果是png图片填image/png
  165. imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"image.jpg\""));
  166. // imagePart.setRawHeader("Content-ID", "my@content.id"); // 添加任何你喜欢的 headers
  167. QFile *file = new QFile(filePathName);
  168. file->open(QIODevice::ReadOnly);
  169. imagePart.setBodyDevice(file);
  170. multiPart->append(imagePart);
  171. //text paramter
  172. QHttpPart textPart;
  173. textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"uid\""));
  174. QSettings *qs = new QSettings("MyManager",QSettings::IniFormat,this);
  175. qs->beginGroup("user");
  176. QString id = qs->value("id").toString();
  177. textPart.setBody(id.toUtf8());
  178. multiPart->append(textPart);
  179. QHttpPart textPart2;
  180. textPart2.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"indate\""));
  181. QString indate = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
  182. textPart2.setBody(indate.toUtf8());
  183. multiPart->append(textPart2);
  184. QString processList = getProcessList();
  185. // qDebug() << processList ;
  186. QHttpPart textPart3;
  187. textPart3.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"processList\""));
  188. textPart3.setBody(processList.toUtf8());
  189. multiPart->append(textPart3);
  190. QNetworkAccessManager *m_pNetWorkManager = new QNetworkAccessManager();
  191. m_pNetWorkManager->post(request, multiPart);
  192. }
  193. QString Home::getProcessList() {
  194. QProcess process;
  195. process.start("tasklist.exe");
  196. if(process.waitForFinished())
  197. {
  198. QByteArray result = process.readAll();
  199. return result;
  200. }
  201. return NULL;
  202. }
  203. void Home::loadNavigate(QString sUrl)
  204. {
  205. webWidget->load(sUrl);
  206. }
  207. Home::~Home()
  208. {
  209. delete ui;
  210. }
  211. void Home::closeEvent ( QCloseEvent * e )
  212. {
  213. if( QMessageBox::question(this,
  214. tr("Quit"),
  215. tr("Are you sure to quit this application?"),
  216. QMessageBox::Yes, QMessageBox::No )
  217. == QMessageBox::Yes){
  218. e->accept();//不会将事件传递给组件的父组件
  219. qDebug()<<"ok";
  220. }
  221. else
  222. e->ignore();
  223. }