MFCApplication1Dlg.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. 
  2. // MFCApplication1Dlg.cpp: 实现文件
  3. //
  4. #include "pch.h"
  5. #include "framework.h"
  6. #include "MFCApplication1.h"
  7. #include "MFCApplication1Dlg.h"
  8. #include "afxdialogex.h"
  9. #include "CvxText.h"
  10. #include "BaseUtility.h"
  11. #include "libpdf.h"
  12. #include "AssignWords.h"
  13. #include "lib_common_depence.h"
  14. #include <direct.h>
  15. #include <io.h>
  16. #include "Buffers.h"
  17. #include "Config.h"
  18. #include <curl/curl.h>
  19. #if defined(GNUC)
  20. #pragma GCC diagnostic push
  21. #pragma GCC diagnostic ignored “-Wdeprecated-declarations”
  22. #elif defined(_MSC_VER)
  23. #pragma warning(disable : 4996)
  24. #endif
  25. #include <json/json.h>
  26. #ifdef _DEBUG
  27. #pragma comment(lib, "libpdfd.lib")
  28. #pragma comment(lib, "lib_common_depence_d.lib")
  29. #else
  30. #pragma comment(lib, "libpdf.lib")
  31. #pragma comment(lib, "lib_common_depence.lib")
  32. #endif
  33. #ifdef _DEBUG
  34. #define new DEBUG_NEW
  35. #endif
  36. #define SAFETY_CLOSE_HANDLE( handle ) \
  37. if( NULL != handle ) \
  38. { \
  39. CloseHandle( handle ); \
  40. handle = NULL; \
  41. }
  42. #define SAFETY_EXIT_THREAD( hThread, Timeout ) \
  43. if( NULL != hThread ) \
  44. { \
  45. if( WaitForSingleObject( hThread, Timeout ) == WAIT_TIMEOUT ) \
  46. TerminateThread( hThread, -1 ); \
  47. CloseHandle( hThread ); \
  48. hThread = NULL; \
  49. }
  50. // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
  51. class CAboutDlg : public CDialogEx
  52. {
  53. public:
  54. CAboutDlg();
  55. // 对话框数据
  56. #ifdef AFX_DESIGN_TIME
  57. enum { IDD = IDD_ABOUTBOX };
  58. #endif
  59. protected:
  60. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
  61. // 实现
  62. protected:
  63. DECLARE_MESSAGE_MAP()
  64. };
  65. CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
  66. {
  67. }
  68. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  69. {
  70. CDialogEx::DoDataExchange(pDX);
  71. }
  72. BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
  73. END_MESSAGE_MAP()
  74. // CMFCApplication1Dlg 对话框
  75. vector<string> g_vecFileList;//扫描文件列表
  76. int g_scanCount = 0;//扫描文件个数
  77. HANDLE _threadProcess = NULL;
  78. DWORD WINAPI ProcessThread(void *param)
  79. {
  80. CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
  81. std::vector<tuple<string, string>> oneList;
  82. std::vector<tuple<string, string>> twoList;
  83. std::vector<tuple<string, string>> errorList;
  84. std::vector<tuple<string, string>> fourList;
  85. int row = 0;
  86. int ret = AssignWordsFromTest(UnicodeToGB2312(pWnd->m_strTextPath), oneList, twoList, errorList, fourList, row);
  87. if (ret < 0)
  88. {
  89. static wchar_t* erroArry[] = { L"未找到答案文件,请校验\r\n", L"题干和答案行数不一致,请确认\r\n",
  90. L"题目文件中第%d行为空,请校验\r\n", L"答案文件中第%d行为空,请校验\r\n" };
  91. wstring erro = L"未知错误";
  92. wchar_t erro2[64] = { 0 };
  93. wchar_t erro3[64] = { 0 };
  94. _stprintf(erro2, erroArry[2], row);
  95. _stprintf(erro3, erroArry[3], row);
  96. switch (ret)
  97. {
  98. default:
  99. break;
  100. case -1:erro = erroArry[0]; break;
  101. case -2:erro = erroArry[1]; break;
  102. case -3:erro = erro2; break;
  103. case -4:erro = erro3; break;
  104. }
  105. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)erro.c_str(), 1);
  106. return 0L;
  107. }
  108. if (ret == 0)
  109. {
  110. for (auto& iter : errorList)
  111. {
  112. wstring erroInfo;
  113. erroInfo.append(L"题目:");
  114. erroInfo.append(CA2T(std::get<0>(iter).c_str()));
  115. erroInfo.append(L"标准答案:");
  116. erroInfo.append(CA2T(std::get<1>(iter).c_str()));
  117. erroInfo.append(L"有问题\r\n");
  118. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)erroInfo.c_str(), 0);
  119. }
  120. }
  121. HMODULE module = GetModuleHandle(0);
  122. TCHAR pFileName[MAX_PATH + 2] = { 0 };
  123. GetModuleFileName(module, pFileName, MAX_PATH);
  124. CString csFullPath(pFileName);
  125. int nPos = csFullPath.ReverseFind(_T('\\'));
  126. CString strFileDir = csFullPath.Left(nPos);
  127. CString strFilePath = strFileDir + L"\\config.ini";
  128. WCHAR wgray[10];
  129. GetPrivateProfileString(L"USER", L"GrayPix", L"100", wgray, 10, strFilePath);
  130. int nLineGrayPix = _wtoi(wgray);
  131. auto CovertPdfFunc = [&](std::vector<tuple<string, string>>& vctFiles, int col, int& pageNum){
  132. std::string strPage1;
  133. for (size_t i = 0; i < vctFiles.size(); i += max_line * col)
  134. {
  135. std::vector<tuple<string, string>> vecSmall;
  136. auto last = std::min(vctFiles.size(), i + max_line * col);
  137. vecSmall.insert(vecSmall.begin(), vctFiles.begin() + i, vctFiles.begin() + last);
  138. std::string strPagePath;
  139. dataCollectionPaper(col, pageNum, 30, nLineGrayPix, false, vecSmall, pWnd->m_strPdfDir, strPagePath);
  140. if (strPage1.length() == 0)
  141. {
  142. strPage1 = strPagePath;
  143. }
  144. else
  145. {
  146. CString strPdfPath;
  147. strPdfPath.Format(L"%s/pdf/%d.pdf", pWnd->m_strPdfDir, pageNum);
  148. PdfNewFromImage({ strPage1, strPagePath }, TstringToGB2312(strPdfPath.GetBuffer()));
  149. strPage1 = "";
  150. CString strMsg = L"";
  151. strMsg.Format(L"%s\r\n", strPdfPath);
  152. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strMsg.GetBuffer(), 0);
  153. strMsg.ReleaseBuffer();
  154. }
  155. pageNum++;
  156. }
  157. if (strPage1.length() > 0)
  158. {
  159. CString strPdfPath;
  160. strPdfPath.Format(L"%s/pdf/%d.pdf", pWnd->m_strPdfDir, pageNum);
  161. PdfNewFromImage({ strPage1 }, TstringToGB2312(strPdfPath.GetBuffer()));
  162. CString strMsg = L"";
  163. strMsg.Format(L"%s\r\n", strPdfPath);
  164. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strMsg.GetBuffer(), 0);
  165. strMsg.ReleaseBuffer();
  166. }
  167. };
  168. int pageNum = _wtoi(pWnd->m_strPageNumber);
  169. CovertPdfFunc(oneList, 1, pageNum);
  170. CovertPdfFunc(twoList, 2, pageNum);
  171. //通知发送消息到窗口
  172. CString strErrorMsg = L"";
  173. strErrorMsg.Format(L"PDF生成完成!\r\n");
  174. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strErrorMsg.GetBuffer(), 1);
  175. strErrorMsg.ReleaseBuffer();
  176. return 0L;
  177. }
  178. const static std::string request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting";
  179. static std::string handwriting_result;
  180. /**
  181. * curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
  182. * @param 参数定义见libcurl文档
  183. * @return 返回值定义见libcurl文档
  184. */
  185. static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream)
  186. {
  187. char * pData = (char *)ptr;
  188. handwriting_result.append(pData, size * nmemb);
  189. // 获取到的body存放在ptr中,先将其转换为string格式
  190. //handwriting_result = std::string((char *)ptr, size * nmemb);
  191. return size * nmemb;
  192. }
  193. /**
  194. * 手写文字识别
  195. * @return 调用成功返回0,发生错误返回其他错误码
  196. */
  197. int handwriting(string strImage, std::string &json_result, const std::string &access_token)
  198. {
  199. handwriting_result = "";
  200. std::string url = request_url + "?access_token=" + access_token;
  201. CURL *curl = NULL;
  202. CURLcode result_code;
  203. int is_success;
  204. curl = curl_easy_init();
  205. if (curl)
  206. {
  207. curl_easy_setopt(curl, CURLOPT_URL, url.data());
  208. curl_easy_setopt(curl, CURLOPT_POST, 1);
  209. curl_httppost *post = NULL;
  210. curl_httppost *last = NULL;
  211. curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, strImage.c_str(), CURLFORM_END);
  212. curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
  213. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
  214. result_code = curl_easy_perform(curl);
  215. if (result_code != CURLE_OK)
  216. {
  217. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  218. curl_easy_strerror(result_code));
  219. is_success = 1;
  220. return is_success;
  221. }
  222. UTF8toANSI(handwriting_result);
  223. json_result = handwriting_result;
  224. curl_easy_cleanup(curl);
  225. is_success = 0;
  226. }
  227. else
  228. {
  229. fprintf(stderr, "curl_easy_init() failed.");
  230. is_success = 1;
  231. }
  232. return is_success;
  233. }
  234. int IdentifyCallback(result::spinfo& pinfo, void* param)
  235. {
  236. CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
  237. if (pinfo.choiceRes.size() < 3 || pinfo.vecUrlAPath.size() == 0)
  238. {
  239. pWnd->PostMessage(WM_PROCESS, NULL, NULL);
  240. pWnd->FormatScanMsg("矫正图像失败原图为:%s", pinfo.vecUrlPath.size() > 0 ? pinfo.vecUrlPath[0].c_str() : "图像为空");
  241. }
  242. std::string strChoiceRes;
  243. for (auto& row : pinfo.choiceRes)
  244. {
  245. for (auto& col : row.vecRes)
  246. {
  247. strChoiceRes.append(col ? "1" : "0");
  248. }
  249. }
  250. int length = strChoiceRes.length();
  251. int number = 0;
  252. for (int i = 0; i < length; ++i)
  253. {
  254. if (strChoiceRes[i] == '1') // 其中注意if的判断两个条件相等的符号
  255. number += pow(2, length - 1 - i);
  256. }
  257. string strToken = CT2A(CConfig::Instance()->m_strToken);
  258. string strResult;
  259. string strEncode = GetImageEncodeString(pinfo.vecUrlAPath[0]);
  260. handwriting(strEncode, strResult, strToken);
  261. std::string strJsonDir = CT2A(CConfig::Instance()->m_strMode);
  262. strJsonDir.append("/");
  263. strJsonDir.append(std::to_string(number));
  264. strJsonDir.append(".json");
  265. std::string strSaveDir = CT2A(CConfig::Instance()->m_strCut);
  266. cutPaper(number, strJsonDir, pinfo.vecUrlAPath[0], strSaveDir);
  267. g_scanCount++;
  268. pWnd->PostMessage(WM_PROCESS, NULL, NULL);
  269. pWnd->FormatScanMsg("生成切割图像:%s", std::to_string(number).c_str());
  270. return 0;
  271. }
  272. enum EnumSearFileType {
  273. file,//扫描文件
  274. dir,//扫描文件夹
  275. };
  276. void GetImages(CString strPath)
  277. {
  278. queue<CString> queuePath;
  279. queuePath.push(strPath);
  280. EnumSearFileType serchType = file;
  281. CFileFind finder;
  282. do
  283. {
  284. bool bFinished = finder.FindFile(queuePath.front() + _T("\\*.JPG"));
  285. while (serchType == file && bFinished)
  286. {
  287. bFinished = finder.FindNextFile();
  288. if (finder.IsDirectory())continue;
  289. CString strPath = finder.GetFilePath();
  290. g_vecFileList.push_back(UnicodeToGB2312(strPath));
  291. }
  292. serchType = dir;
  293. bFinished = finder.FindFile(queuePath.front() + _T("\\*.*"));
  294. while (bFinished)
  295. {
  296. bFinished = finder.FindNextFile();
  297. if (finder.IsDirectory() && !finder.IsDots())
  298. {
  299. queuePath.push(finder.GetFilePath());
  300. }
  301. }
  302. queuePath.pop();
  303. serchType = file;
  304. } while (queuePath.size() > 0);
  305. }
  306. string GetBatchId()
  307. {
  308. SYSTEMTIME st;
  309. GetLocalTime(&st);
  310. char chBuf[24] = { 0 };
  311. sprintf(chBuf, "%02d%02d%02d%02d%02d%02d",
  312. st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
  313. return chBuf;
  314. }
  315. HANDLE _threadScan = NULL;
  316. char* g_strIdentifyMsg[7] = { "正常","内部error","批次号异常","保存路径异常","模板为空","图像列表异常","图像列表不是模板整数倍" };
  317. DWORD WINAPI ScanThread(void *param)
  318. {
  319. g_scanCount = 0;
  320. g_vecFileList.clear();
  321. GetImages(CConfig::Instance()->m_strImg);
  322. CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
  323. pWnd->PostMessage(WM_PROCESS, NULL, NULL);
  324. pWnd->FormatScanMsg("启动扫描线程......");
  325. auto exitfun = [&]() {
  326. pWnd->FormatScanMsg("扫描线程退出。");
  327. SendMessage(pWnd->m_hWnd, WM_SHOWSCANINFO, 0, 1);
  328. };
  329. preinfo::templatesInfo temeplatInfo;
  330. preinfo::SubjChiInfo sujChiInfo;
  331. std::string strTempDir = CT2A(CConfig::Instance()->m_strCut);
  332. /*if (_access(strTempDir.c_str(), 0) == -1)
  333. {
  334. _mkdir(strTempDir.c_str());
  335. }*/
  336. int nRet = PareseModeJson(temeplatInfo);
  337. if (nRet)
  338. {
  339. pWnd->FormatScanMsg("模板解析失败!");
  340. exitfun();
  341. return 0;
  342. }
  343. //生成批次号
  344. string strBatchId = GetBatchId();
  345. pWnd->FormatScanMsg("创建批次id:%s", strBatchId.c_str());
  346. nRet = api_processing_images(strBatchId.c_str(),
  347. strTempDir.c_str(),
  348. temeplatInfo,
  349. sujChiInfo,
  350. g_vecFileList,
  351. LOG_AUTO,
  352. false,
  353. OLNS,
  354. IdentifyCallback,
  355. NULL);
  356. ////启动算法扫描功能
  357. if (nRet != RT_OK)
  358. {
  359. if (nRet >= 0 && nRet <= 6)
  360. pWnd->FormatScanMsg("扫描失败,错误描述:%s, errorCode:%d", g_strIdentifyMsg[nRet], nRet);
  361. else
  362. pWnd->FormatScanMsg("扫描失败,errorCode:%d", nRet);
  363. }
  364. exitfun();
  365. return 0L;
  366. }
  367. CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=nullptr*/)
  368. : CDialogEx(IDD_MFCAPPLICATION1_DIALOG, pParent)
  369. , m_strMsg(_T("")), m_strScanMsg(_T(""))
  370. {
  371. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  372. }
  373. void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
  374. {
  375. CDialogEx::DoDataExchange(pDX);
  376. DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
  377. DDX_Text(pDX, IDC_EDIT_SCANMSG, m_strScanMsg);
  378. }
  379. BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
  380. ON_WM_SYSCOMMAND()
  381. ON_WM_PAINT()
  382. ON_WM_QUERYDRAGICON()
  383. ON_BN_CLICKED(IDOK, &CMFCApplication1Dlg::OnBnClickedOk)
  384. ON_MESSAGE(WM_SHOWINFO, OnShowInfo)
  385. ON_MESSAGE(WM_SHOWSCANINFO, OnShowScanInfo)
  386. ON_MESSAGE(WM_PROCESS, OnProcessInfo)
  387. ON_BN_CLICKED(IDCANCEL, &CMFCApplication1Dlg::OnBnClickedCancel)
  388. ON_BN_CLICKED(IDC_BTN_TXT, &CMFCApplication1Dlg::OnBnClickedBtnTxt)
  389. ON_BN_CLICKED(IDC_BTN_PDF, &CMFCApplication1Dlg::OnBnClickedBtnPdf)
  390. ON_BN_CLICKED(IDC_BTN_MODEPATH, &CMFCApplication1Dlg::OnBnClickedBtnModepath)
  391. ON_BN_CLICKED(IDC_BTN_IMGPATH, &CMFCApplication1Dlg::OnBnClickedBtnImgpath)
  392. ON_BN_CLICKED(IDC_BTN_CUTPATH, &CMFCApplication1Dlg::OnBnClickedBtnCutpath)
  393. ON_BN_CLICKED(ID_BTN_SCAN, &CMFCApplication1Dlg::OnBnClickedBtnScan)
  394. END_MESSAGE_MAP()
  395. // CMFCApplication1Dlg 消息处理程序
  396. BOOL CMFCApplication1Dlg::OnInitDialog()
  397. {
  398. CDialogEx::OnInitDialog();
  399. // 将“关于...”菜单项添加到系统菜单中。
  400. // IDM_ABOUTBOX 必须在系统命令范围内。
  401. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  402. ASSERT(IDM_ABOUTBOX < 0xF000);
  403. CMenu* pSysMenu = GetSystemMenu(FALSE);
  404. if (pSysMenu != nullptr)
  405. {
  406. BOOL bNameValid;
  407. CString strAboutMenu;
  408. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  409. ASSERT(bNameValid);
  410. if (!strAboutMenu.IsEmpty())
  411. {
  412. pSysMenu->AppendMenu(MF_SEPARATOR);
  413. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  414. }
  415. }
  416. // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
  417. // 执行此操作
  418. SetIcon(m_hIcon, TRUE); // 设置大图标
  419. SetIcon(m_hIcon, FALSE); // 设置小图标
  420. CConfig::Instance()->LoadFromINI();
  421. GetDlgItem(IDC_EDIT_TXT)->SetWindowText(CConfig::Instance()->m_strTxt);
  422. GetDlgItem(IDC_EDIT_PDF)->SetWindowText(CConfig::Instance()->m_strPdf);
  423. GetDlgItem(IDC_EDIT_MODEPATH)->SetWindowText(CConfig::Instance()->m_strMode);
  424. GetDlgItem(IDC_EDIT_IMGPATH)->SetWindowText(CConfig::Instance()->m_strImg);
  425. GetDlgItem(IDC_EDIT_CUTPATH)->SetWindowText(CConfig::Instance()->m_strCut);
  426. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  427. }
  428. void CMFCApplication1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
  429. {
  430. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  431. {
  432. CAboutDlg dlgAbout;
  433. dlgAbout.DoModal();
  434. }
  435. else
  436. {
  437. CDialogEx::OnSysCommand(nID, lParam);
  438. }
  439. }
  440. // 如果向对话框添加最小化按钮,则需要下面的代码
  441. // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
  442. // 这将由框架自动完成。
  443. void CMFCApplication1Dlg::OnPaint()
  444. {
  445. if (IsIconic())
  446. {
  447. CPaintDC dc(this); // 用于绘制的设备上下文
  448. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  449. // 使图标在工作区矩形中居中
  450. int cxIcon = GetSystemMetrics(SM_CXICON);
  451. int cyIcon = GetSystemMetrics(SM_CYICON);
  452. CRect rect;
  453. GetClientRect(&rect);
  454. int x = (rect.Width() - cxIcon + 1) / 2;
  455. int y = (rect.Height() - cyIcon + 1) / 2;
  456. // 绘制图标
  457. dc.DrawIcon(x, y, m_hIcon);
  458. }
  459. else
  460. {
  461. CDialogEx::OnPaint();
  462. }
  463. }
  464. //当用户拖动最小化窗口时系统调用此函数取得光标
  465. //显示。
  466. HCURSOR CMFCApplication1Dlg::OnQueryDragIcon()
  467. {
  468. return static_cast<HCURSOR>(m_hIcon);
  469. }
  470. void CMFCApplication1Dlg::OnBnClickedOk()
  471. {
  472. ((CEdit*)GetDlgItem(IDC_EDIT_TXT))->GetWindowText(m_strTextPath);
  473. ((CEdit*)GetDlgItem(IDC_EDIT_PDF))->GetWindowText(m_strPdfDir);
  474. ((CEdit*)GetDlgItem(IDC_EDIT_PAGE_NUMBER))->GetWindowText(m_strPageNumber);
  475. if (m_strTextPath.GetLength() == 0)
  476. {
  477. AfxMessageBox(L"语料库文件未选择");
  478. return;
  479. }
  480. if (m_strPdfDir.GetLength() == 0)
  481. {
  482. AfxMessageBox(L"PDF文件夹未选择");
  483. return;
  484. }
  485. m_strPageNumber.Trim();
  486. if (m_strPageNumber.GetLength() == 0)
  487. {
  488. AfxMessageBox(L"启始页码未输入");
  489. return;
  490. }
  491. if (m_strPageNumber.SpanIncluding(_T("0123456789")) != m_strPageNumber)
  492. {
  493. AfxMessageBox(L"启始页码不是数字");
  494. return;
  495. }
  496. if (_wtoi(m_strPageNumber.GetBuffer()) > 20000)
  497. {
  498. AfxMessageBox(L"启始页码小于20000");
  499. return;
  500. }
  501. GetDlgItem(IDOK)->EnableWindow(FALSE);
  502. m_strMsg = "";
  503. CString strJsonDir(m_strPdfDir);
  504. strJsonDir.Append(L"\\json");
  505. if (!PathIsDirectory(strJsonDir))
  506. {
  507. ::CreateDirectory(strJsonDir, 0);
  508. }
  509. CString strPdfDir(m_strPdfDir);
  510. strPdfDir.Append(L"\\pdf");
  511. if (!PathIsDirectory(strPdfDir))
  512. {
  513. ::CreateDirectory(strPdfDir, 0);
  514. }
  515. SAFETY_EXIT_THREAD(_threadProcess, 100);
  516. _threadProcess = CreateThread(NULL, 0, ProcessThread, NULL, 0, NULL);
  517. //CDialogEx::OnOK();
  518. }
  519. void CMFCApplication1Dlg::UpdateMsg(CString strMsg)
  520. {
  521. m_strMsg += strMsg;
  522. UpdateData(FALSE);
  523. }
  524. void CMFCApplication1Dlg::UpdateScanMsg(CString strMsg)
  525. {
  526. m_strScanMsg += strMsg;
  527. UpdateData(FALSE);
  528. }
  529. LRESULT CMFCApplication1Dlg::OnShowInfo(WPARAM wParam, LPARAM lParam)
  530. {
  531. wchar_t* p = (wchar_t*)wParam;
  532. CString str;
  533. str.Format(_T("%s"), p);
  534. m_strMsg += str;
  535. UpdateData(FALSE);
  536. if (lParam == 1)
  537. {
  538. GetDlgItem(IDOK)->EnableWindow(TRUE);
  539. }
  540. return 0;
  541. }
  542. LRESULT CMFCApplication1Dlg::OnShowScanInfo(WPARAM wParam, LPARAM lParam)
  543. {
  544. if (lParam == 1)
  545. {
  546. GetDlgItem(ID_BTN_SCAN)->EnableWindow(TRUE);
  547. return 0;
  548. }
  549. wchar_t* p = (wchar_t*)wParam;
  550. CString str;
  551. str.Format(_T("%s"), p);
  552. m_strScanMsg += str;
  553. UpdateData(FALSE);
  554. return 0;
  555. }
  556. LRESULT CMFCApplication1Dlg::OnProcessInfo(WPARAM wParam, LPARAM lParam)
  557. {
  558. CString strProcess = _T("--");
  559. if (g_vecFileList.size())
  560. {
  561. float fProcess = (float)g_scanCount / (float)g_vecFileList.size();
  562. strProcess.Format(L"%0.0lf%%", fProcess*100);
  563. }
  564. SetDlgItemText(IDC_EDIT_SCANPROCESS, strProcess);
  565. return 0;
  566. }
  567. void CMFCApplication1Dlg::OnBnClickedCancel()
  568. {
  569. SAFETY_EXIT_THREAD(_threadProcess, 100);
  570. SAFETY_EXIT_THREAD(_threadScan, 100);
  571. CConfig::Instance()->SaveSetting();
  572. CDialogEx::OnCancel();
  573. }
  574. void CMFCApplication1Dlg::SelectFolder(HWND hwnd, CString& lpszFolder)
  575. {
  576. TCHAR szFolderPath[MAX_PATH];
  577. szFolderPath[0] = 0;
  578. BROWSEINFO sInfo;
  579. ::ZeroMemory(&sInfo, sizeof(BROWSEINFO));
  580. sInfo.pidlRoot = 0;
  581. sInfo.lpszTitle = _T("请选择一个文件夹");
  582. sInfo.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_EDITBOX;
  583. sInfo.lpfn = NULL;
  584. sInfo.hwndOwner = hwnd;
  585. LPITEMIDLIST lpidlBrowse = ::SHBrowseForFolder(&sInfo);
  586. if (lpidlBrowse != NULL) {
  587. if (::SHGetPathFromIDList(lpidlBrowse, szFolderPath)) {
  588. lpszFolder = szFolderPath;
  589. lpszFolder.Replace('\\', '/');
  590. }
  591. ::CoTaskMemFree(lpidlBrowse);
  592. }
  593. }
  594. void CMFCApplication1Dlg::SelectFile(CString& lpszFile)
  595. {
  596. CString OpenFilter = _T("文本文件(*.txt)|*.txt;|");
  597. CFileDialog FileOpen(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, OpenFilter);
  598. if (IDOK == FileOpen.DoModal())
  599. {
  600. lpszFile = FileOpen.GetPathName();
  601. lpszFile.Replace('\\', '/');
  602. }
  603. }
  604. void CMFCApplication1Dlg::OnBnClickedBtnTxt()
  605. {
  606. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_TXT);
  607. if (!pEdit)
  608. {
  609. return;
  610. }
  611. CString strText;
  612. SelectFile(strText);
  613. if (strText.GetLength() == 0)
  614. {
  615. return;
  616. }
  617. pEdit->SetWindowText(strText);
  618. CConfig::Instance()->m_strTxt = strText;
  619. }
  620. void CMFCApplication1Dlg::OnBnClickedBtnPdf()
  621. {
  622. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_PDF);
  623. if (!pEdit)
  624. {
  625. return;
  626. }
  627. CString strText;
  628. SelectFolder(GetSafeHwnd(), strText);
  629. if (strText.GetLength() == 0)
  630. {
  631. return;
  632. }
  633. pEdit->SetWindowText(strText);
  634. CConfig::Instance()->m_strPdf = strText;
  635. }
  636. void CMFCApplication1Dlg::OnBnClickedBtnModepath()
  637. {
  638. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_MODEPATH);
  639. if (!pEdit)
  640. {
  641. return;
  642. }
  643. CString strText;
  644. SelectFolder(GetSafeHwnd(), strText);
  645. if (strText.GetLength() == 0)
  646. {
  647. return;
  648. }
  649. pEdit->SetWindowText(strText);
  650. CConfig::Instance()->m_strMode = strText;
  651. }
  652. void CMFCApplication1Dlg::OnBnClickedBtnImgpath()
  653. {
  654. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_IMGPATH);
  655. if (!pEdit)
  656. {
  657. return;
  658. }
  659. CString strText;
  660. SelectFolder(GetSafeHwnd(), strText);
  661. if (strText.GetLength() == 0)
  662. {
  663. return;
  664. }
  665. pEdit->SetWindowText(strText);
  666. CConfig::Instance()->m_strImg = strText;
  667. }
  668. void CMFCApplication1Dlg::OnBnClickedBtnCutpath()
  669. {
  670. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CUTPATH);
  671. if (!pEdit)
  672. {
  673. return;
  674. }
  675. CString strText;
  676. SelectFolder(GetSafeHwnd(), strText);
  677. if (strText.GetLength() == 0)
  678. {
  679. return;
  680. }
  681. pEdit->SetWindowText(strText);
  682. CConfig::Instance()->m_strCut = strText;
  683. }
  684. void CMFCApplication1Dlg::OnBnClickedBtnScan()
  685. {
  686. ((CEdit*)GetDlgItem(IDC_EDIT_MODEPATH))->GetWindowText(m_strModePath);
  687. ((CEdit*)GetDlgItem(IDC_EDIT_IMGPATH))->GetWindowText(m_strImgPath);
  688. ((CEdit*)GetDlgItem(IDC_EDIT_CUTPATH))->GetWindowText(m_strCutPath);
  689. if (m_strModePath.GetLength() == 0)
  690. {
  691. AfxMessageBox(L"模板文件夹未选择");
  692. return;
  693. }
  694. if (m_strImgPath.GetLength() == 0)
  695. {
  696. AfxMessageBox(L"答题卡文件夹未选择");
  697. return;
  698. }
  699. if (m_strCutPath.GetLength() == 0)
  700. {
  701. AfxMessageBox(L"切割生成文件夹未选择");
  702. return;
  703. }
  704. GetDlgItem(ID_BTN_SCAN)->EnableWindow(FALSE);
  705. m_strScanMsg = "";
  706. CString strNormal(m_strCutPath);
  707. strNormal.Append(L"/normal");
  708. if (!PathIsDirectory(strNormal))
  709. {
  710. ::CreateDirectory(strNormal, 0);
  711. }
  712. CString strAbnormal(m_strCutPath);
  713. strAbnormal.Append(L"/abnormal");
  714. if (!PathIsDirectory(strAbnormal))
  715. {
  716. ::CreateDirectory(strAbnormal, 0);
  717. }
  718. SAFETY_EXIT_THREAD(_threadScan, 100);
  719. _threadScan = CreateThread(NULL, 0, ScanThread, NULL, 0, NULL);
  720. }
  721. void CMFCApplication1Dlg::FormatScanMsg(const char *szFmt, ...)
  722. {
  723. if (!szFmt)
  724. return;
  725. va_list ap;
  726. va_start(ap, szFmt);
  727. size_t nLength = _vsnprintf(nullptr, 0, szFmt, ap) + 1;
  728. CBuffers buffer;
  729. char* szLogMsg = buffer.GetBuf(nLength);
  730. _vsnprintf(szLogMsg, nLength, szFmt, ap);
  731. va_end(ap);
  732. SYSTEMTIME st;
  733. GetLocalTime(&st);
  734. char chBuf[24] = { 0 };
  735. sprintf(chBuf, "%02d-%02d-%02d %02d:%02d:%02d ",
  736. st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
  737. CString strMsg;
  738. // 扫描异常
  739. strMsg.Format(L"%s %s\r\n", GB2312ToTstring(chBuf).c_str(), GB2312ToTstring(szLogMsg).c_str());
  740. SendMessage(WM_SHOWSCANINFO, (WPARAM)strMsg.GetBuffer(), 0);
  741. }