// UploadPhoto.cpp : implementation file // #include "stdafx.h" #include "ylgl.h" #include "UploadPhoto.h" #include "vfw.h" #include "ShowPic22.h" #define THUMBNAIL_WIDTH 80 #define THUMBNAIL_HEIGHT 72 ///////////////////////////////////////////////////////////////////////////// // UploadPhoto dialog UploadPhoto::UploadPhoto(CWnd* pParent /*=NULL*/) : CDialog(UploadPhoto::IDD, pParent) { //{{AFX_DATA_INIT(UploadPhoto) // NOTE: the ClassWizard will add member initialization here m_bTerminate=false; m_bRunning=false; m_hThread = NULL; //}}AFX_DATA_INIT } void UploadPhoto::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(UploadPhoto) DDX_Control(pDX, IDC_LIST1, m_List1); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(UploadPhoto, CDialog) //{{AFX_MSG_MAP(UploadPhoto) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_BN_CLICKED(IDC_BUTTON3, OnButton3) ON_WM_DESTROY() ON_BN_CLICKED(IDC_BUTTON4, OnButton4) ON_BN_CLICKED(IDC_BUTTON5, OnButton5) ON_BN_CLICKED(IDC_BUTTON6, OnButton6) ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // UploadPhoto message handlers void UploadPhoto::OnButton1() { // TODO: Add your control notification handler code here 426,320 CAPTUREPARMS m_CaptureParams; CRect rc; GetDlgItem(IDC_STATIC1)->GetWindowRect(rc); ScreenToClient(rc); m_hCapture=capCreateCaptureWindow("Capture",WS_CHILD|WS_VISIBLE,rc.left , rc.top , rc.Width (), rc.Height (),m_hWnd,0); //创建捕捉窗口 if (!m_hCapture) { AfxMessageBox("CreateCaptureWindow Failure"); //连接不成功 } BOOL bConn=0; for(int i=0; i<100; i++) { if(capDriverConnect(m_hCapture,0))//连接设备驱动程序 { bConn=1; break; } } if(!bConn) AfxMessageBox("驱动程序连接失败"); //Driver Connect Failure if(!capCaptureGetSetup(m_hCapture,&m_CaptureParams,sizeof(m_CaptureParams)))//得到相关的设置 AfxMessageBox("视频格式设置失败");//如果驱动不支持此格式将返回0 ,Fail to set the video format capPreviewScale(m_hCapture,TRUE); capPreviewRate(m_hCapture,66);//预览桢速 capPreview(this->m_hCapture,TRUE);//可以预览 } void UploadPhoto::OnButton3() { // TODO: Add your control notification handler code here capDlgVideoSource(m_hCapture);//设置源 } void UploadPhoto::LoadPhotos() { m_patharray1.RemoveAll (); m_path.MakeLower (); CStringArray dirarray; CString str; if(1) { CString filename; using namespace helper_coffs; ffsco o; o.dirs(0); o.find(LPCSTR(m_path), LPCSTR("*.jpg")); ffsco::typeT coo; ffsco::typeT::iterator it; coo = o.co_file(); for (it = coo.begin(); coo.end() != it; it ++) { str=(*it).c_str(); if(str.GetAt (str.ReverseFind ('\\')+1)=='s')continue; if(str.GetAt (str.ReverseFind ('\\')+1)=='m')continue; str.MakeLower (); m_patharray1.Add (str); } } TerminateThread(); CStringArray *pArray=&m_patharray1; m_vFileName.clear(); for(int i=0; iGetSize (); i++) { CString filePath=pArray->ElementAt (i); filePath.MakeLower(); m_vFileName.push_back(filePath); } LoadThumbImages(); } unsigned __stdcall LoadThumbNail18(LPVOID lpParam) { try { UploadPhoto *pDlg=(UploadPhoto*)lpParam; CListCtrl *ListCtrl=&pDlg->m_List1; CImageList* pImgList=&pDlg->m_ImageListThumb; // reset our image list for(int i=0; iGetImageCount(); i++) pImgList->Remove(i); // remove all items from list view ListCtrl->DeleteAllItems(); pImgList->SetImageCount(pDlg->m_vFileName.size()); TCHAR path[MAX_PATH]; vector::iterator iter; // Set redraw to FALSE to avoid flickering during adding new items ListCtrl->SetRedraw(FALSE); int nIndex=0; CString str, spath; for(iter=pDlg->m_vFileName.begin(); iter!=pDlg->m_vFileName.end() && pDlg->m_bTerminate!=true; iter++, nIndex++) { str=*iter; spath.Empty (); if(str.Left (str.ReverseFind ('\\')+1)!=pDlg->m_path) { spath=str.Left (str.ReverseFind ('\\')+1); spath= spath.Right (spath.GetLength ()-pDlg->m_path.GetLength ()); } str=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1); str=spath+str.Left (str.GetLength ()-4); ListCtrl->InsertItem(nIndex, str, nIndex); } ListCtrl->SetRedraw(TRUE); ListCtrl->Invalidate(); const float fRatio=(float)THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH; int XDest, YDest, nDestWidth, nDestHeight; nIndex=0; SolidBrush whitebrush(Color(255,255,255,255)); for(iter=pDlg->m_vFileName.begin(); iter!=pDlg->m_vFileName.end() && pDlg->m_bTerminate!=true; iter++, nIndex++) { // Load Image File Image *image=NULL; Bitmap *pBmp=NULL; str=*iter; spath=str.Left (str.ReverseFind ('\\')+1); spath+="s"; spath+=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1); if(PathFileExists (spath)) ::LoadImageFromBuf (&image, spath); else ::LoadImageFromBuf (&image, str); int orientation=GetOrientation(image); if(orientation==8) image->RotateFlip( Rotate270FlipNone ); else if(orientation==6) image->RotateFlip( Rotate90FlipNone ); if(image->GetWidth()==0) continue; // Calculate Rect to fit to canvas const float fImgRatio=(float)image->GetHeight()/image->GetWidth(); if(fImgRatio > fRatio) { nDestWidth=THUMBNAIL_HEIGHT/fImgRatio; XDest=(THUMBNAIL_WIDTH-nDestWidth)/2; YDest=0; nDestHeight=THUMBNAIL_HEIGHT; } else { XDest=0; nDestWidth=THUMBNAIL_WIDTH; nDestHeight=THUMBNAIL_WIDTH*fImgRatio; YDest=(THUMBNAIL_HEIGHT-nDestHeight)/2; } pBmp=new Bitmap(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, PixelFormat24bppRGB ); Graphics graph2(pBmp); graph2.Clear(Color(255,192,192,192)); graph2.FillRectangle (&whitebrush,1, 1, THUMBNAIL_WIDTH-2, THUMBNAIL_HEIGHT-2); Graphics graph(pBmp); Rect desRect(XDest, YDest, nDestWidth, nDestHeight); // Draw Image graph.DrawImage(image, desRect, 0,0,image->GetWidth(),image->GetHeight(),UnitPixel); delete image; // Attach to Bitmap and Replace image in CImageList CBitmap bitmap; HBITMAP hbmp; pBmp->GetHBITMAP(Color(255,255,255,255), &hbmp); bitmap.Attach(hbmp); pImgList->Replace(nIndex, &bitmap, NULL); delete pBmp; // Redraw only a current item for removing flickering and fast speed. ListCtrl->RedrawItems(nIndex, nIndex); // Release used DC and Object } ListCtrl->Invalidate(); pDlg->m_bRunning=false; pDlg->m_bTerminate=false; _endthreadex( 0 ); return 0; } catch(...) { } } void UploadPhoto::LoadThumbImages() { TerminateThread(); m_hThread = (HANDLE)_beginthreadex(NULL,0, LoadThumbNail18,(LPVOID)this,0/* CREATE_SUSPENDED*/,&m_dwThreadID); m_bRunning=true; } BOOL UploadPhoto::TerminateThread() { if ( !m_bRunning ) return TRUE; m_bTerminate=true; for( ; ; ) { if ( ::WaitForSingleObject(m_hThread, 0) == WAIT_OBJECT_0 ) break; MSG msg; while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) { if (!AfxGetApp()->PumpMessage()) break; } } ::CloseHandle(m_hThread); return TRUE; } void UploadPhoto::OnDestroy() { TerminateThread(); CDialog::OnDestroy(); // TODO: Add your message handler code here } BOOL UploadPhoto::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_ImageListThumb.Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR24, 0, 1); m_List1.SetImageList(&m_ImageListThumb, LVSIL_NORMAL); LoadPhotos(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } int GetFileNo(CString dir) { CString path; CString path2; CString path3; CString path4; int begin=1; CString temp; CStringArray array; using namespace helper_coffs; ffsco o; o.dirs(1); o.find(LPCSTR(dir), LPCSTR("*.*")); ffsco::typeT coo; ffsco::typeT::iterator it; coo = o.co_dir(); for (it = coo.begin(); coo.end() != it; it ++) { temp=(*it).c_str(); if(::FindArray (&array, temp)==-1) array.Add (temp); } BOOL bFind=1; while(bFind) { bFind=0; for(int i=0; iGetWidth(), m_pImg->GetHeight(), rc); Image *pTemp=m_pImg->GetThumbnailImage(rc.Width (), rc.Height (), NULL, NULL); Graphics graphic(pTemp); graphic.SetSmoothingMode(SmoothingModeHighQuality); graphic.DrawImage(m_pImg, 0,0,pTemp->GetWidth(), pTemp->GetHeight() ); CString savepath; savepath.Format("%s%03d.jpg", m_path, GetFileNo(m_path) ); ::SaveImageToFile (pTemp, savepath); delete m_pImg; delete pTemp;LoadPhotos(); } } void UploadPhoto::OnButton5() { // TODO: Add your control notification handler code here POSITION pos; pos=m_List1.GetFirstSelectedItemPosition(); if(pos==NULL) { AfxMessageBox("请先选中您要删除的照片!", MB_ICONINFORMATION); return; } if(AfxMessageBox("删除后将无法恢复, 确认删除吗?", MB_YESNO|MB_ICONSTOP)!=IDYES)return; CStringArray delarray; while (pos) { int iItem=m_List1.GetNextSelectedItem(pos); CString title=m_List1.GetItemText (iItem, 0); ::DeleteFile (m_path+title+".jpg"); // 更新时删除jpg文件; ::DeleteFile (m_path+"s"+title+".jpg"); // 更新时删除s的jpg小图; } LoadPhotos(); } //extern void DeleteDirectory(CString strDir); void UploadPhoto::OnButton6() { // TODO: Add your control notification handler code here if(AfxMessageBox("删除后将无法恢复, 确认删除吗?", MB_YESNO|MB_ICONSTOP)!=IDYES)return; CString path=m_path; path.TrimRight ('\\'); DeleteDirectory(path); // 递归删除更新时文件; LoadPhotos(); ::CreateDirectory (m_path, NULL); } void UploadPhoto::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here POSITION pos; pos=m_List1.GetFirstSelectedItemPosition(); if(pos==NULL) { return; } int iItem=m_List1.GetNextSelectedItem(pos); ShowPic22 dlg; dlg.m_pos=iItem; dlg.m_pPathArray=&m_patharray1; dlg.DoModal (); *pResult = 0; }