home.cpp 7.9 KB

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