如何在vc中读入一个文件夹里面的所有图像
1:
CString strDir;//储存文件夹路径
CFileFind ff;
......... /* 获取文件夹路径*/
strDir+="*.*";//如果是只需要某种文件,怎么替换应该很简单了吧
BOOL res =ff.FindFile(strDir);
while(res)
{
res=ff.FindNextFile();
if(ff.IsDirectory){/*如果是文件夹就XXXXX*/};
else
{ //ff.GetFileName就是每个文件名,比如如下设计
CString s;
s.Format("%s",ff.GetFileName());
///干你想干的事情吧
///不过注意有时候会把你看不到得系统文件也读出来
}
}
2:
VC代码,功能很完整。可以只选择文件夹中的一幅图像,然后点OK,会自动遍历
整个文件夹。读取各个文件的完整路径到一个vector中,并不把所有图像读进内存,这样可以
节省不必要的内存开销。 在处理函数中,根据这些完整的路径,打开一个处理一个释放一个。
直到处理完所有图像。
- C/C++ code
-
int CBatch::FileOpenEx()
{
int counter = 0 ;
CString ss = "" ;
pair < map < CString,CString > ::iterator, bool > Pair_Insert;
// TODO: Add your command handler code here
int nimg = 0 ;
CString ftitle,fname,fpath,fpathname,froot;
CString mp[ 7 ];
static char szFilter[] = " *.BMP|*.BMP|All Files(*.*)|*.*|| " ;
CFileDialog FileDlg( TRUE, NULL, NULL,OFN_HIDEREADONLY, szFilter );
if ( FileDlg.DoModal() == IDOK )
{
fpathname = FileDlg.GetPathName();
CFileFind finder;
finder.FindFile(fpathname);
finder.FindNextFile();
froot = finder.GetRoot();
BOOL bResult;
fpathname = froot + _T( " //*.* " ); // 找到该文件的文件夹,
bResult = finder.FindFile(fpathname); // 然后从头开始一个一个遍历
while (bResult)
{
counter ++ ;
bResult = finder.FindNextFile();
if (finder.IsDots()) continue ;
if (finder.IsDirectory()) continue ;
fname = finder.GetFileName();
if ( " bmp " != fname.Right( 3 ) && " BMP " != fname.Right( 3 ) ) continue ;
// 找到了一个bmp文件,开始提取相关信息
FPRO fpro; // 自定义的一个结构体
fpro.filePath = finder.GetFilePath(); // 路径和文件名,包括扩展名。
fpro.fileName = finder.GetFileName();
fpro.fileTitle = finder.GetFileTitle();
m_imvec.push_back(fpro);
} // while
} // if
BatchProcess();
return 1 ;
}
int CBatch::BatchProcess()
{
CMainFrame * pMain = (CMainFrame * ) AfxGetApp() -> m_pMainWnd;
CBlurEstimation3View * pView = (CBlurEstimation3View * ) pMain -> GetActiveView();
IMIter it_start,it_end,it;
for (it = m_imvec.begin(); it != m_imvec.end(); ++ it)
{
CString Dir = it -> filePath;
if (dib.Load(Dir))
{
CClientDC dc(pView);
dib.SetPalette( & dc);
dib.Draw( & dc);
}
else
continue ;
int width = dib.m_Width;
int height = dib.m_Height;
int srcLineBytes = dib.m_DataSize / height;
int residual = 4 - width % 4 ;
if (residual == 4 )
residual = 0 ;
if (dib.m_DataSize == width * height)
residual = 0 ;
width = width - width % 4 ;
#pragma region
// 分配内存,读取1D图像数据
BYTE * pBits = new BYTE[width * height];
// 分配内存,读取2D 图像数据
// BYTE **p2DBits = new BYTE *[height];
// for(int i=0; i<height; ++i)
// p2DBits[i] = new BYTE[width];
BYTE pixel;
for ( int i = 0 ; i < height; ++ i)
for ( int j = 0 ; j < width; ++ j)
{
pixel = dib.m_pDibBits[srcLineBytes * i + j];
// p2DBits[i][j] = pixel;
pBits[i * width + j] = pixel;
}
#pragma endregion
float param = 0 ;
// ***************************************************************
// 此处添加代码
// 处理
// end此处添加代码
// ****************************************************************
CString str;
str.Format( " %f " ,param);
it -> param = str;
// for(i=0; i<height;++i)
// delete []p2DBits[i];
// delete []p2DBits;
delete []pBits;
}
return 1 ;
}

浙公网安备 33010602011771号