MFCApplication1Dlg.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. CString strTipsPath = strFileDir + "\\tips.txt";
  132. std::string strTips;
  133. GetPaperTips(UnicodeToGB2312(strTipsPath), strTips);
  134. auto CovertPdfFunc = [&](std::vector<tuple<string, string>>& vctFiles, int col, int& pageNum){
  135. std::string strPage1;
  136. for (size_t i = 0; i < vctFiles.size(); i += max_line * col)
  137. {
  138. std::vector<tuple<string, string>> vecSmall;
  139. auto last = std::min(vctFiles.size(), i + max_line * col);
  140. vecSmall.insert(vecSmall.begin(), vctFiles.begin() + i, vctFiles.begin() + last);
  141. std::string strPagePath;
  142. dataCollectionPaper(col, pageNum, 30, nLineGrayPix, false, vecSmall, pWnd->m_strPdfDir, strPagePath, strTips);
  143. if (strPage1.length() == 0)
  144. {
  145. strPage1 = strPagePath;
  146. }
  147. else
  148. {
  149. CString strPdfPath;
  150. strPdfPath.Format(L"%s/pdf/%d.pdf", pWnd->m_strPdfDir, pageNum);
  151. PdfNewFromImage({ strPage1, strPagePath }, TstringToGB2312(strPdfPath.GetBuffer()));
  152. strPage1 = "";
  153. CString strMsg = L"";
  154. strMsg.Format(L"%s\r\n", strPdfPath);
  155. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strMsg.GetBuffer(), 0);
  156. strMsg.ReleaseBuffer();
  157. }
  158. pageNum++;
  159. }
  160. if (strPage1.length() > 0)
  161. {
  162. CString strPdfPath;
  163. strPdfPath.Format(L"%s/pdf/%d.pdf", pWnd->m_strPdfDir, pageNum);
  164. PdfNewFromImage({ strPage1 }, TstringToGB2312(strPdfPath.GetBuffer()));
  165. CString strMsg = L"";
  166. strMsg.Format(L"%s\r\n", strPdfPath);
  167. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strMsg.GetBuffer(), 0);
  168. strMsg.ReleaseBuffer();
  169. }
  170. };
  171. int pageNum = _wtoi(pWnd->m_strPageNumber);
  172. CovertPdfFunc(oneList, 1, pageNum);
  173. CovertPdfFunc(twoList, 2, pageNum);
  174. //通知发送消息到窗口
  175. CString strErrorMsg = L"";
  176. strErrorMsg.Format(L"PDF生成完成!\r\n");
  177. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strErrorMsg.GetBuffer(), 1);
  178. strErrorMsg.ReleaseBuffer();
  179. return 0L;
  180. }
  181. const static std::string request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting";
  182. static std::string handwriting_result;
  183. /**
  184. * curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
  185. * @param 参数定义见libcurl文档
  186. * @return 返回值定义见libcurl文档
  187. */
  188. static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream)
  189. {
  190. char * pData = (char *)ptr;
  191. handwriting_result.append(pData, size * nmemb);
  192. // 获取到的body存放在ptr中,先将其转换为string格式
  193. //handwriting_result = std::string((char *)ptr, size * nmemb);
  194. return size * nmemb;
  195. }
  196. /**
  197. * 手写文字识别
  198. * @return 调用成功返回0,发生错误返回其他错误码
  199. */
  200. int handwriting(string strImage, std::string &json_result, const std::string &access_token)
  201. {
  202. handwriting_result = "";
  203. std::string url = request_url + "?access_token=" + access_token;
  204. CURL *curl = NULL;
  205. CURLcode result_code;
  206. int is_success;
  207. curl = curl_easy_init();
  208. if (curl)
  209. {
  210. curl_easy_setopt(curl, CURLOPT_URL, url.data());
  211. curl_easy_setopt(curl, CURLOPT_POST, 1);
  212. curl_httppost *post = NULL;
  213. curl_httppost *last = NULL;
  214. curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, strImage.c_str(), CURLFORM_END);
  215. curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
  216. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
  217. result_code = curl_easy_perform(curl);
  218. if (result_code != CURLE_OK)
  219. {
  220. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  221. curl_easy_strerror(result_code));
  222. is_success = 1;
  223. return is_success;
  224. }
  225. UTF8toANSI(handwriting_result);
  226. json_result = handwriting_result;
  227. curl_easy_cleanup(curl);
  228. is_success = 0;
  229. }
  230. else
  231. {
  232. fprintf(stderr, "curl_easy_init() failed.");
  233. is_success = 1;
  234. }
  235. return is_success;
  236. }
  237. bool ParseJson(std::string& strJson, vector<std::tuple<cv::Rect, std::string>>& vec)
  238. {
  239. Json::Features features;
  240. Json::Reader reader(features);
  241. Json::Value root;
  242. if (!reader.parse(strJson, root))
  243. {
  244. return false;
  245. }
  246. Json::Value words_result = root["words_result"];
  247. if (!words_result.isArray())
  248. {
  249. return false;
  250. }
  251. for (int i = 0; i < words_result.size(); i++)
  252. {
  253. Json::Value row = words_result[i];
  254. Json::Value location = row["location"];
  255. cv::Rect rc = cv::Rect(location["left"].asInt(), location["top"].asInt(), location["width"].asInt(), location["height"].asInt());
  256. vec.push_back(make_tuple(rc, row["words"].asString()));
  257. }
  258. return true;
  259. }
  260. int IdentifyCallback(result::spinfo& pinfo, void* param)
  261. {
  262. CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
  263. if (pinfo.choiceRes.size() < 3 || pinfo.vecUrlAPath.size() == 0)
  264. {
  265. pWnd->PostMessage(WM_PROCESS, NULL, NULL);
  266. pWnd->FormatScanMsg("矫正图像失败原图为:%s", pinfo.vecUrlPath.size() > 0 ? pinfo.vecUrlPath[0].c_str() : "图像为空");
  267. }
  268. std::string strChoiceRes;
  269. for (auto& row : pinfo.choiceRes)
  270. {
  271. for (auto& col : row.vecRes)
  272. {
  273. strChoiceRes.append(col ? "1" : "0");
  274. }
  275. }
  276. int length = strChoiceRes.length();
  277. int number = 0;
  278. for (int i = 0; i < length; ++i)
  279. {
  280. if (strChoiceRes[i] == '1') // 其中注意if的判断两个条件相等的符号
  281. number += pow(2, length - 1 - i);
  282. }
  283. string strToken = CT2A(CConfig::Instance()->m_strToken);
  284. string strResult;
  285. string strEncode = GetImageEncodeString(pinfo.vecUrlAPath[0]);
  286. vector<std::tuple<cv::Rect, std::string>> vecTranslate;
  287. int trycnt = 3;
  288. bool ret = false;
  289. do
  290. {
  291. handwriting(strEncode, strResult, strToken);
  292. ret = ParseJson(strResult, vecTranslate);
  293. if (ret)
  294. {
  295. break;
  296. }
  297. trycnt--;
  298. } while (trycnt > 0);
  299. if (!ret)
  300. {
  301. pWnd->FormatScanMsg("调用百度接口,生成切割图像失败");
  302. return 0;
  303. }
  304. std::string strJsonDir = CT2A(CConfig::Instance()->m_strMode);
  305. strJsonDir.append("/");
  306. strJsonDir.append(std::to_string(number));
  307. strJsonDir.append(".json");
  308. std::string strSaveDir = CT2A(CConfig::Instance()->m_strCut);
  309. cutPaper(number, strJsonDir, pinfo.vecUrlAPath[0], strSaveDir, vecTranslate);
  310. g_scanCount++;
  311. pWnd->PostMessage(WM_PROCESS, NULL, NULL);
  312. pWnd->FormatScanMsg("生成切割图像:%s", std::to_string(number).c_str());
  313. return 0;
  314. }
  315. enum EnumSearFileType {
  316. file,//扫描文件
  317. dir,//扫描文件夹
  318. };
  319. void GetImages(CString strPath)
  320. {
  321. queue<CString> queuePath;
  322. queuePath.push(strPath);
  323. EnumSearFileType serchType = file;
  324. CFileFind finder;
  325. do
  326. {
  327. bool bFinished = finder.FindFile(queuePath.front() + _T("\\*.JPG"));
  328. while (serchType == file && bFinished)
  329. {
  330. bFinished = finder.FindNextFile();
  331. if (finder.IsDirectory())continue;
  332. CString strPath = finder.GetFilePath();
  333. g_vecFileList.push_back(UnicodeToGB2312(strPath));
  334. }
  335. serchType = dir;
  336. bFinished = finder.FindFile(queuePath.front() + _T("\\*.*"));
  337. while (bFinished)
  338. {
  339. bFinished = finder.FindNextFile();
  340. if (finder.IsDirectory() && !finder.IsDots())
  341. {
  342. queuePath.push(finder.GetFilePath());
  343. }
  344. }
  345. queuePath.pop();
  346. serchType = file;
  347. } while (queuePath.size() > 0);
  348. }
  349. string GetBatchId()
  350. {
  351. SYSTEMTIME st;
  352. GetLocalTime(&st);
  353. char chBuf[24] = { 0 };
  354. sprintf(chBuf, "%02d%02d%02d%02d%02d%02d",
  355. st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
  356. return chBuf;
  357. }
  358. HANDLE _threadScan = NULL;
  359. char* g_strIdentifyMsg[7] = { "正常","内部error","批次号异常","保存路径异常","模板为空","图像列表异常","图像列表不是模板整数倍" };
  360. DWORD WINAPI ScanThread(void *param)
  361. {
  362. g_scanCount = 0;
  363. g_vecFileList.clear();
  364. GetImages(CConfig::Instance()->m_strImg);
  365. CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
  366. pWnd->PostMessage(WM_PROCESS, NULL, NULL);
  367. pWnd->FormatScanMsg("启动扫描线程......");
  368. auto exitfun = [&]() {
  369. pWnd->FormatScanMsg("扫描线程退出。");
  370. SendMessage(pWnd->m_hWnd, WM_SHOWSCANINFO, 0, 1);
  371. };
  372. preinfo::templatesInfo temeplatInfo;
  373. preinfo::SubjChiInfo sujChiInfo;
  374. std::string strTempDir = CT2A(CConfig::Instance()->m_strCut);
  375. /*if (_access(strTempDir.c_str(), 0) == -1)
  376. {
  377. _mkdir(strTempDir.c_str());
  378. }*/
  379. int nRet = PareseModeJson(temeplatInfo);
  380. if (nRet)
  381. {
  382. pWnd->FormatScanMsg("模板解析失败!");
  383. exitfun();
  384. return 0;
  385. }
  386. //生成批次号
  387. string strBatchId = GetBatchId();
  388. pWnd->FormatScanMsg("创建批次id:%s", strBatchId.c_str());
  389. nRet = api_processing_images(strBatchId.c_str(),
  390. strTempDir.c_str(),
  391. temeplatInfo,
  392. sujChiInfo,
  393. g_vecFileList,
  394. LOG_AUTO,
  395. false,
  396. OLNS,
  397. IdentifyCallback,
  398. NULL);
  399. ////启动算法扫描功能
  400. if (nRet != RT_OK)
  401. {
  402. if (nRet >= 0 && nRet <= 6)
  403. pWnd->FormatScanMsg("扫描失败,错误描述:%s, errorCode:%d", g_strIdentifyMsg[nRet], nRet);
  404. else
  405. pWnd->FormatScanMsg("扫描失败,errorCode:%d", nRet);
  406. }
  407. exitfun();
  408. return 0L;
  409. }
  410. CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=nullptr*/)
  411. : CDialogEx(IDD_MFCAPPLICATION1_DIALOG, pParent)
  412. , m_strMsg(_T("")), m_strScanMsg(_T(""))
  413. {
  414. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  415. }
  416. void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
  417. {
  418. CDialogEx::DoDataExchange(pDX);
  419. DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
  420. DDX_Text(pDX, IDC_EDIT_SCANMSG, m_strScanMsg);
  421. }
  422. BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
  423. ON_WM_SYSCOMMAND()
  424. ON_WM_PAINT()
  425. ON_WM_QUERYDRAGICON()
  426. ON_BN_CLICKED(IDOK, &CMFCApplication1Dlg::OnBnClickedOk)
  427. ON_MESSAGE(WM_SHOWINFO, OnShowInfo)
  428. ON_MESSAGE(WM_SHOWSCANINFO, OnShowScanInfo)
  429. ON_MESSAGE(WM_PROCESS, OnProcessInfo)
  430. ON_BN_CLICKED(IDCANCEL, &CMFCApplication1Dlg::OnBnClickedCancel)
  431. ON_BN_CLICKED(IDC_BTN_TXT, &CMFCApplication1Dlg::OnBnClickedBtnTxt)
  432. ON_BN_CLICKED(IDC_BTN_PDF, &CMFCApplication1Dlg::OnBnClickedBtnPdf)
  433. ON_BN_CLICKED(IDC_BTN_MODEPATH, &CMFCApplication1Dlg::OnBnClickedBtnModepath)
  434. ON_BN_CLICKED(IDC_BTN_IMGPATH, &CMFCApplication1Dlg::OnBnClickedBtnImgpath)
  435. ON_BN_CLICKED(IDC_BTN_CUTPATH, &CMFCApplication1Dlg::OnBnClickedBtnCutpath)
  436. ON_BN_CLICKED(ID_BTN_SCAN, &CMFCApplication1Dlg::OnBnClickedBtnScan)
  437. END_MESSAGE_MAP()
  438. // CMFCApplication1Dlg 消息处理程序
  439. BOOL CMFCApplication1Dlg::OnInitDialog()
  440. {
  441. CDialogEx::OnInitDialog();
  442. // 将“关于...”菜单项添加到系统菜单中。
  443. // IDM_ABOUTBOX 必须在系统命令范围内。
  444. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  445. ASSERT(IDM_ABOUTBOX < 0xF000);
  446. CMenu* pSysMenu = GetSystemMenu(FALSE);
  447. if (pSysMenu != nullptr)
  448. {
  449. BOOL bNameValid;
  450. CString strAboutMenu;
  451. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  452. ASSERT(bNameValid);
  453. if (!strAboutMenu.IsEmpty())
  454. {
  455. pSysMenu->AppendMenu(MF_SEPARATOR);
  456. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  457. }
  458. }
  459. // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
  460. // 执行此操作
  461. SetIcon(m_hIcon, TRUE); // 设置大图标
  462. SetIcon(m_hIcon, FALSE); // 设置小图标
  463. CConfig::Instance()->LoadFromINI();
  464. GetDlgItem(IDC_EDIT_TXT)->SetWindowText(CConfig::Instance()->m_strTxt);
  465. GetDlgItem(IDC_EDIT_PDF)->SetWindowText(CConfig::Instance()->m_strPdf);
  466. GetDlgItem(IDC_EDIT_MODEPATH)->SetWindowText(CConfig::Instance()->m_strMode);
  467. GetDlgItem(IDC_EDIT_IMGPATH)->SetWindowText(CConfig::Instance()->m_strImg);
  468. GetDlgItem(IDC_EDIT_CUTPATH)->SetWindowText(CConfig::Instance()->m_strCut);
  469. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  470. }
  471. void CMFCApplication1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
  472. {
  473. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  474. {
  475. CAboutDlg dlgAbout;
  476. dlgAbout.DoModal();
  477. }
  478. else
  479. {
  480. CDialogEx::OnSysCommand(nID, lParam);
  481. }
  482. }
  483. // 如果向对话框添加最小化按钮,则需要下面的代码
  484. // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
  485. // 这将由框架自动完成。
  486. void CMFCApplication1Dlg::OnPaint()
  487. {
  488. if (IsIconic())
  489. {
  490. CPaintDC dc(this); // 用于绘制的设备上下文
  491. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  492. // 使图标在工作区矩形中居中
  493. int cxIcon = GetSystemMetrics(SM_CXICON);
  494. int cyIcon = GetSystemMetrics(SM_CYICON);
  495. CRect rect;
  496. GetClientRect(&rect);
  497. int x = (rect.Width() - cxIcon + 1) / 2;
  498. int y = (rect.Height() - cyIcon + 1) / 2;
  499. // 绘制图标
  500. dc.DrawIcon(x, y, m_hIcon);
  501. }
  502. else
  503. {
  504. CDialogEx::OnPaint();
  505. }
  506. }
  507. //当用户拖动最小化窗口时系统调用此函数取得光标
  508. //显示。
  509. HCURSOR CMFCApplication1Dlg::OnQueryDragIcon()
  510. {
  511. return static_cast<HCURSOR>(m_hIcon);
  512. }
  513. void CMFCApplication1Dlg::OnBnClickedOk()
  514. {
  515. ((CEdit*)GetDlgItem(IDC_EDIT_TXT))->GetWindowText(m_strTextPath);
  516. ((CEdit*)GetDlgItem(IDC_EDIT_PDF))->GetWindowText(m_strPdfDir);
  517. ((CEdit*)GetDlgItem(IDC_EDIT_PAGE_NUMBER))->GetWindowText(m_strPageNumber);
  518. if (m_strTextPath.GetLength() == 0)
  519. {
  520. AfxMessageBox(L"语料库文件未选择");
  521. return;
  522. }
  523. if (m_strPdfDir.GetLength() == 0)
  524. {
  525. AfxMessageBox(L"PDF文件夹未选择");
  526. return;
  527. }
  528. m_strPageNumber.Trim();
  529. if (m_strPageNumber.GetLength() == 0)
  530. {
  531. AfxMessageBox(L"启始页码未输入");
  532. return;
  533. }
  534. if (m_strPageNumber.SpanIncluding(_T("0123456789")) != m_strPageNumber)
  535. {
  536. AfxMessageBox(L"启始页码不是数字");
  537. return;
  538. }
  539. if (_wtoi(m_strPageNumber.GetBuffer()) > 20000)
  540. {
  541. AfxMessageBox(L"启始页码小于20000");
  542. return;
  543. }
  544. GetDlgItem(IDOK)->EnableWindow(FALSE);
  545. m_strMsg = "";
  546. CString strJsonDir(m_strPdfDir);
  547. strJsonDir.Append(L"\\json");
  548. if (!PathIsDirectory(strJsonDir))
  549. {
  550. ::CreateDirectory(strJsonDir, 0);
  551. }
  552. CString strPdfDir(m_strPdfDir);
  553. strPdfDir.Append(L"\\pdf");
  554. if (!PathIsDirectory(strPdfDir))
  555. {
  556. ::CreateDirectory(strPdfDir, 0);
  557. }
  558. SAFETY_EXIT_THREAD(_threadProcess, 100);
  559. _threadProcess = CreateThread(NULL, 0, ProcessThread, NULL, 0, NULL);
  560. //CDialogEx::OnOK();
  561. }
  562. void CMFCApplication1Dlg::UpdateMsg(CString strMsg)
  563. {
  564. m_strMsg += strMsg;
  565. UpdateData(FALSE);
  566. }
  567. void CMFCApplication1Dlg::UpdateScanMsg(CString strMsg)
  568. {
  569. m_strScanMsg += strMsg;
  570. UpdateData(FALSE);
  571. }
  572. LRESULT CMFCApplication1Dlg::OnShowInfo(WPARAM wParam, LPARAM lParam)
  573. {
  574. wchar_t* p = (wchar_t*)wParam;
  575. CString str;
  576. str.Format(_T("%s"), p);
  577. m_strMsg += str;
  578. UpdateData(FALSE);
  579. if (lParam == 1)
  580. {
  581. GetDlgItem(IDOK)->EnableWindow(TRUE);
  582. }
  583. return 0;
  584. }
  585. LRESULT CMFCApplication1Dlg::OnShowScanInfo(WPARAM wParam, LPARAM lParam)
  586. {
  587. if (lParam == 1)
  588. {
  589. GetDlgItem(ID_BTN_SCAN)->EnableWindow(TRUE);
  590. return 0;
  591. }
  592. wchar_t* p = (wchar_t*)wParam;
  593. CString str;
  594. str.Format(_T("%s"), p);
  595. m_strScanMsg += str;
  596. UpdateData(FALSE);
  597. return 0;
  598. }
  599. LRESULT CMFCApplication1Dlg::OnProcessInfo(WPARAM wParam, LPARAM lParam)
  600. {
  601. CString strProcess = _T("--");
  602. if (g_vecFileList.size())
  603. {
  604. float fProcess = (float)g_scanCount / (float)g_vecFileList.size();
  605. strProcess.Format(L"%0.0lf%%", fProcess*100);
  606. }
  607. SetDlgItemText(IDC_EDIT_SCANPROCESS, strProcess);
  608. return 0;
  609. }
  610. void CMFCApplication1Dlg::OnBnClickedCancel()
  611. {
  612. SAFETY_EXIT_THREAD(_threadProcess, 100);
  613. SAFETY_EXIT_THREAD(_threadScan, 100);
  614. CConfig::Instance()->SaveSetting();
  615. CDialogEx::OnCancel();
  616. }
  617. void CMFCApplication1Dlg::SelectFolder(HWND hwnd, CString& lpszFolder)
  618. {
  619. TCHAR szFolderPath[MAX_PATH];
  620. szFolderPath[0] = 0;
  621. BROWSEINFO sInfo;
  622. ::ZeroMemory(&sInfo, sizeof(BROWSEINFO));
  623. sInfo.pidlRoot = 0;
  624. sInfo.lpszTitle = _T("请选择一个文件夹");
  625. sInfo.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_EDITBOX;
  626. sInfo.lpfn = NULL;
  627. sInfo.hwndOwner = hwnd;
  628. LPITEMIDLIST lpidlBrowse = ::SHBrowseForFolder(&sInfo);
  629. if (lpidlBrowse != NULL) {
  630. if (::SHGetPathFromIDList(lpidlBrowse, szFolderPath)) {
  631. lpszFolder = szFolderPath;
  632. lpszFolder.Replace('\\', '/');
  633. }
  634. ::CoTaskMemFree(lpidlBrowse);
  635. }
  636. }
  637. void CMFCApplication1Dlg::SelectFile(CString& lpszFile)
  638. {
  639. CString OpenFilter = _T("文本文件(*.txt)|*.txt;|");
  640. CFileDialog FileOpen(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, OpenFilter);
  641. if (IDOK == FileOpen.DoModal())
  642. {
  643. lpszFile = FileOpen.GetPathName();
  644. lpszFile.Replace('\\', '/');
  645. }
  646. }
  647. void CMFCApplication1Dlg::OnBnClickedBtnTxt()
  648. {
  649. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_TXT);
  650. if (!pEdit)
  651. {
  652. return;
  653. }
  654. CString strText;
  655. SelectFile(strText);
  656. if (strText.GetLength() == 0)
  657. {
  658. return;
  659. }
  660. pEdit->SetWindowText(strText);
  661. CConfig::Instance()->m_strTxt = strText;
  662. }
  663. void CMFCApplication1Dlg::OnBnClickedBtnPdf()
  664. {
  665. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_PDF);
  666. if (!pEdit)
  667. {
  668. return;
  669. }
  670. CString strText;
  671. SelectFolder(GetSafeHwnd(), strText);
  672. if (strText.GetLength() == 0)
  673. {
  674. return;
  675. }
  676. pEdit->SetWindowText(strText);
  677. CConfig::Instance()->m_strPdf = strText;
  678. }
  679. void CMFCApplication1Dlg::OnBnClickedBtnModepath()
  680. {
  681. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_MODEPATH);
  682. if (!pEdit)
  683. {
  684. return;
  685. }
  686. CString strText;
  687. SelectFolder(GetSafeHwnd(), strText);
  688. if (strText.GetLength() == 0)
  689. {
  690. return;
  691. }
  692. pEdit->SetWindowText(strText);
  693. CConfig::Instance()->m_strMode = strText;
  694. }
  695. void CMFCApplication1Dlg::OnBnClickedBtnImgpath()
  696. {
  697. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_IMGPATH);
  698. if (!pEdit)
  699. {
  700. return;
  701. }
  702. CString strText;
  703. SelectFolder(GetSafeHwnd(), strText);
  704. if (strText.GetLength() == 0)
  705. {
  706. return;
  707. }
  708. pEdit->SetWindowText(strText);
  709. CConfig::Instance()->m_strImg = strText;
  710. }
  711. void CMFCApplication1Dlg::OnBnClickedBtnCutpath()
  712. {
  713. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CUTPATH);
  714. if (!pEdit)
  715. {
  716. return;
  717. }
  718. CString strText;
  719. SelectFolder(GetSafeHwnd(), strText);
  720. if (strText.GetLength() == 0)
  721. {
  722. return;
  723. }
  724. pEdit->SetWindowText(strText);
  725. CConfig::Instance()->m_strCut = strText;
  726. }
  727. void CMFCApplication1Dlg::OnBnClickedBtnScan()
  728. {
  729. ((CEdit*)GetDlgItem(IDC_EDIT_MODEPATH))->GetWindowText(m_strModePath);
  730. ((CEdit*)GetDlgItem(IDC_EDIT_IMGPATH))->GetWindowText(m_strImgPath);
  731. ((CEdit*)GetDlgItem(IDC_EDIT_CUTPATH))->GetWindowText(m_strCutPath);
  732. if (m_strModePath.GetLength() == 0)
  733. {
  734. AfxMessageBox(L"模板文件夹未选择");
  735. return;
  736. }
  737. if (m_strImgPath.GetLength() == 0)
  738. {
  739. AfxMessageBox(L"答题卡文件夹未选择");
  740. return;
  741. }
  742. if (m_strCutPath.GetLength() == 0)
  743. {
  744. AfxMessageBox(L"切割生成文件夹未选择");
  745. return;
  746. }
  747. GetDlgItem(ID_BTN_SCAN)->EnableWindow(FALSE);
  748. m_strScanMsg = "";
  749. CString strNormal(m_strCutPath);
  750. strNormal.Append(L"/normal");
  751. if (!PathIsDirectory(strNormal))
  752. {
  753. ::CreateDirectory(strNormal, 0);
  754. }
  755. CString strAbnormal(m_strCutPath);
  756. strAbnormal.Append(L"/abnormal");
  757. if (!PathIsDirectory(strAbnormal))
  758. {
  759. ::CreateDirectory(strAbnormal, 0);
  760. }
  761. CString strNormalSmall(m_strCutPath);
  762. strNormalSmall.Append(L"/normal_small");
  763. if (!PathIsDirectory(strNormalSmall))
  764. {
  765. ::CreateDirectory(strNormalSmall, 0);
  766. }
  767. CString strAbnormalSmall(m_strCutPath);
  768. strAbnormalSmall.Append(L"/abnormal_small");
  769. if (!PathIsDirectory(strAbnormalSmall))
  770. {
  771. ::CreateDirectory(strAbnormalSmall, 0);
  772. }
  773. SAFETY_EXIT_THREAD(_threadScan, 100);
  774. _threadScan = CreateThread(NULL, 0, ScanThread, NULL, 0, NULL);
  775. }
  776. void CMFCApplication1Dlg::FormatScanMsg(const char *szFmt, ...)
  777. {
  778. if (!szFmt)
  779. return;
  780. va_list ap;
  781. va_start(ap, szFmt);
  782. size_t nLength = _vsnprintf(nullptr, 0, szFmt, ap) + 1;
  783. CBuffers buffer;
  784. char* szLogMsg = buffer.GetBuf(nLength);
  785. _vsnprintf(szLogMsg, nLength, szFmt, ap);
  786. va_end(ap);
  787. SYSTEMTIME st;
  788. GetLocalTime(&st);
  789. char chBuf[24] = { 0 };
  790. sprintf(chBuf, "%02d-%02d-%02d %02d:%02d:%02d ",
  791. st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
  792. CString strMsg;
  793. // 扫描异常
  794. strMsg.Format(L"%s %s\r\n", GB2312ToTstring(chBuf).c_str(), GB2312ToTstring(szLogMsg).c_str());
  795. SendMessage(WM_SHOWSCANINFO, (WPARAM)strMsg.GetBuffer(), 0);
  796. }