C++中如何实现,遍历整个文件夹,处理所选文件夹中的所有图片

richiessh 2012-09-07 05:46:13
我之前写的程序相当于只读取一个一幅图像进行处理,处理过后需要手动保存。现在如果想要实现批量处理一个文件夹下的所有图片,应该对程序做哪些修改,不知道怎么入手,请高手帮帮忙! 最好有源码 谢谢
...全文
2054 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gloveing 2012-09-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
agoago_2009:
你好,我当前创建的是一个MFC单文档的程序,起初是通过DOC::opendocement来载入一幅图像,通过Ipicture类 将图像都转换为位图格式,继而进行处理,由于第一次接触遍历整个文件夹,对文件夹下的所有图像进行处理,不知道3楼的ProcPicDir()这一步具体应该放在哪里执行,还有这种批量处理完之后,图像都是怎么保存的?
[/Quote]
你可以创建一个菜单,点击菜单就开始执行ProcPicDir(),而且在找到文件的同时就可以对图片进行操作了啊
richiessh 2012-09-08
  • 打赏
  • 举报
回复
agoago_2009:
你好,我当前创建的是一个MFC单文档的程序,起初是通过DOC::opendocement来载入一幅图像,通过Ipicture类 将图像都转换为位图格式,继而进行处理,由于第一次接触遍历整个文件夹,对文件夹下的所有图像进行处理,不知道3楼的ProcPicDir()这一步具体应该放在哪里执行,还有这种批量处理完之后,图像都是怎么保存的?
Gloveing 2012-09-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
引用 3 楼 的回复:

C/C++ code

void ProcPicDir(CString strPicDir)
{
CFileFind fileFinder;

if (strPicDir.Right(1) == TEXT("\\"))
{
int nPos = strPicDir.ReverseFind(TEXT('\\'));
strPicDir =……

你……
[/Quote]
放到:你想对图像进行处理的时候
richiessh 2012-09-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

C/C++ code

void ProcPicDir(CString strPicDir)
{
CFileFind fileFinder;

if (strPicDir.Right(1) == TEXT("\\"))
{
int nPos = strPicDir.ReverseFind(TEXT('\\'));
strPicDir =……
[/Quote]
你好, 程序具体看懂了,但这应该放在程序的哪一部分较好
wqvbjhc 2012-09-07
  • 打赏
  • 举报
回复
#include <io.h>
//查找dir路径下所有文件(dir包括配置符,如c:\*.txt)
//ishavedir表示返回的文件名是否包含全路径,true为返回全路径的文件名,false只返回文件名
vector<string> FindAllFile(const char* dir,bool ishavedir=false)
{
_finddata_t file;
vector<string> file_list;
long lf;
if((lf = _findfirst(dir, &file))==-1l) { //_findfirst返回的是long型; long __cdecl _findfirst(const char *, struct _finddata_t *)
return file_list;
} else {
do {
if (ishavedir) {
string tmppath=dir;
int index=tmppath.find_last_of("*.")-1;
tmppath=tmppath.substr(0,index)+file.name;
file_list.push_back(tmppath);
} else {
file_list.push_back(file.name);
}
} while (_findnext( lf, &file ) == 0);//int _findnext(long, struct _finddata_t *);如果找到下个文件的名字成功的话就返回0,否则返回-1
_findclose(lf);
return file_list;
}
}
  • 打赏
  • 举报
回复

void ProcPicDir(CString strPicDir)
{
CFileFind fileFinder;

if (strPicDir.Right(1) == TEXT("\\"))
{
int nPos = strPicDir.ReverseFind(TEXT('\\'));
strPicDir = strPicDir.Left(nPos);
}

CString strPicFile = TEXT("");
strPicFile.Format(TEXT("%s\\%s"),strPicDir,TEXT("*.*"));

BOOL bWorking = fileFinder.FindFile(strPicFile);
while (bWorking)
{
bWorking = fileFinder.FindNextFile();
if (fileFinder.IsDots())
{
continue;
}

CString strFilePath = fileFinder.GetFilePath();
if (fileFinder.IsDirectory())
{
//继续遍历目录
ProcPicDir(strFilePath);
}
else
{
int nPos = strFilePath.ReverseFind(TEXT('.'));
CString strExt = strFilePath.Right(strFilePath.GetLength() - nPos - 1);
if (strExt.CompareNoCase(TEXT("jpg")) == 0 ||
strExt.CompareNoCase(TEXT("jpeg")) == 0 ||
strExt.CompareNoCase(TEXT("bmp")) == 0)
{
//处理图片
//...

}
}
}
fileFinder.Close();
}
Eleven 2012-09-07
  • 打赏
  • 举报
回复
CFileFind::FindFile/FileNextFile()
oyljerry 2012-09-07
  • 打赏
  • 举报
回复
FindFirstFile/FindNextFile 遍历,保存一个个到list

19,469

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧