| 代码如下: typedef struct
{
void* pParam;
char dstPath[256];
char aviPath[256];
}setStu;
bool KCreateFolder(CString folder)
{
folder += "/";
folder.Replace("/","\\");
int index=2;
bool isOk = true;
while(1)
{
// folder.Delete(0, index+1);
index = folder.Find("\\",index+1);
index = index<0? folder.Find("/") : index;
if(index<0) break;
CString str = folder.Left(index);
isOk = (BOOL)CreateDirectory( str, 0 );
//if(!isOk)
// return false;
}
return true;
}
void Karl_OnDraw(IplImage* img, CWnd *cWnd)
{
/* IplImage* dst= cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
cvCopy(img, dst, NULL);
cvNamedWindow( "显示图像", 1 );//创建窗口
cvShowImage( "显示图像", dst );//显示图像
*/
if(cWnd==NULL) return;
CDC *pDC = cWnd->GetDC();
HDC hDC= pDC->GetSafeHdc();
CRect rect;
cWnd->GetClientRect(&rect);
CvvImage cimg;
cimg.CopyOf(img);
cimg.DrawToHDC(hDC,&rect);
cWnd->ReleaseDC(pDC);
}
void savePictures(void* pParam)
{
setStu *stu ;
stu = (setStu*)pParam;
CGetFramesFromAVIDlg *dlg = (CGetFramesFromAVIDlg*)stu->pParam;
dlg->Proc(stu);
}
void CGetFramesFromAVIDlg::Proc(setStu *pstu )
{
if(pstu==NULL)
return;
setStu *stu = new setStu;
stu->pParam = pstu->pParam;
sprintf(stu->aviPath, "%s", pstu->aviPath);
sprintf(stu->dstPath, "%s", pstu->dstPath);
if(strlen(stu->aviPath)<2 || strlen(stu->aviPath)<2 )
return;
CString dstPath(stu->dstPath);
CString aviPath(stu->aviPath);
char savePath[256]={0};
delete stu;
bool showin = false;
char aviFilePath[256]={0};
sprintf(aviFilePath, "%s", aviPath);
// LPWSTR out;
// Utf82Unicode(out,outsize, aviFilePath, strlen(aviFilePath);
CoInitialize(NULL);//初始化com,否则多线程中的cvCaptureFromFile 不能再次使用
CvCapture* capture=0;
capture = cvCaptureFromFile( aviFilePath);
CWnd *window = GetDlgItem(IDC_IMAGE);
if( capture )
{
if(showin) cvNamedWindow( "avi Play", 1 );
int times=0;
while(1)
{
if(!isRun)
break;
times++;
if( !cvGrabFrame( capture ))
break;
IplImage* image = cvQueryFrame( capture );
if( image )
{
sprintf(savePath, "%s\\%d.jpg", dstPath, times);
Karl_OnDraw(image, window);
cvSaveImage(savePath, image);
if(showin) cvShowImage( "avi Play", image );
}
Sleep(2);
}
cvReleaseCapture( &capture );
if(showin) cvDestroyWindow( "avi Play" );
}
cvReleaseCapture( &capture );
CoUninitialize();
}
void CGetFramesFromAVIDlg::OnButton2()
{
UpdateData(true);
setStu stu ;
memset(&stu , 0, sizeof(stu));
sprintf(stu.aviPath,"%s",m_aviPath);
sprintf(stu.dstPath, "%s", m_path);
stu.pParam = this;
KCreateFolder(m_aviPath);
KCreateFolder(m_path);
isRun = true;
_beginthread(savePictures, 0 ,&stu);
}
void CGetFramesFromAVIDlg::OnBtBrowserAvi()
{
CFileDialog fileOpenDlg(TRUE, _T("*.avi"), "",OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,
"avi files (*.avi) |*.avi;|All Files (*.*)|*.*||",NULL);
if (fileOpenDlg.DoModal()!=IDOK) return ;
m_aviPath = fileOpenDlg.GetPathName();
UpdateData(false);
}
void CGetFramesFromAVIDlg::OnButton1()
{
isRun = false;
} |