fork(5) download
  1. // FingerAnalyserDlg.cpp : implementation file
  2. //#pragma once
  3.  
  4. #include "stdafx.h"
  5. #include "FingerAnalyser.h"
  6. #include "FingerAnalyserDlg.h"
  7. #include "math.h"
  8.  
  9. #include <list>
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #endif
  14.  
  15. // CFingerAnalyserDlg dialog
  16.  
  17. CFingerAnalyserDlg::CFingerAnalyserDlg(CWnd* pParent /*=NULL*/)
  18. : CDialog(CFingerAnalyserDlg::IDD, pParent)
  19. {
  20. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  21.  
  22. // Инициализация переменных
  23. m_WorkFile = _T(""); // рабочий файл
  24. m_ShowBase = FALSE; // Просмотр выключен
  25. mouseX = 0; // Координаты точки
  26. mouseY = 0;
  27. m_DotsCount = 0; // Количество точек
  28. m_ScanTime = 0; // Время анализа
  29. picture = NULL;
  30. }
  31. // Деструктор
  32. CFingerAnalyserDlg::~CFingerAnalyserDlg()
  33. {
  34. delete(fp);
  35. delete(picture);
  36.  
  37. if(compareResult)
  38. {
  39. for(list<CCompareFing>::iterator i = compareResult->begin();i != compareResult->end();i++)
  40. {
  41. list<CPairSur>::iterator j;
  42.  
  43. for(j=i->surdots.begin(); j!=i->surdots.end(); j++)
  44. {
  45. j->first->clear();
  46. delete(j->first);
  47. j->second->clear();
  48. delete(j->second);
  49. }
  50. }
  51.  
  52. compareResult->clear();
  53. delete(compareResult);
  54. compareResult = NULL;
  55. }
  56. }
  57.  
  58. void CFingerAnalyserDlg::DoDataExchange(CDataExchange* pDX)
  59. {
  60. CDialog::DoDataExchange(pDX);
  61. DDX_Check(pDX, IDC_SHOW, m_ShowBase);
  62. DDX_Control(pDX, IDC_PROGRESS_COMPARE, m_ProgressCompare);
  63. DDX_Control(pDX, IDC_PROGRESS_SAVE, m_ProgressSave);
  64. DDX_Text(pDX, IDC_POINTS, m_DotsCount);
  65. DDX_Text(pDX, IDC_TIME, m_ScanTime);
  66. DDX_Text(pDX, IDC_FILE, m_WorkFile);
  67. }
  68.  
  69. BEGIN_MESSAGE_MAP(CFingerAnalyserDlg, CDialog)
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. //}}AFX_MSG_MAP
  73. ON_BN_CLICKED(IDC_OPEN, &CFingerAnalyserDlg::OnBnClickedOpen)
  74. ON_BN_CLICKED(IDC_ANALYSE, &CFingerAnalyserDlg::OnBnClickedAnalyse)
  75. ON_BN_CLICKED(IDC_COMPARE, &CFingerAnalyserDlg::OnBnClickedCompare)
  76. ON_BN_CLICKED(IDC_SAVE, &CFingerAnalyserDlg::OnBnClickedSave)
  77. ON_BN_CLICKED(IDC_BACK, &CFingerAnalyserDlg::OnBnClickedBack)
  78. ON_BN_CLICKED(IDC_NEXT, &CFingerAnalyserDlg::OnBnClickedNext)
  79. ON_BN_CLICKED(IDC_SHOW,&CFingerAnalyserDlg::OnBnClickedShowBase)
  80. ON_WM_LBUTTONDOWN()
  81. ON_WM_TIMER()
  82. END_MESSAGE_MAP()
  83.  
  84.  
  85. // CFingerAnalyserDlg message handlers
  86.  
  87. BOOL CFingerAnalyserDlg::OnInitDialog()
  88. {
  89. CDialog::OnInitDialog();
  90.  
  91. // Set the icon for this dialog. The framework does this automatically
  92. // when the application's main window is not a dialog
  93. SetIcon(m_hIcon, TRUE); // Set big icon
  94. SetIcon(m_hIcon, FALSE); // Set small icon
  95.  
  96. fp = new CFingPicture(this->GetDC());
  97.  
  98. char fullpath[200];
  99. _fullpath(fullpath, NULL, 200);
  100. pathToSAV = fullpath;
  101. pathToSAV += "\\sav\\";
  102. dbFile = pathToSAV + "fingbase.bse";
  103. compareResult = NULL;
  104.  
  105. return TRUE; // return TRUE unless you set the focus to a control
  106. }
  107.  
  108. // If you add a minimize button to your dialog, you will need the code below
  109. // to draw the icon. For MFC applications using the document/view model,
  110. // this is automatically done for you by the framework.
  111.  
  112. void CFingerAnalyserDlg::OnPaint()
  113. {
  114. if (IsIconic())
  115. {
  116. CPaintDC dc(this); // device context for painting
  117.  
  118. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  119.  
  120. // Center icon in client rectangle
  121. int cxIcon = GetSystemMetrics(SM_CXICON);
  122. int cyIcon = GetSystemMetrics(SM_CYICON);
  123. CRect rect;
  124. GetClientRect(&rect);
  125. int x = (rect.Width() - cxIcon + 1) / 2;
  126. int y = (rect.Height() - cyIcon + 1) / 2;
  127.  
  128. // Draw the icon
  129. dc.DrawIcon(x, y, m_hIcon);
  130. }
  131. else
  132. {
  133. CPaintDC dc(this);
  134. if(m_ShowBase)
  135. {
  136. ShowBase(true,false);
  137. }
  138. else
  139. {//режим просмотра открытого образа
  140. if (picture != NULL)
  141. {
  142. picture->GetPic1()->Show(140, 70);
  143. picture->GetPic2()->Show(545, 70);
  144. }
  145.  
  146. m_DotsCount = (int)fingA.size();
  147. UpdateData(false);
  148. }
  149.  
  150. CDialog::OnPaint();
  151. }
  152. }
  153.  
  154. // The system calls this function to obtain the cursor to display while the user drags
  155. // the minimized window.
  156. HCURSOR CFingerAnalyserDlg::OnQueryDragIcon()
  157. {
  158. return static_cast<HCURSOR>(m_hIcon);
  159. }
  160.  
  161. // Открытие файла
  162. void CFingerAnalyserDlg::OnBnClickedOpen()
  163. {
  164. char szFilters[]= "Образы (*.bmp)|*.bmp|All Files (*.*)|*.*||";
  165. CFileDialog dlg(TRUE, "bmp", "*.bmp", OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
  166.  
  167. if(dlg.DoModal() != IDOK)
  168. return; //никаких файлов не открыли
  169.  
  170. if(dlg.GetFileExt().CompareNoCase("bmp"))
  171. return; //открытый файл не имеет расширеня .bmp
  172.  
  173. CString fileName = dlg.GetFileName();
  174.  
  175. if(picture != NULL)
  176. delete(picture);
  177.  
  178. picture = new CAnalysePicture(fileName, this->GetDC());
  179. m_WorkFile = fileName;
  180.  
  181. if(compareResult)
  182. {
  183. for(list<CCompareFing>::iterator i = compareResult->begin(); i != compareResult->end(); i++)
  184. {
  185. list<CPairSur>::iterator j;
  186. for(j=i->surdots.begin(); j!=i->surdots.end(); j++)
  187. {
  188. j->first->clear();
  189. delete(j->first);
  190. j->second->clear();
  191. delete(j->second);
  192. }
  193. }
  194.  
  195. compareResult->clear();
  196. }
  197.  
  198. m_ShowBase = false;
  199. Invalidate();
  200. }
  201.  
  202. // Анализ изображения
  203. void CFingerAnalyserDlg::OnBnClickedAnalyse()
  204. {
  205. if(picture == NULL)
  206. return;
  207.  
  208. LPSYSTEMTIME mTime;
  209. mTime = new SYSTEMTIME;
  210. GetSystemTime(mTime);
  211. long int workTime;
  212. workTime = mTime->wSecond*1000+mTime->wMilliseconds;
  213.  
  214. fingA = picture->AnalysePicture();
  215. fingA.SaveFing(GetSAV(picture->getPathSrc()));
  216.  
  217. GetSystemTime(mTime);
  218.  
  219. workTime = mTime->wSecond*1000+mTime->wMilliseconds - workTime;
  220. workTime = (workTime<0)?60000+workTime:workTime;
  221. delete(mTime);
  222. m_ScanTime = workTime;
  223. Invalidate();
  224. }
  225. // Сравнение
  226. void CFingerAnalyserDlg::OnBnClickedCompare()
  227. {
  228. if(fingA.size() == 0)
  229. {
  230. MessageBox("Отпечаток не обработан", "Ошибка");
  231. return;
  232. }
  233.  
  234. fingR.Convert(fingA);
  235.  
  236. if(compareResult)
  237. {
  238. for(list<CCompareFing>::iterator i = compareResult->begin(); i != compareResult->end(); i++)
  239. {
  240. list<CPairSur>::iterator j;
  241.  
  242. for(j=i->surdots.begin(); j!=i->surdots.end(); j++)
  243. {
  244. j->first->clear(); delete(j->first);
  245. j->second->clear(); delete(j->second);
  246. }
  247. }
  248.  
  249. compareResult->clear();
  250. delete(compareResult);
  251. compareResult = NULL;
  252. showIter = NULL;
  253. }
  254.  
  255. compareResult = CompareWithBase();
  256.  
  257. if(compareResult->size() == 0)
  258. return;
  259.  
  260. CString sOut="";
  261.  
  262. for(list<CCompareFing>::iterator i = compareResult->begin(); i != compareResult->end(); i++)
  263. {
  264. CString s="";
  265. int mlevel = min(i->nfng,m_DotsCount);
  266. int percent;
  267.  
  268. if(mlevel > 10)
  269. mlevel = 10;
  270.  
  271. if(i->cDot > mlevel)
  272. percent = 100;
  273. else
  274. percent = (int)(100.0*i->cDot/(double)mlevel + 0.5);
  275.  
  276. if(percent == 0)
  277. continue;
  278.  
  279. s.Format("%d %d %% %s\n", i->cDot, percent, i->name);
  280. sOut += s;
  281. }
  282.  
  283. if(sOut.GetLength()==0)
  284. sOut = "Ни одного отпечатка не найдено!\n";
  285.  
  286. CString kol;
  287. kol.Format("\n Всего в базе: %d", compareResult->size());
  288. sOut += kol;
  289.  
  290. PrintReport(picture->getPathSrc(), sOut);
  291. MessageBox(sOut, picture->getPathSrc());
  292. }
  293.  
  294. // Сохранить в БД
  295. void CFingerAnalyserDlg::OnBnClickedSave()
  296. {
  297. char szFilters[] = "Образы (*.bmp)|*.bmp|All Files (*.*)|*.*||";
  298. CFileDialog dlg( TRUE, "bmp", "*.bmp", OFN_FILEMUSTEXIST| OFN_HIDEREADONLY| OFN_ALLOWMULTISELECT, szFilters, this);
  299.  
  300. if(dlg.DoModal() == IDOK)
  301. {
  302. listCInfo *fingDB = LoadDB(dbFile);
  303. FILE *fbse = fopen(dbFile, "wb");
  304.  
  305. if(fbse == NULL)
  306. {
  307. MessageBox("Невозможно создать базу данных с отпечатками", "Ошибка создания БД", MB_OK);
  308. return;
  309. }
  310.  
  311. POSITION pos, posStart;
  312. CInfo newFingInDB;
  313. pos = posStart = dlg.GetStartPosition();
  314. int kolFile = 0;
  315.  
  316. while(pos)
  317. {
  318. dlg.GetNextPathName(pos);
  319. kolFile++;
  320. }
  321.  
  322. pos = posStart;
  323. m_ProgressSave.SetRange(0, kolFile);
  324. int progressPos = 1;
  325.  
  326. while(pos)
  327. {
  328. CString fileName = dlg.GetNextPathName(pos).MakeLower();
  329.  
  330. if(fileName.Find(".bmp") == -1)
  331. continue;
  332.  
  333. CAnalysePicture *loadingPic;
  334. loadingPic = new CAnalysePicture(fileName, this->GetDC());
  335. m_WorkFile = fileName;
  336. fingA = loadingPic->AnalysePicture();
  337.  
  338. if(fingA.size() < MIN_SIZE)
  339. {
  340. MessageBox("Отпечаток не пригоден для сохраниения в базу!", fileName);
  341. continue;
  342. }
  343.  
  344. if(fingA.size() > MAX_SIZE)
  345. {
  346. MessageBox("Отпечаток не пригоден для сохраниения в базу!", fileName);
  347. continue;
  348. }
  349.  
  350. fingA.SaveFing(GetSAV(fileName));
  351. newFingInDB.src = fileName;
  352. fingDB->remove(newFingInDB);
  353. fingDB->push_back(newFingInDB);
  354. m_ProgressSave.SetPos(progressPos);
  355. progressPos++;
  356. Invalidate();
  357. delete(loadingPic);
  358. }
  359.  
  360. m_ProgressSave.SetPos(0);
  361. int count = 0;
  362. fwrite((void*)&count, sizeof(count), 1, fbse);
  363.  
  364. for(list<CInfo>::iterator iter = fingDB->begin(); iter != fingDB->end(); iter++)
  365. {
  366. iter->Printf(fbse);
  367. count++;
  368. }
  369.  
  370. fseek(fbse, 0, SEEK_SET);
  371. fwrite((void*)&count, sizeof(count), 1, fbse);
  372. fingDB->clear();
  373. delete(fingDB);
  374. fclose(fbse);
  375. }
  376.  
  377. }
  378.  
  379. void CFingerAnalyserDlg::OnBnClickedBack()
  380. {
  381. ShowBase(false);
  382. }
  383.  
  384. void CFingerAnalyserDlg::OnBnClickedNext()
  385. {
  386. ShowBase(true);
  387. }
  388.  
  389. // Нажатие кнопки мыши над формой
  390. void CFingerAnalyserDlg::OnLButtonDown(UINT nFlags, CPoint point)
  391. {
  392. if(!m_ShowBase)
  393. return;
  394.  
  395. mousePos = point;
  396. mousePos.x -= 110;
  397. mousePos.y -= 45;
  398. ShowBase(true, false);
  399.  
  400. CDialog::OnLButtonDown(nFlags, point);
  401. }
  402.  
  403. // обновлять при событии от таймера
  404. void CFingerAnalyserDlg::OnTimer(UINT_PTR nIDEvent)
  405. {
  406. Invalidate();
  407. CDialog::OnTimer(nIDEvent);
  408. }
  409.  
  410.  
  411. void CFingerAnalyserDlg::OnBnClickedShowBase()
  412. {
  413. m_ShowBase =! m_ShowBase;
  414. UpdateData(false);
  415.  
  416. if(m_ShowBase)
  417. {
  418. ShowBase(true, false);
  419. }
  420. else
  421. {
  422. OnPaint();
  423. }
  424. }
  425.  
  426. //key - направление перемотки по базе (влево, вправо)
  427. //next - нужно ли переходить к следующему отпечатку
  428. void CFingerAnalyserDlg::ShowBase(bool key, bool next)
  429. {
  430.  
  431. if(!compareResult)
  432. return;
  433.  
  434. if(compareResult->size() == 0)
  435. {
  436. MessageBox("База данных отпечатков пуста", "Сообщение", MB_OK);
  437. return;
  438. }
  439.  
  440. if (showIter == NULL)
  441. showIter = compareResult->begin();
  442. else
  443. {
  444. if(next)
  445. if(key)
  446. {
  447. showIter++;
  448.  
  449. if(showIter == compareResult->end())
  450. showIter = compareResult->begin();
  451. }
  452. else
  453. {
  454. if(showIter == compareResult->begin())
  455. showIter = compareResult->end();
  456. showIter--;
  457. }
  458. }
  459.  
  460. CFingPicture *pic;
  461. pic = new CFingPicture(this->GetDC());
  462.  
  463. if(!pic->Load(BLANK))
  464. return;
  465.  
  466. CPaintDC dc(this); // device context for painting
  467.  
  468. list<CPairSur>::iterator is = showIter->surdots.begin();
  469. list<CPairAbsDot>::iterator id = showIter->dots.begin();
  470.  
  471. for(; id != showIter->dots.end(); id++, is++)
  472. {
  473. COLORREF col;
  474.  
  475. if(is->first->empty())
  476. col = 0xBBBBBB;
  477. else
  478. col = (id->first.type)?0xff0000:0x000000;
  479.  
  480. pic->Line(id->first.coord, id->first.coord, 5, col);
  481. pic->Line(id->first.coord,
  482. CPoint(id->first.coord.x+(int)(10.0*cos(id->first.alpha)),id->first.coord.y-(int)(10.0*sin(id->first.alpha))),2, col);
  483.  
  484. if(is->first->empty())
  485. continue; //окружения для этой точки нет
  486.  
  487. //проверка, что "мышь" находится над точкой
  488. if( abs(mousePos.x-id->first.coord.x)<6 && abs(mousePos.y-id->first.coord.y)<6 )
  489. {
  490. CFingPicture pic2(this->GetDC());
  491.  
  492. if(!pic2.Load(BLANK))
  493. return;
  494.  
  495. pic2.Copy(*picture->GetPic2());
  496.  
  497. for(listCRelDot::iterator ii = is->first->begin(); ii != is->first->end(); ii++)
  498. {
  499. COLORREF cl = 0x554444;
  500. CPoint cd;
  501. cd.x = (long)(id->first.coord.x - ii->l * cos(ii->a1*M_PI/180.0 - id->first.alpha));
  502. cd.y = (long)(id->first.coord.y - ii->l * sin(ii->a1*M_PI/180.0 - id->first.alpha));
  503. pic->Line(id->first.coord, cd, 1, cl);
  504. }
  505.  
  506. for(listCRelDot::iterator ii = is->second->begin(); ii != is->second->end(); ii++)
  507. {
  508. COLORREF cl = 0x554444;
  509. CPoint cd;
  510. cd.x = (long)(id->second.coord.x - ii->l * cos(ii->a1*M_PI/180.0 - id->second.alpha));
  511. cd.y = (long)(id->second.coord.y - ii->l * sin(ii->a1*M_PI/180.0 - id->second.alpha));
  512. pic2.Line(id->second.coord, cd, 1, cl);
  513. }
  514.  
  515. pic2.Show(545, 45);
  516. }
  517. }
  518.  
  519. if (pic != NULL)
  520. {
  521. pic->Show(110, 45);
  522. m_WorkFile = showIter->name;
  523. }
  524.  
  525. UpdateData(false);
  526. delete(pic);
  527. }
  528.  
  529. void CFingerAnalyserDlg::PrintReport(CString file, CString report)
  530. {
  531. FILE *outf = fopen("report.txt", "a");
  532. CString msg = "\n------ "+file+" ------\n"+report;
  533. fprintf(outf, msg);
  534. fclose(outf);
  535. }
  536. CString CFingerAnalyserDlg::GetSAV(CString srcName)
  537. {
  538. CString fsav = srcName.Left(srcName.GetLength() - 3) + "sav";
  539. while(fsav.Find("\\") != -1){ fsav = fsav.Right(fsav.GetLength() - fsav.Find("\\")-1); }
  540. return pathToSAV + fsav;
  541. }
  542.  
  543. //загрузить точки из БД
  544. listCInfo *CFingerAnalyserDlg::LoadDB(CString dbFile)
  545. {
  546. listCInfo *bse = new listCInfo();
  547. CInfo finf; //данные по отпечатку
  548.  
  549. FILE *fbse = fopen(dbFile, "rb");
  550.  
  551. if(fbse == NULL)
  552. {
  553. // MessageBox("Невозможно загрузить базу данных с отпечатками", "Ошибка загрузки БД", MB_OK);
  554. return bse;
  555. }
  556.  
  557. int count = 0;
  558. fread((void*)&count, sizeof(count), 1, fbse);
  559.  
  560. for(;count > 0; count--)
  561. {
  562. finf.Scanf(fbse);
  563. bse->push_back(finf);
  564. }
  565.  
  566. fclose(fbse);
  567.  
  568. return bse;
  569. }
  570.  
  571. //сравнить точку с точками в БД
  572. list<CCompareFing> *CFingerAnalyserDlg::CompareWithBase()
  573. {
  574. listCInfo *bse;
  575. list<CCompareFing> *cFng;
  576. cFng = new list<CCompareFing>;
  577. bse = LoadDB(dbFile);
  578.  
  579. if(bse->empty())
  580. {
  581. MessageBox("База данных отпечатков пуста", "Сообщение", MB_OK);
  582. return cFng;
  583. }
  584.  
  585. CAbsFing aFng;
  586. CRelFing baseFng;
  587.  
  588. m_ProgressCompare.SetRange(0, (short)bse->size());
  589.  
  590. for(list<CInfo>::iterator ibse = bse->begin();ibse != bse->end(); ibse++)
  591. {
  592.  
  593. if(!aFng.LoadFing(GetSAV(ibse->src)))
  594. continue;
  595.  
  596. baseFng.Convert(aFng);
  597. CCompareFing compareRes = fingR.Compare(baseFng);
  598. compareRes.name = ibse->src;
  599. cFng->push_back(compareRes);
  600. m_ProgressCompare.SetPos((int)cFng->size());
  601. }
  602.  
  603. bse->clear();
  604. m_ProgressCompare.SetPos(0);
  605. delete(bse);
  606. return cFng;
  607. }
  608.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:20: fatal error: stdafx.h: No such file or directory
 #include "stdafx.h"
                    ^
compilation terminated.
stdout
Standard output is empty