123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- // 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; i<pArray->GetSize (); 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; i<pImgList->GetImageCount(); i++)
- pImgList->Remove(i);
- // remove all items from list view
- ListCtrl->DeleteAllItems();
- pImgList->SetImageCount(pDlg->m_vFileName.size());
- TCHAR path[MAX_PATH];
- vector<CString>::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; i<array.GetSize (); i++)
- {
- dir=array.ElementAt (i);
- path.Format ("%s%03d.jpg", dir, begin);
- path2.Format ("%s%03d.raw", dir, begin);
- path3.Format ("%s%03d.cr2", dir, begin);
- path4.Format ("%s%03d.NEF", dir, begin);
- while(PathFileExists (path) || PathFileExists (path2) || PathFileExists (path3) || PathFileExists (path4))
- {
- begin++;
- path.Format ("%s%03d.jpg", dir, begin);
- path2.Format ("%s%03d.raw", dir, begin);
- path3.Format ("%s%03d.cr2", dir, begin);
- path4.Format ("%s%03d.NEF", dir, begin);
- bFind=1;
- }
- }
- }
- return begin;
- }
- void UploadPhoto::OnButton2()
- {
- // TODO: Add your control notification handler code here
- CString str1="d:\\1.jpg";
- str1.Format("%s%03d.jpg", m_path, GetFileNo(m_path) );
- char *ch;
- ch=(LPSTR)(LPCTSTR)str1;
- if(capFileSaveDIB(m_hCapture,ch))
- {
- LoadPhotos();
- }
- }
- extern void SaveImageToFile(Image *img, CString path);
- void UploadPhoto::OnButton4()
- {
- // TODO: Add your control notification handler code here
- CFileDialog fdlg(true, NULL,"", OFN_HIDEREADONLY, "jpg files(*jpg)|*.jpg||");
- if(fdlg.DoModal ()!=IDOK)return;
- CString path=fdlg.GetPathName();
- Image *m_pImg=NULL;
- ::LoadImageFromBuf (&m_pImg, path);
- if(m_pImg==NULL)
- AfxMessageBox("无效图片文件!");
- else
- {
- CRect rc(0,0,640,640);
- RectFitDes(m_pImg->GetWidth(), 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_nCurImgPos=iItem;
- dlg.m_AryOfImgPath=&m_patharray1;
- dlg.DoModal ();
- *pResult = 0;
- }
|