UploadPhoto.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // UploadPhoto.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ylgl.h"
  5. #include "UploadPhoto.h"
  6. #include "vfw.h"
  7. #include "ShowPic22.h"
  8. #define THUMBNAIL_WIDTH 80
  9. #define THUMBNAIL_HEIGHT 72
  10. /////////////////////////////////////////////////////////////////////////////
  11. // UploadPhoto dialog
  12. UploadPhoto::UploadPhoto(CWnd* pParent /*=NULL*/)
  13. : CDialog(UploadPhoto::IDD, pParent)
  14. {
  15. //{{AFX_DATA_INIT(UploadPhoto)
  16. // NOTE: the ClassWizard will add member initialization here
  17. m_bTerminate=false;
  18. m_bRunning=false;
  19. m_hThread = NULL;
  20. //}}AFX_DATA_INIT
  21. }
  22. void UploadPhoto::DoDataExchange(CDataExchange* pDX)
  23. {
  24. CDialog::DoDataExchange(pDX);
  25. //{{AFX_DATA_MAP(UploadPhoto)
  26. DDX_Control(pDX, IDC_LIST1, m_List1);
  27. //}}AFX_DATA_MAP
  28. }
  29. BEGIN_MESSAGE_MAP(UploadPhoto, CDialog)
  30. //{{AFX_MSG_MAP(UploadPhoto)
  31. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  32. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  33. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  34. ON_WM_DESTROY()
  35. ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
  36. ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
  37. ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
  38. ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
  39. //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // UploadPhoto message handlers
  43. void UploadPhoto::OnButton1()
  44. {
  45. // TODO: Add your control notification handler code here 426,320
  46. CAPTUREPARMS m_CaptureParams;
  47. CRect rc;
  48. GetDlgItem(IDC_STATIC1)->GetWindowRect(rc);
  49. ScreenToClient(rc);
  50. m_hCapture=capCreateCaptureWindow("Capture",WS_CHILD|WS_VISIBLE,rc.left , rc.top , rc.Width (), rc.Height (),m_hWnd,0);
  51. //创建捕捉窗口
  52. if (!m_hCapture)
  53. {
  54. AfxMessageBox("CreateCaptureWindow Failure"); //连接不成功
  55. }
  56. BOOL bConn=0;
  57. for(int i=0; i<100; i++)
  58. {
  59. if(capDriverConnect(m_hCapture,0))//连接设备驱动程序
  60. {
  61. bConn=1;
  62. break;
  63. }
  64. }
  65. if(!bConn) AfxMessageBox("驱动程序连接失败"); //Driver Connect Failure
  66. if(!capCaptureGetSetup(m_hCapture,&m_CaptureParams,sizeof(m_CaptureParams)))//得到相关的设置
  67. AfxMessageBox("视频格式设置失败");//如果驱动不支持此格式将返回0 ,Fail to set the video format
  68. capPreviewScale(m_hCapture,TRUE);
  69. capPreviewRate(m_hCapture,66);//预览桢速
  70. capPreview(this->m_hCapture,TRUE);//可以预览
  71. }
  72. void UploadPhoto::OnButton3()
  73. {
  74. // TODO: Add your control notification handler code here
  75. capDlgVideoSource(m_hCapture);//设置源
  76. }
  77. void UploadPhoto::LoadPhotos()
  78. {
  79. m_patharray1.RemoveAll ();
  80. m_path.MakeLower ();
  81. CStringArray dirarray;
  82. CString str;
  83. if(1)
  84. {
  85. CString filename;
  86. using namespace helper_coffs;
  87. ffsco o;
  88. o.dirs(0);
  89. o.find(LPCSTR(m_path), LPCSTR("*.jpg"));
  90. ffsco::typeT coo;
  91. ffsco::typeT::iterator it;
  92. coo = o.co_file();
  93. for (it = coo.begin();
  94. coo.end() != it;
  95. it ++)
  96. {
  97. str=(*it).c_str();
  98. if(str.GetAt (str.ReverseFind ('\\')+1)=='s')continue;
  99. if(str.GetAt (str.ReverseFind ('\\')+1)=='m')continue;
  100. str.MakeLower ();
  101. m_patharray1.Add (str);
  102. }
  103. }
  104. TerminateThread();
  105. CStringArray *pArray=&m_patharray1;
  106. m_vFileName.clear();
  107. for(int i=0; i<pArray->GetSize (); i++)
  108. {
  109. CString filePath=pArray->ElementAt (i);
  110. filePath.MakeLower();
  111. m_vFileName.push_back(filePath);
  112. }
  113. LoadThumbImages();
  114. }
  115. unsigned __stdcall LoadThumbNail18(LPVOID lpParam)
  116. {
  117. try
  118. {
  119. UploadPhoto *pDlg=(UploadPhoto*)lpParam;
  120. CListCtrl *ListCtrl=&pDlg->m_List1;
  121. CImageList* pImgList=&pDlg->m_ImageListThumb;
  122. // reset our image list
  123. for(int i=0; i<pImgList->GetImageCount(); i++)
  124. pImgList->Remove(i);
  125. // remove all items from list view
  126. ListCtrl->DeleteAllItems();
  127. pImgList->SetImageCount(pDlg->m_vFileName.size());
  128. TCHAR path[MAX_PATH];
  129. vector<CString>::iterator iter;
  130. // Set redraw to FALSE to avoid flickering during adding new items
  131. ListCtrl->SetRedraw(FALSE);
  132. int nIndex=0;
  133. CString str, spath;
  134. for(iter=pDlg->m_vFileName.begin(); iter!=pDlg->m_vFileName.end() && pDlg->m_bTerminate!=true; iter++, nIndex++)
  135. {
  136. str=*iter;
  137. spath.Empty ();
  138. if(str.Left (str.ReverseFind ('\\')+1)!=pDlg->m_path)
  139. {
  140. spath=str.Left (str.ReverseFind ('\\')+1);
  141. spath= spath.Right (spath.GetLength ()-pDlg->m_path.GetLength ());
  142. }
  143. str=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1);
  144. str=spath+str.Left (str.GetLength ()-4);
  145. ListCtrl->InsertItem(nIndex, str, nIndex);
  146. }
  147. ListCtrl->SetRedraw(TRUE);
  148. ListCtrl->Invalidate();
  149. const float fRatio=(float)THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH;
  150. int XDest, YDest, nDestWidth, nDestHeight;
  151. nIndex=0;
  152. SolidBrush whitebrush(Color(255,255,255,255));
  153. for(iter=pDlg->m_vFileName.begin(); iter!=pDlg->m_vFileName.end() && pDlg->m_bTerminate!=true; iter++, nIndex++)
  154. {
  155. // Load Image File
  156. Image *image=NULL;
  157. Bitmap *pBmp=NULL;
  158. str=*iter;
  159. spath=str.Left (str.ReverseFind ('\\')+1);
  160. spath+="s";
  161. spath+=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1);
  162. if(PathFileExists (spath))
  163. ::LoadImageFromBuf (&image, spath);
  164. else
  165. ::LoadImageFromBuf (&image, str);
  166. int orientation=GetOrientation(image);
  167. if(orientation==8)
  168. image->RotateFlip( Rotate270FlipNone );
  169. else if(orientation==6)
  170. image->RotateFlip( Rotate90FlipNone );
  171. if(image->GetWidth()==0)
  172. continue;
  173. // Calculate Rect to fit to canvas
  174. const float fImgRatio=(float)image->GetHeight()/image->GetWidth();
  175. if(fImgRatio > fRatio)
  176. {
  177. nDestWidth=THUMBNAIL_HEIGHT/fImgRatio;
  178. XDest=(THUMBNAIL_WIDTH-nDestWidth)/2;
  179. YDest=0;
  180. nDestHeight=THUMBNAIL_HEIGHT;
  181. }
  182. else
  183. {
  184. XDest=0;
  185. nDestWidth=THUMBNAIL_WIDTH;
  186. nDestHeight=THUMBNAIL_WIDTH*fImgRatio;
  187. YDest=(THUMBNAIL_HEIGHT-nDestHeight)/2;
  188. }
  189. pBmp=new Bitmap(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, PixelFormat24bppRGB );
  190. Graphics graph2(pBmp);
  191. graph2.Clear(Color(255,192,192,192));
  192. graph2.FillRectangle (&whitebrush,1, 1, THUMBNAIL_WIDTH-2, THUMBNAIL_HEIGHT-2);
  193. Graphics graph(pBmp);
  194. Rect desRect(XDest, YDest, nDestWidth, nDestHeight);
  195. // Draw Image
  196. graph.DrawImage(image, desRect, 0,0,image->GetWidth(),image->GetHeight(),UnitPixel);
  197. delete image;
  198. // Attach to Bitmap and Replace image in CImageList
  199. CBitmap bitmap;
  200. HBITMAP hbmp;
  201. pBmp->GetHBITMAP(Color(255,255,255,255), &hbmp);
  202. bitmap.Attach(hbmp);
  203. pImgList->Replace(nIndex, &bitmap, NULL);
  204. delete pBmp;
  205. // Redraw only a current item for removing flickering and fast speed.
  206. ListCtrl->RedrawItems(nIndex, nIndex);
  207. // Release used DC and Object
  208. }
  209. ListCtrl->Invalidate();
  210. pDlg->m_bRunning=false;
  211. pDlg->m_bTerminate=false;
  212. _endthreadex( 0 );
  213. return 0;
  214. }
  215. catch(...)
  216. {
  217. }
  218. }
  219. void UploadPhoto::LoadThumbImages()
  220. {
  221. TerminateThread();
  222. m_hThread = (HANDLE)_beginthreadex(NULL,0, LoadThumbNail18,(LPVOID)this,0/* CREATE_SUSPENDED*/,&m_dwThreadID);
  223. m_bRunning=true;
  224. }
  225. BOOL UploadPhoto::TerminateThread()
  226. {
  227. if ( !m_bRunning )
  228. return TRUE;
  229. m_bTerminate=true;
  230. for( ; ; )
  231. {
  232. if ( ::WaitForSingleObject(m_hThread, 0) == WAIT_OBJECT_0 )
  233. break;
  234. MSG msg;
  235. while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
  236. {
  237. if (!AfxGetApp()->PumpMessage())
  238. break;
  239. }
  240. }
  241. ::CloseHandle(m_hThread);
  242. return TRUE;
  243. }
  244. void UploadPhoto::OnDestroy()
  245. {
  246. TerminateThread();
  247. CDialog::OnDestroy();
  248. // TODO: Add your message handler code here
  249. }
  250. BOOL UploadPhoto::OnInitDialog()
  251. {
  252. CDialog::OnInitDialog();
  253. // TODO: Add extra initialization here
  254. m_ImageListThumb.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1);
  255. m_List1.SetImageList(&m_ImageListThumb, LVSIL_NORMAL);
  256. LoadPhotos();
  257. return TRUE; // return TRUE unless you set the focus to a control
  258. // EXCEPTION: OCX Property Pages should return FALSE
  259. }
  260. int GetFileNo(CString dir)
  261. {
  262. CString path;
  263. CString path2;
  264. CString path3;
  265. CString path4;
  266. int begin=1;
  267. CString temp;
  268. CStringArray array;
  269. using namespace helper_coffs;
  270. ffsco o;
  271. o.dirs(1);
  272. o.find(LPCSTR(dir), LPCSTR("*.*"));
  273. ffsco::typeT coo;
  274. ffsco::typeT::iterator it;
  275. coo = o.co_dir();
  276. for (it = coo.begin();
  277. coo.end() != it;
  278. it ++)
  279. {
  280. temp=(*it).c_str();
  281. if(::FindArray (&array, temp)==-1)
  282. array.Add (temp);
  283. }
  284. BOOL bFind=1;
  285. while(bFind)
  286. {
  287. bFind=0;
  288. for(int i=0; i<array.GetSize (); i++)
  289. {
  290. dir=array.ElementAt (i);
  291. path.Format ("%s%03d.jpg", dir, begin);
  292. path2.Format ("%s%03d.raw", dir, begin);
  293. path3.Format ("%s%03d.cr2", dir, begin);
  294. path4.Format ("%s%03d.NEF", dir, begin);
  295. while(PathFileExists (path) || PathFileExists (path2) || PathFileExists (path3) || PathFileExists (path4))
  296. {
  297. begin++;
  298. path.Format ("%s%03d.jpg", dir, begin);
  299. path2.Format ("%s%03d.raw", dir, begin);
  300. path3.Format ("%s%03d.cr2", dir, begin);
  301. path4.Format ("%s%03d.NEF", dir, begin);
  302. bFind=1;
  303. }
  304. }
  305. }
  306. return begin;
  307. }
  308. void UploadPhoto::OnButton2()
  309. {
  310. // TODO: Add your control notification handler code here
  311. CString str1="d:\\1.jpg";
  312. str1.Format("%s%03d.jpg", m_path, GetFileNo(m_path) );
  313. char *ch;
  314. ch=(LPSTR)(LPCTSTR)str1;
  315. if(capFileSaveDIB(m_hCapture,ch))
  316. {
  317. LoadPhotos();
  318. }
  319. }
  320. extern void SaveImageToFile(Image *img, CString path);
  321. void UploadPhoto::OnButton4()
  322. {
  323. // TODO: Add your control notification handler code here
  324. CFileDialog fdlg(true, NULL,"", OFN_HIDEREADONLY, "jpg files(*jpg)|*.jpg||");
  325. if(fdlg.DoModal ()!=IDOK)return;
  326. CString path=fdlg.GetPathName();
  327. Image *m_pImg=NULL;
  328. ::LoadImageFromBuf (&m_pImg, path);
  329. if(m_pImg==NULL)
  330. AfxMessageBox("无效图片文件!");
  331. else
  332. {
  333. CRect rc(0,0,640,640);
  334. RectFitDes(m_pImg->GetWidth(), m_pImg->GetHeight(), rc);
  335. Image *pTemp=m_pImg->GetThumbnailImage(rc.Width (), rc.Height (), NULL, NULL);
  336. Graphics graphic(pTemp);
  337. graphic.SetSmoothingMode(SmoothingModeHighQuality);
  338. graphic.DrawImage(m_pImg, 0,0,pTemp->GetWidth(), pTemp->GetHeight() );
  339. CString savepath;
  340. savepath.Format("%s%03d.jpg", m_path, GetFileNo(m_path) );
  341. ::SaveImageToFile (pTemp, savepath);
  342. delete m_pImg;
  343. delete pTemp;LoadPhotos();
  344. }
  345. }
  346. void UploadPhoto::OnButton5()
  347. {
  348. // TODO: Add your control notification handler code here
  349. POSITION pos;
  350. pos=m_List1.GetFirstSelectedItemPosition();
  351. if(pos==NULL)
  352. {
  353. AfxMessageBox("请先选中您要删除的照片!", MB_ICONINFORMATION);
  354. return;
  355. }
  356. if(AfxMessageBox("删除后将无法恢复, 确认删除吗?", MB_YESNO|MB_ICONSTOP)!=IDYES)return;
  357. CStringArray delarray;
  358. while (pos)
  359. {
  360. int iItem=m_List1.GetNextSelectedItem(pos);
  361. CString title=m_List1.GetItemText (iItem, 0);
  362. ::DeleteFile (m_path+title+".jpg"); // 更新时删除jpg文件;
  363. ::DeleteFile (m_path+"s"+title+".jpg"); // 更新时删除s的jpg小图;
  364. }
  365. LoadPhotos();
  366. }
  367. //extern void DeleteDirectory(CString strDir);
  368. void UploadPhoto::OnButton6()
  369. {
  370. // TODO: Add your control notification handler code here
  371. if(AfxMessageBox("删除后将无法恢复, 确认删除吗?", MB_YESNO|MB_ICONSTOP)!=IDYES)return;
  372. CString path=m_path;
  373. path.TrimRight ('\\');
  374. DeleteDirectory(path); // 递归删除更新时文件;
  375. LoadPhotos();
  376. ::CreateDirectory (m_path, NULL);
  377. }
  378. void UploadPhoto::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
  379. {
  380. // TODO: Add your control notification handler code here
  381. POSITION pos;
  382. pos=m_List1.GetFirstSelectedItemPosition();
  383. if(pos==NULL)
  384. {
  385. return;
  386. }
  387. int iItem=m_List1.GetNextSelectedItem(pos);
  388. ShowPic22 dlg;
  389. dlg.m_nCurImgPos=iItem;
  390. dlg.m_AryOfImgPath=&m_patharray1;
  391. dlg.DoModal ();
  392. *pResult = 0;
  393. }