因为要对图片进行批量处理,所以需要遍历文件夹下所有图片,其中有许多注意和踩的坑,但最终还是实现了相关功能,现在将写完的遍历函数记录下来供大家参考参考。
#include<io.h>
#include<iostream>
using namespace std;
void get_image_names(std::string file_path, std::vector<std::string>& file_names)
{
intptr_t hFile = 0;
_finddata_t fileInfo;
hFile = _findfirst(file_path.c_str(), &fileInfo);
if (hFile != -1){
do{
//如果为文件夹,可以通过递归继续遍历,此处我们不需要
if ((fileInfo.attrib & _A_SUBDIR)){
continue;
}
//如果为单个文件直接push_back
else{
file_names.push_back(fileInfo.name);
cout << fileInfo.name << endl;
}
} while (_findnext(hFile, &fileInfo) ==0);
_findclose(hFile);
}
}
int main()
{
std::vector<std::string> file_names;
get_image_names("./test2/*.jpg", file_names);
return 0;
}
最终结果: