CvxText.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. #include "pch.h"
  2. #include "CvxText.h"
  3. #include <json/json.h>
  4. #include "BaseUtility.h"
  5. #include "libpdf.h"
  6. using namespace std;
  7. using namespace cv;
  8. #if defined(GNUC)
  9. #pragma GCC diagnostic push
  10. #pragma GCC diagnostic ignored “-Wdeprecated-declarations”
  11. #elif defined(_MSC_VER)
  12. #pragma warning(disable : 4996)
  13. #endif
  14. void GetStringSize(HDC hDC, const char* str, int* w, int* h)
  15. {
  16. SIZE size;
  17. GetTextExtentPoint32A(hDC, str, strlen(str), &size);
  18. if (w != 0) *w = size.cx;
  19. if (h != 0) *h = size.cy;
  20. }
  21. cv::Size GetTextSize(const char* str, int fontSize, const char* fn, bool italic, bool underline)
  22. {
  23. HDC hDC = CreateCompatibleDC(0);
  24. LOGFONTA lf;
  25. lf.lfHeight = -fontSize;
  26. lf.lfWidth = 0;
  27. lf.lfEscapement = 0;
  28. lf.lfOrientation = 0;
  29. lf.lfWeight = 5;
  30. lf.lfItalic = italic; //斜体
  31. lf.lfUnderline = underline; //下划线
  32. lf.lfStrikeOut = 0;
  33. lf.lfCharSet = DEFAULT_CHARSET;
  34. lf.lfOutPrecision = 0;
  35. lf.lfClipPrecision = 0;
  36. lf.lfQuality = PROOF_QUALITY;
  37. lf.lfPitchAndFamily = 0;
  38. strcpy_s(lf.lfFaceName, fn);
  39. HFONT hf = CreateFontIndirectA(&lf);
  40. HFONT hOldFont = (HFONT)SelectObject(hDC, hf);
  41. int strBaseW = 0, strBaseH = 0;
  42. int singleRow = 0;
  43. char buf[1 << 12];
  44. strcpy_s(buf, str);
  45. char *bufT[1 << 12]; // 这个用于分隔字符串后剩余的字符,可能会超出。
  46. //处理多行
  47. {
  48. int nnh = 0;
  49. int cw, ch;
  50. const char* ln = strtok_s(buf, "\n", bufT);
  51. while (ln != 0)
  52. {
  53. GetStringSize(hDC, ln, &cw, &ch);
  54. strBaseW = max(strBaseW, cw);
  55. strBaseH = max(strBaseH, ch);
  56. ln = strtok_s(0, "\n", bufT);
  57. nnh++;
  58. }
  59. singleRow = strBaseH;
  60. strBaseH *= nnh;
  61. }
  62. SelectObject(hDC, hOldFont);
  63. DeleteObject(hf);
  64. DeleteDC(hDC);
  65. cv::Size size;
  66. size.width = strBaseW;
  67. size.height = strBaseH;
  68. return size;
  69. }
  70. void putTextZH(cv::Mat &dst, cv::Size & rSize, const char* str, Point org, Scalar color, int fontSize, const char* fn, bool italic, bool underline)
  71. {
  72. CV_Assert(dst.data != 0 && (dst.channels() == 1 || dst.channels() == 3));
  73. int x, y, r, b;
  74. if (org.x > dst.cols || org.y > dst.rows) return;
  75. x = org.x < 0 ? -org.x : 0;
  76. y = org.y < 0 ? -org.y : 0;
  77. LOGFONTA lf;
  78. lf.lfHeight = -fontSize;
  79. lf.lfWidth = 0;
  80. lf.lfEscapement = 0;
  81. lf.lfOrientation = 0;
  82. lf.lfWeight = 5;
  83. lf.lfItalic = italic; //斜体
  84. lf.lfUnderline = underline; //下划线
  85. lf.lfStrikeOut = 0;
  86. lf.lfCharSet = DEFAULT_CHARSET;
  87. lf.lfOutPrecision = 0;
  88. lf.lfClipPrecision = 0;
  89. lf.lfQuality = PROOF_QUALITY;
  90. lf.lfPitchAndFamily = 0;
  91. strcpy_s(lf.lfFaceName, fn);
  92. HFONT hf = CreateFontIndirectA(&lf);
  93. HDC hDC = CreateCompatibleDC(0);
  94. HFONT hOldFont = (HFONT)SelectObject(hDC, hf);
  95. int strBaseW = 0, strBaseH = 0;
  96. int singleRow = 0;
  97. char buf[1 << 12];
  98. strcpy_s(buf, str);
  99. char *bufT[1 << 12]; // 这个用于分隔字符串后剩余的字符,可能会超出。
  100. //处理多行
  101. {
  102. int nnh = 0;
  103. int cw, ch;
  104. const char* ln = strtok_s(buf, "\n", bufT);
  105. while (ln != 0)
  106. {
  107. GetStringSize(hDC, ln, &cw, &ch);
  108. strBaseW = max(strBaseW, cw);
  109. strBaseH = max(strBaseH, ch);
  110. ln = strtok_s(0, "\n", bufT);
  111. nnh++;
  112. }
  113. singleRow = strBaseH;
  114. strBaseH *= nnh;
  115. }
  116. rSize.width = strBaseW;
  117. rSize.height = strBaseH;
  118. if (org.x + strBaseW < 0 || org.y + strBaseH < 0)
  119. {
  120. SelectObject(hDC, hOldFont);
  121. DeleteObject(hf);
  122. DeleteObject(hDC);
  123. return;
  124. }
  125. r = org.x + strBaseW > dst.cols ? dst.cols - org.x - 1 : strBaseW - 1;
  126. b = org.y + strBaseH > dst.rows ? dst.rows - org.y - 1 : strBaseH - 1;
  127. org.x = org.x < 0 ? 0 : org.x;
  128. org.y = org.y < 0 ? 0 : org.y;
  129. BITMAPINFO bmp = { 0 };
  130. BITMAPINFOHEADER& bih = bmp.bmiHeader;
  131. int strDrawLineStep = strBaseW * 3 % 4 == 0 ? strBaseW * 3 : (strBaseW * 3 + 4 - ((strBaseW * 3) % 4));
  132. bih.biSize = sizeof(BITMAPINFOHEADER);
  133. bih.biWidth = strBaseW;
  134. bih.biHeight = strBaseH;
  135. bih.biPlanes = 1;
  136. bih.biBitCount = 24;
  137. bih.biCompression = BI_RGB;
  138. bih.biSizeImage = strBaseH * strDrawLineStep;
  139. bih.biClrUsed = 0;
  140. bih.biClrImportant = 0;
  141. void* pDibData = 0;
  142. HBITMAP hBmp = CreateDIBSection(hDC, &bmp, DIB_RGB_COLORS, &pDibData, 0, 0);
  143. CV_Assert(pDibData != 0);
  144. HBITMAP hOldBmp = (HBITMAP)SelectObject(hDC, hBmp);
  145. //color.val[2], color.val[1], color.val[0]
  146. SetTextColor(hDC, RGB(255, 255, 255));
  147. SetBkColor(hDC, 0);
  148. //SetStretchBltMode(hDC, COLORONCOLOR);
  149. strcpy_s(buf, str);
  150. const char* ln = strtok_s(buf, "\n", bufT);
  151. int outTextY = 0;
  152. while (ln != 0)
  153. {
  154. TextOutA(hDC, 0, outTextY, ln, strlen(ln));
  155. outTextY += singleRow;
  156. ln = strtok_s(0, "\n", bufT);
  157. }
  158. uchar* dstData = (uchar*)dst.data;
  159. int dstStep = dst.step / sizeof(dstData[0]);
  160. unsigned char* pImg = (unsigned char*)dst.data + org.x * dst.channels() + org.y * dstStep;
  161. unsigned char* pStr = (unsigned char*)pDibData + x * 3;
  162. for (int tty = y; tty <= b; ++tty)
  163. {
  164. unsigned char* subImg = pImg + (tty - y) * dstStep;
  165. unsigned char* subStr = pStr + (strBaseH - tty - 1) * strDrawLineStep;
  166. for (int ttx = x; ttx <= r; ++ttx)
  167. {
  168. for (int n = 0; n < dst.channels(); ++n) {
  169. double vtxt = subStr[n] / 255.0;
  170. int cvv = vtxt * color.val[n] + (1 - vtxt) * subImg[n];
  171. subImg[n] = cvv > 255 ? 255 : (cvv < 0 ? 0 : cvv);
  172. }
  173. subStr += 3;
  174. subImg += dst.channels();
  175. }
  176. }
  177. SelectObject(hDC, hOldBmp);
  178. SelectObject(hDC, hOldFont);
  179. DeleteObject(hf);
  180. DeleteObject(hBmp);
  181. DeleteDC(hDC);
  182. }
  183. ///////////////////////////// 数据收集卡 ///////////////////////////////////////
  184. // 获取字符串的画布上的宽度
  185. int getLineStrWidth(string str, int fontSize, int ttBoxW, int tiSl, int backPix) {
  186. int iw = str.length()*fontSize*0.5;
  187. iw += ttBoxW;
  188. iw += tiSl;
  189. iw += backPix;
  190. iw += fontSize * 1.5; // 题号的宽度 最多不超过1.5个字体宽度 56.
  191. return iw;
  192. }
  193. // 转码 utf-8 2 ansi
  194. static void UTF82ANSI(LPCSTR lpBuff, int nLen, char* pData, int iMaxLen)
  195. {
  196. ZeroMemory(pData, sizeof(char)*iMaxLen);
  197. int nCont = MultiByteToWideChar(CP_UTF8, 0, lpBuff, nLen, NULL, 0);
  198. WCHAR* szTemp = new WCHAR[nCont + 1];
  199. ZeroMemory(szTemp, sizeof(WCHAR)*(nCont + 1));
  200. MultiByteToWideChar(CP_UTF8, 0, lpBuff, nLen, szTemp, nCont);
  201. //获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
  202. int len = WideCharToMultiByte(CP_ACP, 0, szTemp, nCont, NULL, 0, NULL, NULL);
  203. if (len < iMaxLen) {
  204. WideCharToMultiByte(CP_ACP, 0, szTemp, nCont, pData, len, NULL, NULL);
  205. }
  206. else {
  207. char* buffer = new char[len + 1];
  208. //宽字节编码转换成多字节编码
  209. WideCharToMultiByte(CP_ACP, 0, szTemp, nCont, buffer, len, NULL, NULL);
  210. buffer[len] = '\0';
  211. //删除缓冲区并返回值
  212. strncpy(pData, buffer, iMaxLen - 1);
  213. delete[] buffer;
  214. buffer = NULL;
  215. }
  216. delete[]szTemp;
  217. szTemp = NULL;
  218. }
  219. // 二进制码生成获取
  220. void createBinString(Mat & img, int pageNum, int ptw, int pth, cv::Point ptStart, vector<cv::Rect>& vecBoxPages) {
  221. vector<int> vecBinStr; vecBinStr.resize(12);
  222. for (size_t i = 11; i > 0; i--) {
  223. int ys = pageNum % 2;
  224. vecBinStr[i] = ys;
  225. pageNum = pageNum / 2;
  226. }
  227. vecBoxPages.clear();
  228. int ptPgW = ptw / 2; int ptPgH = pth / 2;
  229. for (int j = 0; j < 3; j++) {
  230. int _startY = ptStart.y + 15 + 50 * j;
  231. for (int i = 0; i < 4; i++) {
  232. int _startX = ptStart.x + 18 + i * 72;
  233. vecBoxPages.push_back(cv::Rect(_startX, _startY, ptPgW, ptPgH));
  234. if (vecBinStr[j * 4 + i])
  235. rectangle(img, vecBoxPages[vecBoxPages.size() - 1], cv::Scalar(0), -1);
  236. else
  237. rectangle(img, vecBoxPages[vecBoxPages.size() - 1], cv::Scalar(200, 200, 200), 2, 8, 0);
  238. }
  239. }
  240. return;
  241. }
  242. void generatePageJson(vector<cv::Rect>& vecPts, vector<cv::Rect>& vecBoxPages)
  243. {
  244. Json::Value imageSize;
  245. imageSize["width"] = 1654;
  246. imageSize["height"] = 2344;
  247. Json::Value location(Json::arrayValue);
  248. for (auto& iter : vecPts)
  249. {
  250. Json::Value item;
  251. item["x"] = iter.x;
  252. item["y"] = iter.y;
  253. item["width"] = iter.width;
  254. item["height"] = iter.height;
  255. location.append(item);
  256. }
  257. Json::Value pagenumber(Json::arrayValue);
  258. int nSubVecSize = 4;// 每个小vector的容量
  259. for (size_t i = 0, j = 1; i < vecBoxPages.size(); i += nSubVecSize)
  260. {
  261. vector<cv::Rect> vecSmall;
  262. auto last = std::min(vecBoxPages.size(), i + nSubVecSize);
  263. vecSmall.insert(vecSmall.begin(), vecBoxPages.begin() + i, vecBoxPages.begin() + last);
  264. Json::Value opt(Json::arrayValue);
  265. std::string choice[] = { "A", "B", "C", "D" };
  266. for (int h = 0; h < vecSmall.size(); h++)
  267. {
  268. Json::Value bin;
  269. bin["x"] = vecSmall[h].x;
  270. bin["y"] = vecSmall[h].y;
  271. bin["width"] = vecSmall[h].width;
  272. bin["height"] = vecSmall[h].height;
  273. bin["optName"] = choice[h];
  274. opt.append(bin);
  275. }
  276. Json::Value item;
  277. item["id"] = j;
  278. item["opt"] = opt;
  279. pagenumber.append(item);
  280. j++;
  281. }
  282. Json::Value itemRoot;
  283. itemRoot["imageSize"] = imageSize;
  284. itemRoot["location"] = location;
  285. itemRoot["pagenumber"] = pagenumber;
  286. Json::FastWriter writer;
  287. std::string strJson = writer.write(itemRoot);
  288. CFile zip;
  289. zip.Open(_T("D:\\page.json"), CFile::modeCreate | CFile::modeWrite);
  290. DWORD wide_string_len = MultiByteToWideChar(CP_ACP, 0, strJson.c_str(), -1, NULL, 0);
  291. WCHAR* wide_string = new WCHAR[wide_string_len];
  292. wide_string_len = MultiByteToWideChar(CP_ACP, 0, strJson.c_str(), -1, wide_string, wide_string_len);
  293. DWORD utf8_string_len = WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, NULL, 0, NULL, NULL);
  294. CHAR* utf8_string = new CHAR[utf8_string_len];
  295. utf8_string_len = WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, utf8_string, utf8_string_len, NULL, NULL);
  296. zip.Write((void*)utf8_string, utf8_string_len - 1);
  297. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("%s"), CString(json_str.c_str()));
  298. delete wide_string;
  299. delete utf8_string;
  300. zip.Close();
  301. }
  302. std::string JsonToString(const Json::Value & root)
  303. {
  304. static Json::Value def = []{
  305. Json::Value def;
  306. Json::StreamWriterBuilder::setDefaults(&def);
  307. def["emitUTF8"] = true;
  308. return def;
  309. }();
  310. std::ostringstream stream;
  311. Json::StreamWriterBuilder stream_builder;
  312. stream_builder.settings_ = def;//Config emitUTF8
  313. std::unique_ptr<Json::StreamWriter> writer(stream_builder.newStreamWriter());
  314. writer->write(root, &stream);
  315. return stream.str();
  316. }
  317. int dataCollectionPaper(int cols, int index, int fontSize, int lineGrayPix, bool engShow, std::vector<string> & vecLines, CString dir, HWND hWnd) {
  318. /* 这里要做一些栏数 和 vecLines的对应值确认 */
  319. if (2 == cols && 14 * 2 == vecLines.size())
  320. ; // ok
  321. if (3 == cols && 14 * 3 == vecLines.size())
  322. ; // ok
  323. if (4 == cols && 14 * 4 == vecLines.size())
  324. ; // ok
  325. // 生成画布图像
  326. cv::Mat img(cv::Size(1654, 2344), CV_8UC3, cv::Scalar(255, 255, 255));
  327. int ptw = 80; int pth = 40;
  328. cv::Size fSize(0, 0);
  329. int leftPos = 100, rightPos = 1450;
  330. int topPos = 90;
  331. int fontW = rightPos - leftPos + ptw;
  332. vector<cv::Rect> vecPts = {
  333. cv::Rect(100,90,ptw,pth),
  334. cv::Rect(580,90,ptw,pth),
  335. cv::Rect(1450,90,ptw,pth),
  336. cv::Rect(100,2176,ptw,pth),
  337. cv::Rect(1450,2176,ptw,pth),
  338. };
  339. // 画定位点
  340. for (size_t i = 0; i < vecPts.size(); i++)
  341. {
  342. rectangle(img, vecPts[i], cv::Scalar(0, 0, 0), -1);
  343. }
  344. // 画示例框
  345. topPos += pth; topPos += 30;
  346. rectangle(img, cv::Rect(leftPos, topPos, fontW, 200), cv::Scalar(0, 0, 0), 2, 8, 0);
  347. // 画页码框
  348. cv::Rect boxPageNumber(leftPos + fontW - 300, topPos, 300, 200);
  349. rectangle(img, boxPageNumber, cv::Scalar(0, 0, 0), 2, 8, 0);
  350. // 生成二进制码流
  351. /* 二进制码流的坐标在下面函数里面实现,需要用书写获取 */
  352. vector<cv::Rect> vecBoxPages;
  353. createBinString(img, index, ptw, pth, cv::Point(boxPageNumber.x, boxPageNumber.y), vecBoxPages);
  354. string strPageNumInfo = "关联ID: " + to_string(index);
  355. putTextZH(img, fSize, strPageNumInfo.c_str(), cv::Point(boxPageNumber.x + 100, boxPageNumber.y + 50 * 2 + 20 + 15 + 15 + 15), Scalar(0), 20, "宋体");
  356. // 生成page.json
  357. //generatePageJson(vecPts, vecBoxPages);
  358. // 画警示信息
  359. string strMesInfo = "1、请在每题下方的横线上书写本题的内容!\
  360. \n\n2、题号不需要书写,标点符号需要原样书写。☆\
  361. \n\n3、如果本题书写有误,进行了涂抹修改等操作,请将题号前矩形框用任意笔进行填涂!☆☆\n";
  362. putTextZH(img, fSize, strMesInfo.c_str(), cv::Point(leftPos + 20, topPos + 20), Scalar(0), 25, "宋体");
  363. int lineTop1 = topPos + 20 + fSize.height + 20;
  364. cv::line(img, cv::Point(leftPos, lineTop1), cv::Point(rightPos + ptw - 300, topPos + 20 + fSize.height + 20), Scalar(0), 2, 8, 0);
  365. // 正确示例
  366. string strEgInfo = "正\n确\n示\n例";
  367. putTextZH(img, fSize, strEgInfo.c_str(), cv::Point(leftPos + 20, topPos + 20 + fSize.height + 20 + 3), Scalar(0), 20, "宋体");
  368. cv::line(img, cv::Point(leftPos + 20 + fSize.width + 10, lineTop1), cv::Point(leftPos + 20 + fSize.width + 10, topPos + 200), Scalar(0, 0, 0), 2, 8, 0);
  369. // 贴图区域
  370. /* 暂不实现 */
  371. topPos += 200;
  372. // 文字区域
  373. static int maxLineNum = 14; // 最大行数14
  374. static int colWidth = fontW / cols; // 单栏的宽度
  375. static int ttBoxWidth = 30; // 填涂框的边长
  376. static int tiSl = 10; // 题号和题干间的距离
  377. static int lineDis = 20; // 两行之间的距离
  378. fSize.width = 0; fSize.height = 0;
  379. Json::Value root(Json::arrayValue);
  380. for (int col = 0; col < cols; col++)
  381. { // 逐栏画题
  382. int colTopPos = topPos;
  383. int colLeftPos = leftPos + col * colWidth;
  384. for (size_t i = 0; i < maxLineNum; i++)
  385. {
  386. colTopPos += lineDis;
  387. int lineLen = ttBoxWidth + tiSl;
  388. // 画填涂框 30*30 大小
  389. cv::Rect rcbox(colLeftPos, colTopPos, ttBoxWidth, ttBoxWidth);
  390. rectangle(img, rcbox, cv::Scalar(0), 1, 8, 0);
  391. int iQNum = i + 1 + col * maxLineNum;
  392. string strStinfo = to_string(iQNum); strStinfo += ".";
  393. cv::Point ptNum(colLeftPos + ttBoxWidth + tiSl, colTopPos);
  394. putTextZH(img, fSize, strStinfo.c_str(), ptNum, Scalar(0, 0, 0), fontSize, "宋体"); // 跟填涂框保持10px距离
  395. lineLen += fSize.width;
  396. cv::Rect rcNum(ptNum.x, ptNum.y, fSize.width, fSize.height);
  397. strStinfo.clear();
  398. size_t vecIndex = i + col * maxLineNum;
  399. string strTgInfo = vecLines[vecIndex];
  400. if (!engShow)
  401. ; /* 移除词性 */
  402. //char szTemp[2048];
  403. //memset(szTemp, 0, sizeof(char)*(2048));
  404. //UTF82ANSI(strTgInfo.c_str(), strTgInfo.length(), szTemp, 2048);
  405. //strTgInfo = szTemp;
  406. cv::Point ptQues(colLeftPos + ttBoxWidth + tiSl + fSize.width, colTopPos);
  407. putTextZH(img, fSize, strTgInfo.c_str(), ptQues, Scalar(0, 0, 0), fontSize, "宋体"); // 跟填涂框保持10px距离
  408. lineLen += fSize.width;
  409. cv::Rect rcQues(ptQues.x, ptQues.y, fSize.width, fSize.height);
  410. cv::Rect rcAns(colLeftPos + 30, colTopPos + fSize.height, colWidth - ttBoxWidth, 1);
  411. colTopPos = colTopPos + fSize.height * 3.5; //两行之间两倍的距离用于书写
  412. line(img, cv::Point(colLeftPos + 30, colTopPos), cv::Point(colLeftPos + colWidth - 1, colTopPos), Scalar(lineGrayPix, lineGrayPix, lineGrayPix), 1, 8, 0);
  413. rcAns.height = colTopPos - rcAns.y + lineDis;
  414. //生成识别信息
  415. Json::Value ttbox;
  416. ttbox["x"] = rcbox.x;
  417. ttbox["y"] = rcbox.y;
  418. ttbox["width"] = rcbox.width;
  419. ttbox["height"] = rcbox.height;
  420. Json::Value queNum;
  421. queNum["x"] = rcNum.x;
  422. queNum["y"] = rcNum.y;
  423. queNum["width"] = rcNum.width;
  424. queNum["height"] = rcNum.height;
  425. Json::Value queInfo;
  426. queInfo["x"] = rcQues.x;
  427. queInfo["y"] = rcQues.y;
  428. queInfo["width"] = rcQues.width;
  429. queInfo["height"] = rcQues.height;
  430. queInfo["info"] = strTgInfo.c_str();
  431. Json::Value ansInfo;
  432. ansInfo["x"] = rcAns.x;
  433. ansInfo["y"] = rcAns.y;
  434. ansInfo["width"] = rcAns.width;
  435. ansInfo["height"] = rcAns.height;
  436. Json::Value item;
  437. item["id"] = iQNum;
  438. item["ttbox"] = ttbox;
  439. item["queNum"] = queNum;
  440. item["queInfo"] = queInfo;
  441. item["ansInfo"] = queInfo;
  442. root.append(item);
  443. // test
  444. /*rectangle(img, rcbox, cv::Scalar(0, 0, 200), 1, 8, 0);
  445. rectangle(img, rcNum, cv::Scalar(0, 200, 0), 1, 8, 0);
  446. rectangle(img, rcQues, cv::Scalar(100, 0, 0), 1, 8, 0);
  447. rectangle(img, rcAns, cv::Scalar(200, 0, 0), 1, 8, 0);*/
  448. }
  449. }
  450. std::string strJson = JsonToString(root);
  451. CString strTemplatePath;
  452. strTemplatePath.Format(L"%s/%d.json", dir, index);
  453. CFile zip;
  454. zip.Open(strTemplatePath, CFile::modeCreate | CFile::modeWrite);
  455. zip.Write((void*)strJson.c_str(), strJson.length() - 1);
  456. zip.Close();
  457. wchar_t tempPath[MAX_PATH];
  458. DWORD dwSize = MAX_PATH;
  459. GetTempPath(dwSize, tempPath);//获取临时文件夹路径
  460. _tcscat(tempPath, L"template-2.png");
  461. std::string strPngPath = TstringToGB2312(tempPath);
  462. cv::imwrite(strPngPath, img);
  463. CString strPdfPath;
  464. strPdfPath.Format(L"%s/%d.pdf", dir, index);
  465. PdfNewFromImage({ strPngPath }, TstringToGB2312(strPdfPath.GetBuffer()));
  466. /*cv::namedWindow("fuck", 1);
  467. cv::imshow("fuck", img);
  468. cv::waitKey(0);*/
  469. return 0;
  470. }