MFCApplication1Dlg.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. #if defined(GNUC)
  17. #pragma GCC diagnostic push
  18. #pragma GCC diagnostic ignored “-Wdeprecated-declarations”
  19. #elif defined(_MSC_VER)
  20. #pragma warning(disable : 4996)
  21. #endif
  22. #include <json/json.h>
  23. #ifdef _DEBUG
  24. #pragma comment(lib, "libpdfd.lib")
  25. #pragma comment(lib, "lib_common_depence_d.lib")
  26. #else
  27. #pragma comment(lib, "libpdf.lib")
  28. #pragma comment(lib, "lib_common_depence.lib")
  29. #endif
  30. #ifdef _DEBUG
  31. #define new DEBUG_NEW
  32. #endif
  33. #define SAFETY_CLOSE_HANDLE( handle ) \
  34. if( NULL != handle ) \
  35. { \
  36. CloseHandle( handle ); \
  37. handle = NULL; \
  38. }
  39. #define SAFETY_EXIT_THREAD( hThread, Timeout ) \
  40. if( NULL != hThread ) \
  41. { \
  42. if( WaitForSingleObject( hThread, Timeout ) == WAIT_TIMEOUT ) \
  43. TerminateThread( hThread, -1 ); \
  44. CloseHandle( hThread ); \
  45. hThread = NULL; \
  46. }
  47. // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
  48. class CAboutDlg : public CDialogEx
  49. {
  50. public:
  51. CAboutDlg();
  52. // 对话框数据
  53. #ifdef AFX_DESIGN_TIME
  54. enum { IDD = IDD_ABOUTBOX };
  55. #endif
  56. protected:
  57. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
  58. // 实现
  59. protected:
  60. DECLARE_MESSAGE_MAP()
  61. };
  62. CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
  63. {
  64. }
  65. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  66. {
  67. CDialogEx::DoDataExchange(pDX);
  68. }
  69. BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
  70. END_MESSAGE_MAP()
  71. // CMFCApplication1Dlg 对话框
  72. HANDLE _threadProcess = NULL;
  73. DWORD WINAPI ProcessThread(void *param)
  74. {
  75. CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
  76. std::vector<std::string> twoList;
  77. std::vector<std::string> threeList;
  78. std::vector<std::string> fourList;
  79. AssignWordsFromTest(UnicodeToGB2312(pWnd->m_strTextPath), twoList, threeList, fourList);
  80. auto CovertPdfFunc = [&](std::vector<std::string>& vctFiles, int col, int& pageNum){
  81. std::string strPage1;
  82. for (size_t i = 0; i < vctFiles.size(); i += max_line * col)
  83. {
  84. std::vector<std::string> vecSmall;
  85. auto last = std::min(vctFiles.size(), i + max_line * col);
  86. vecSmall.insert(vecSmall.begin(), vctFiles.begin() + i, vctFiles.begin() + last);
  87. std::string strPagePath;
  88. dataCollectionPaper(col, pageNum, 30, 100, false, vecSmall, pWnd->m_strPdfDir, strPagePath);
  89. if (strPage1.length() == 0)
  90. {
  91. strPage1 = strPagePath;
  92. }
  93. else
  94. {
  95. CString strPdfPath;
  96. strPdfPath.Format(L"%s/pdf/%d.pdf", pWnd->m_strPdfDir, pageNum);
  97. PdfNewFromImage({ strPage1, strPagePath }, TstringToGB2312(strPdfPath.GetBuffer()));
  98. strPage1 = "";
  99. CString strMsg = L"";
  100. strMsg.Format(L"%s\r\n", strPdfPath);
  101. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strMsg.GetBuffer(), 0);
  102. strMsg.ReleaseBuffer();
  103. }
  104. pageNum++;
  105. }
  106. if (strPage1.length() > 0)
  107. {
  108. CString strPdfPath;
  109. strPdfPath.Format(L"%s/pdf/%d.pdf", pWnd->m_strPdfDir, pageNum);
  110. PdfNewFromImage({ strPage1 }, TstringToGB2312(strPdfPath.GetBuffer()));
  111. CString strMsg = L"";
  112. strMsg.Format(L"%s\r\n", strPdfPath);
  113. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strMsg.GetBuffer(), 0);
  114. strMsg.ReleaseBuffer();
  115. }
  116. };
  117. int pageNum = 100;
  118. CovertPdfFunc(twoList, 2, pageNum);
  119. CovertPdfFunc(threeList, 3, pageNum);
  120. CovertPdfFunc(fourList, 4, pageNum);
  121. //通知发送消息到窗口
  122. CString strErrorMsg = L"";
  123. strErrorMsg.Format(L"PDF生成完成!\r\n");
  124. SendMessage(pWnd->m_hWnd, WM_SHOWINFO, (WPARAM)strErrorMsg.GetBuffer(), 1);
  125. strErrorMsg.ReleaseBuffer();
  126. return 0L;
  127. }
  128. int IdentifyCallback(result::spinfo& pinfo, void* param)
  129. {
  130. return 0;
  131. }
  132. HANDLE _threadScan = NULL;
  133. DWORD WINAPI ScanThread(void *param)
  134. {
  135. CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
  136. std::vector<std::string> imgPath; //图片本地路径(和上面url地址一一对应)
  137. preinfo::templatesInfo temeplatInfo;
  138. preinfo::SubjChiInfo sujChiInfo;
  139. std::string strTempDir = "./scantemp/";
  140. if (_access(strTempDir.c_str(), 0) == -1)
  141. {
  142. _mkdir(strTempDir.c_str());
  143. }
  144. int nRet = api_processing_images("",
  145. strTempDir.c_str(),
  146. temeplatInfo,
  147. sujChiInfo,
  148. imgPath,
  149. LOG_AUTO,
  150. false,
  151. OLNS,
  152. IdentifyCallback,
  153. NULL);
  154. ////启动算法扫描功能
  155. if (nRet != 0)
  156. {
  157. // 扫描异常
  158. CString strErrorMsg = L"";
  159. strErrorMsg.Format(L"扫描失败,errorCode:%d!\r\n", nRet);
  160. SendMessage(pWnd->m_hWnd, WM_SHOWSCANINFO, (WPARAM)strErrorMsg.GetBuffer(), 1);
  161. }
  162. //通知发送消息到窗口
  163. CString strErrorMsg = L"";
  164. strErrorMsg.Format(L"扫描完成!\r\n");
  165. SendMessage(pWnd->m_hWnd, WM_SHOWSCANINFO, (WPARAM)strErrorMsg.GetBuffer(), 1);
  166. strErrorMsg.ReleaseBuffer();
  167. return 0L;
  168. }
  169. CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=nullptr*/)
  170. : CDialogEx(IDD_MFCAPPLICATION1_DIALOG, pParent)
  171. , m_strMsg(_T("")), m_strScanMsg(_T(""))
  172. {
  173. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  174. }
  175. void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
  176. {
  177. CDialogEx::DoDataExchange(pDX);
  178. DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
  179. DDX_Text(pDX, IDC_EDIT_SCANMSG, m_strScanMsg);
  180. }
  181. BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
  182. ON_WM_SYSCOMMAND()
  183. ON_WM_PAINT()
  184. ON_WM_QUERYDRAGICON()
  185. ON_BN_CLICKED(IDOK, &CMFCApplication1Dlg::OnBnClickedOk)
  186. ON_MESSAGE(WM_SHOWINFO, OnShowInfo)
  187. ON_MESSAGE(WM_SHOWSCANINFO, OnShowScanInfo)
  188. ON_BN_CLICKED(IDCANCEL, &CMFCApplication1Dlg::OnBnClickedCancel)
  189. ON_BN_CLICKED(IDC_BTN_TXT, &CMFCApplication1Dlg::OnBnClickedBtnTxt)
  190. ON_BN_CLICKED(IDC_BTN_PDF, &CMFCApplication1Dlg::OnBnClickedBtnPdf)
  191. ON_BN_CLICKED(IDC_BTN_MODEPATH, &CMFCApplication1Dlg::OnBnClickedBtnModepath)
  192. ON_BN_CLICKED(IDC_BTN_IMGPATH, &CMFCApplication1Dlg::OnBnClickedBtnImgpath)
  193. ON_BN_CLICKED(IDC_BTN_CUTPATH, &CMFCApplication1Dlg::OnBnClickedBtnCutpath)
  194. ON_BN_CLICKED(ID_BTN_SCAN, &CMFCApplication1Dlg::OnBnClickedBtnScan)
  195. END_MESSAGE_MAP()
  196. // CMFCApplication1Dlg 消息处理程序
  197. BOOL CMFCApplication1Dlg::OnInitDialog()
  198. {
  199. CDialogEx::OnInitDialog();
  200. // 将“关于...”菜单项添加到系统菜单中。
  201. // IDM_ABOUTBOX 必须在系统命令范围内。
  202. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  203. ASSERT(IDM_ABOUTBOX < 0xF000);
  204. CMenu* pSysMenu = GetSystemMenu(FALSE);
  205. if (pSysMenu != nullptr)
  206. {
  207. BOOL bNameValid;
  208. CString strAboutMenu;
  209. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  210. ASSERT(bNameValid);
  211. if (!strAboutMenu.IsEmpty())
  212. {
  213. pSysMenu->AppendMenu(MF_SEPARATOR);
  214. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  215. }
  216. }
  217. // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
  218. // 执行此操作
  219. SetIcon(m_hIcon, TRUE); // 设置大图标
  220. SetIcon(m_hIcon, FALSE); // 设置小图标
  221. // TODO: 在此添加额外的初始化代码
  222. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  223. }
  224. void CMFCApplication1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
  225. {
  226. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  227. {
  228. CAboutDlg dlgAbout;
  229. dlgAbout.DoModal();
  230. }
  231. else
  232. {
  233. CDialogEx::OnSysCommand(nID, lParam);
  234. }
  235. }
  236. // 如果向对话框添加最小化按钮,则需要下面的代码
  237. // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
  238. // 这将由框架自动完成。
  239. void CMFCApplication1Dlg::OnPaint()
  240. {
  241. if (IsIconic())
  242. {
  243. CPaintDC dc(this); // 用于绘制的设备上下文
  244. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  245. // 使图标在工作区矩形中居中
  246. int cxIcon = GetSystemMetrics(SM_CXICON);
  247. int cyIcon = GetSystemMetrics(SM_CYICON);
  248. CRect rect;
  249. GetClientRect(&rect);
  250. int x = (rect.Width() - cxIcon + 1) / 2;
  251. int y = (rect.Height() - cyIcon + 1) / 2;
  252. // 绘制图标
  253. dc.DrawIcon(x, y, m_hIcon);
  254. }
  255. else
  256. {
  257. CDialogEx::OnPaint();
  258. }
  259. }
  260. //当用户拖动最小化窗口时系统调用此函数取得光标
  261. //显示。
  262. HCURSOR CMFCApplication1Dlg::OnQueryDragIcon()
  263. {
  264. return static_cast<HCURSOR>(m_hIcon);
  265. }
  266. void CMFCApplication1Dlg::OnBnClickedOk()
  267. {
  268. ((CEdit*)GetDlgItem(IDC_EDIT_TXT))->GetWindowText(m_strTextPath);
  269. ((CEdit*)GetDlgItem(IDC_EDIT_PDF))->GetWindowText(m_strPdfDir);
  270. if (m_strTextPath.GetLength() == 0)
  271. {
  272. AfxMessageBox(L"语料库文件未选择");
  273. return;
  274. }
  275. if (m_strPdfDir.GetLength() == 0)
  276. {
  277. AfxMessageBox(L"PDF文件夹未选择");
  278. return;
  279. }
  280. GetDlgItem(IDOK)->EnableWindow(FALSE);
  281. m_strMsg = "";
  282. CString strJsonDir(m_strPdfDir);
  283. strJsonDir.Append(L"\\json");
  284. if (!PathIsDirectory(strJsonDir))
  285. {
  286. ::CreateDirectory(strJsonDir, 0);
  287. }
  288. CString strPdfDir(m_strPdfDir);
  289. strPdfDir.Append(L"\\pdf");
  290. if (!PathIsDirectory(strPdfDir))
  291. {
  292. ::CreateDirectory(strPdfDir, 0);
  293. }
  294. SAFETY_EXIT_THREAD(_threadProcess, 100);
  295. _threadProcess = CreateThread(NULL, 0, ProcessThread, NULL, 0, NULL);
  296. //CDialogEx::OnOK();
  297. }
  298. void CMFCApplication1Dlg::UpdateMsg(CString strMsg)
  299. {
  300. m_strMsg += strMsg;
  301. UpdateData(FALSE);
  302. }
  303. void CMFCApplication1Dlg::UpdateScanMsg(CString strMsg)
  304. {
  305. m_strScanMsg += strMsg;
  306. UpdateData(FALSE);
  307. }
  308. LRESULT CMFCApplication1Dlg::OnShowInfo(WPARAM wParam, LPARAM lParam)
  309. {
  310. wchar_t* p = (wchar_t*)wParam;
  311. CString str;
  312. str.Format(_T("%s"), p);
  313. m_strMsg += str;
  314. UpdateData(FALSE);
  315. if (lParam == 1)
  316. {
  317. GetDlgItem(IDOK)->EnableWindow(TRUE);
  318. }
  319. return 0;
  320. }
  321. LRESULT CMFCApplication1Dlg::OnShowScanInfo(WPARAM wParam, LPARAM lParam)
  322. {
  323. wchar_t* p = (wchar_t*)wParam;
  324. CString str;
  325. str.Format(_T("%s"), p);
  326. m_strScanMsg += str;
  327. UpdateData(FALSE);
  328. if (lParam == 1)
  329. {
  330. GetDlgItem(ID_BTN_SCAN)->EnableWindow(TRUE);
  331. }
  332. return 0;
  333. }
  334. void CMFCApplication1Dlg::OnBnClickedCancel()
  335. {
  336. SAFETY_EXIT_THREAD(_threadProcess, 100);
  337. SAFETY_EXIT_THREAD(_threadScan, 100);
  338. CDialogEx::OnCancel();
  339. }
  340. void CMFCApplication1Dlg::SelectFolder(HWND hwnd, CString& lpszFolder)
  341. {
  342. TCHAR szFolderPath[MAX_PATH];
  343. szFolderPath[0] = 0;
  344. BROWSEINFO sInfo;
  345. ::ZeroMemory(&sInfo, sizeof(BROWSEINFO));
  346. sInfo.pidlRoot = 0;
  347. sInfo.lpszTitle = _T("请选择一个文件夹");
  348. sInfo.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_EDITBOX;
  349. sInfo.lpfn = NULL;
  350. sInfo.hwndOwner = hwnd;
  351. LPITEMIDLIST lpidlBrowse = ::SHBrowseForFolder(&sInfo);
  352. if (lpidlBrowse != NULL) {
  353. if (::SHGetPathFromIDList(lpidlBrowse, szFolderPath)) {
  354. lpszFolder = szFolderPath;
  355. lpszFolder.Replace('\\', '/');
  356. }
  357. ::CoTaskMemFree(lpidlBrowse);
  358. }
  359. }
  360. void CMFCApplication1Dlg::SelectFile(CString& lpszFile)
  361. {
  362. CString OpenFilter = _T("文本文件(*.txt)|*.txt;|");
  363. CFileDialog FileOpen(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, OpenFilter);
  364. if (IDOK == FileOpen.DoModal())
  365. {
  366. lpszFile = FileOpen.GetPathName();
  367. }
  368. }
  369. void CMFCApplication1Dlg::OnBnClickedBtnTxt()
  370. {
  371. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_TXT);
  372. if (!pEdit)
  373. {
  374. return;
  375. }
  376. CString strText;
  377. SelectFile(strText);
  378. if (strText.GetLength() == 0)
  379. {
  380. return;
  381. }
  382. pEdit->SetWindowText(strText);
  383. }
  384. void CMFCApplication1Dlg::OnBnClickedBtnPdf()
  385. {
  386. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_PDF);
  387. if (!pEdit)
  388. {
  389. return;
  390. }
  391. CString strText;
  392. SelectFolder(GetSafeHwnd(), strText);
  393. if (strText.GetLength() == 0)
  394. {
  395. return;
  396. }
  397. pEdit->SetWindowText(strText);
  398. }
  399. void CMFCApplication1Dlg::OnBnClickedBtnModepath()
  400. {
  401. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_MODEPATH);
  402. if (!pEdit)
  403. {
  404. return;
  405. }
  406. CString strText;
  407. SelectFolder(GetSafeHwnd(), strText);
  408. if (strText.GetLength() == 0)
  409. {
  410. return;
  411. }
  412. pEdit->SetWindowText(strText);
  413. }
  414. void CMFCApplication1Dlg::OnBnClickedBtnImgpath()
  415. {
  416. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_IMGPATH);
  417. if (!pEdit)
  418. {
  419. return;
  420. }
  421. CString strText;
  422. SelectFolder(GetSafeHwnd(), strText);
  423. if (strText.GetLength() == 0)
  424. {
  425. return;
  426. }
  427. pEdit->SetWindowText(strText);
  428. }
  429. void CMFCApplication1Dlg::OnBnClickedBtnCutpath()
  430. {
  431. CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CUTPATH);
  432. if (!pEdit)
  433. {
  434. return;
  435. }
  436. CString strText;
  437. SelectFolder(GetSafeHwnd(), strText);
  438. if (strText.GetLength() == 0)
  439. {
  440. return;
  441. }
  442. pEdit->SetWindowText(strText);
  443. }
  444. void CMFCApplication1Dlg::OnBnClickedBtnScan()
  445. {
  446. ((CEdit*)GetDlgItem(IDC_EDIT_MODEPATH))->GetWindowText(m_strModePath);
  447. ((CEdit*)GetDlgItem(IDC_EDIT_IMGPATH))->GetWindowText(m_strImgPath);
  448. ((CEdit*)GetDlgItem(IDC_EDIT_CUTPATH))->GetWindowText(m_strCutPath);
  449. if (m_strModePath.GetLength() == 0)
  450. {
  451. AfxMessageBox(L"模板文件夹未选择");
  452. return;
  453. }
  454. if (m_strImgPath.GetLength() == 0)
  455. {
  456. AfxMessageBox(L"答题卡文件夹未选择");
  457. return;
  458. }
  459. if (m_strCutPath.GetLength() == 0)
  460. {
  461. AfxMessageBox(L"切割生成文件夹未选择");
  462. return;
  463. }
  464. GetDlgItem(ID_BTN_SCAN)->EnableWindow(FALSE);
  465. m_strScanMsg = "";
  466. SAFETY_EXIT_THREAD(_threadScan, 100);
  467. _threadScan = CreateThread(NULL, 0, ScanThread, NULL, 0, NULL);
  468. }