278 lines
8.8 KiB
C++
278 lines
8.8 KiB
C++
#include "Hd_VirtualCamera_ReadFile.h"
|
|||
|
|
namespace fs = std::filesystem;
|
||
|
|
/// <summary>
|
||
|
|
/// 初始化时读取目录的文件
|
||
|
|
/// </summary>
|
||
|
|
std::vector<std::string>files;
|
||
|
|
string filePath;
|
||
|
|
string cameraName;
|
||
|
|
int fileIndex = 0;
|
||
|
|
/// <summary>
|
||
|
|
/// 一次读几张照片
|
||
|
|
/// </summary>
|
||
|
|
int numOfGroup = 1;
|
||
|
|
string mode;
|
||
|
|
/// <summary>
|
||
|
|
/// 输出的图片的容器
|
||
|
|
/// </summary>
|
||
|
|
std::vector<cv::Mat> srcImages;
|
||
|
|
std::vector<string> folders;
|
||
|
|
Hd_VirtualCamera_ReadFile* create()
|
||
|
|
{
|
||
|
|
qDebug() << "[INFO] " << "Hd_VirtualCamera_ReadFile create success!";
|
||
|
|
return new Hd_VirtualCamera_ReadFile;
|
||
|
|
}
|
||
|
|
void destory(Hd_VirtualCamera_ReadFile* ptr)
|
||
|
|
{
|
||
|
|
if (ptr)
|
||
|
|
{
|
||
|
|
delete ptr;
|
||
|
|
ptr = nullptr;
|
||
|
|
}
|
||
|
|
qDebug() << "[INFO] " << "Hd_VirtualCamera_ReadFile destory success!";
|
||
|
|
}
|
||
|
|
|
||
|
|
Hd_VirtualCamera_ReadFile::Hd_VirtualCamera_ReadFile()
|
||
|
|
{
|
||
|
|
m_thread = new MyThread;
|
||
|
|
}
|
||
|
|
|
||
|
|
Hd_VirtualCamera_ReadFile::~Hd_VirtualCamera_ReadFile()
|
||
|
|
{
|
||
|
|
if (m_thread)
|
||
|
|
{
|
||
|
|
m_thread->exit();
|
||
|
|
m_thread->wait(1);
|
||
|
|
delete m_thread;
|
||
|
|
m_thread = nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
qDebug() << "[INFO] " << " delete success!";
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Hd_VirtualCamera_ReadFile::sendData(std::vector<cv::Mat>& mat, QByteArray& data)
|
||
|
|
{
|
||
|
|
|
||
|
|
if (srcImages.size() == 0)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
for (auto img : srcImages)
|
||
|
|
mat.push_back(img);
|
||
|
|
srcImages.clear();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
void findFolders(const std::string& path, std::vector<std::string>& folders) {
|
||
|
|
for (const auto& entry : fs::directory_iterator(path)) {
|
||
|
|
if (fs::is_directory(entry)) {
|
||
|
|
std::string folderPath = entry.path().string();
|
||
|
|
folders.push_back(folderPath);
|
||
|
|
findFolders(folderPath, folders); // 递归调用,查找子文件夹
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
void getAllFiles(string path, vector<string>& files, string format)
|
||
|
|
{
|
||
|
|
intptr_t hFile = 0;//恅璃曆梟
|
||
|
|
struct _finddata_t fileinfo;//恅璃陓洘
|
||
|
|
string p;
|
||
|
|
if ((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1) //恅璃湔婓
|
||
|
|
{
|
||
|
|
do
|
||
|
|
{
|
||
|
|
if ((fileinfo.attrib & _A_SUBDIR))//瓚剿岆瘁峈恅璃標
|
||
|
|
{
|
||
|
|
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)//恅璃標靡笢祥漪"."睿".."
|
||
|
|
{
|
||
|
|
files.push_back(p.assign(path).append("\\").append(fileinfo.name)); //悵湔恅璃標靡
|
||
|
|
getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files, format); //菰寥梢盪恅璃標
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
files.push_back(p.assign(path).append("\\").append(fileinfo.name));//彆祥岆恅璃標ㄛ揣湔恅璃靡
|
||
|
|
}
|
||
|
|
} while (_findnext(hFile, &fileinfo) == 0);
|
||
|
|
_findclose(hFile);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Hd_VirtualCamera_ReadFile::reciveData(std::vector<cv::Mat>& mat, QByteArray& data)
|
||
|
|
{
|
||
|
|
m_flag = false;
|
||
|
|
Username = data.toStdString();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Hd_VirtualCamera_ReadFile::initParas(QByteArray&)
|
||
|
|
{
|
||
|
|
if (Username.find_last_of(",") != 0 && Username.find_first_of(",") != 0 && Username.find_first_of(",") != Username.find_last_of(","))
|
||
|
|
{
|
||
|
|
{
|
||
|
|
size_t comma1Pos = Username.find(',');
|
||
|
|
if (comma1Pos == std::string::npos) {
|
||
|
|
std::cout << "输入字符串格式错误,找不到第一个逗号。" << std::endl;
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 提取第一个参数
|
||
|
|
cameraName = Username.substr(0, comma1Pos);
|
||
|
|
|
||
|
|
// 从逗号后的位置继续找到第二个逗号的位置
|
||
|
|
size_t comma2Pos = Username.find(',', comma1Pos + 1);
|
||
|
|
if (comma2Pos == std::string::npos) {
|
||
|
|
std::cout << "输入字符串格式错误,找不到第二个逗号。" << std::endl;
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 提取第二个参数
|
||
|
|
mode = Username.substr(comma1Pos + 1, comma2Pos - comma1Pos - 1);
|
||
|
|
|
||
|
|
// 从第二个逗号后的位置继续找到第三个逗号的位置
|
||
|
|
size_t comma3Pos = Username.find(',', comma2Pos + 1);
|
||
|
|
if (comma3Pos == std::string::npos) {
|
||
|
|
std::cout << "输入字符串格式错误,找不到第三个逗号。" << std::endl;
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 提取第三个参数
|
||
|
|
numOfGroup = stoi(Username.substr(comma2Pos + 1, comma3Pos - comma2Pos - 1));
|
||
|
|
|
||
|
|
// 第四个参数是最后一个逗号后的部分
|
||
|
|
filePath = Username.substr(comma3Pos + 1);
|
||
|
|
}
|
||
|
|
//filePath = Username.substr(Username.find_last_of(",")+1, Username.size() - Username.find_last_of(","));
|
||
|
|
//mode = Username.substr(0, Username.find_first_of(","));
|
||
|
|
//numOfGroup = stoi(Username.substr(Username.find_first_of(",") + 1, Username.find_last_of(",")));
|
||
|
|
findFolders(filePath, folders);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Hd_VirtualCamera_ReadFile::run()
|
||
|
|
{
|
||
|
|
qDebug() << "[INFO] " << " run start" << QString::fromStdString(Username);
|
||
|
|
try
|
||
|
|
{
|
||
|
|
m_thread->start();
|
||
|
|
while (true)
|
||
|
|
{
|
||
|
|
if (m_flag)
|
||
|
|
{
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
QThread::msleep(1);
|
||
|
|
}
|
||
|
|
m_flag = false;
|
||
|
|
}
|
||
|
|
catch (QString ev)
|
||
|
|
{
|
||
|
|
qDebug() << "[Error] " << " ev:" << ev;
|
||
|
|
}
|
||
|
|
qDebug() << "[INFO] " << __LINE__ << " run end";
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
void MyThread::run()
|
||
|
|
{
|
||
|
|
if (!cameraName.compare("camera1"))
|
||
|
|
{
|
||
|
|
vector<std::string>files;
|
||
|
|
getAllFiles(folders[fileIndex], files, "bmp");
|
||
|
|
if (fileIndex == folders.size() || fileIndex == files.size())
|
||
|
|
fileIndex = 0;
|
||
|
|
vector<std::string>names;
|
||
|
|
for (int i = 0; i < files.size(); i++)
|
||
|
|
{
|
||
|
|
if (files[i].find(cameraName) != std::string::npos)
|
||
|
|
names.push_back(files[i]);
|
||
|
|
}
|
||
|
|
for (int i = 0; i < names.size(); i++)
|
||
|
|
{
|
||
|
|
for (std::string name : names)
|
||
|
|
{
|
||
|
|
switch (i)
|
||
|
|
{
|
||
|
|
case 0:
|
||
|
|
if (name.find("SprfSpecularReflectionImage") != std::string::npos)
|
||
|
|
srcImages.push_back(imread(name, -1));
|
||
|
|
break;
|
||
|
|
case 1:
|
||
|
|
if (name.find("SprfDiffuseReflectionImage") != std::string::npos)
|
||
|
|
srcImages.push_back(imread(name, -1));
|
||
|
|
break;
|
||
|
|
case 2:
|
||
|
|
if (name.find("SprfShapeImage1") != std::string::npos)
|
||
|
|
srcImages.push_back(imread(name, -1));
|
||
|
|
break;
|
||
|
|
case 3:
|
||
|
|
if (name.find("SprfShapeImage2") != std::string::npos)
|
||
|
|
srcImages.push_back(imread(name, -1));
|
||
|
|
break;
|
||
|
|
case 4:
|
||
|
|
if (name.find("SprfGlossRatioImage") != std::string::npos)
|
||
|
|
srcImages.push_back(imread(name, -1));
|
||
|
|
break;
|
||
|
|
case 5:
|
||
|
|
if (name.find("SprfNormalImage") != std::string::npos)
|
||
|
|
srcImages.push_back(imread(name, -1));
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else if (!cameraName.compare("camera2"))
|
||
|
|
{
|
||
|
|
vector<std::string>files;
|
||
|
|
vector<std::string>files2;
|
||
|
|
if (fileIndex == folders.size() || fileIndex == files.size())
|
||
|
|
fileIndex = 0;
|
||
|
|
getAllFiles(folders[fileIndex], files, "bmp");
|
||
|
|
getAllFiles(folders[fileIndex], files2, "tiff");
|
||
|
|
for (int i = 0; i < files.size(); i++)
|
||
|
|
{
|
||
|
|
if (files[i].find(cameraName) != std::string::npos)
|
||
|
|
srcImages.push_back(imread(files[i], -1));
|
||
|
|
}
|
||
|
|
for (int i = 0; i < files2.size(); i++)
|
||
|
|
{
|
||
|
|
if (files2[i].find(cameraName) != std::string::npos)
|
||
|
|
srcImages.push_back(imread(files2[i], -1));
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
else if (!cameraName.compare("camera3P"))
|
||
|
|
{
|
||
|
|
vector<std::string>files;
|
||
|
|
getAllFiles(folders[fileIndex], files, "bmp");
|
||
|
|
if (fileIndex == folders.size() || fileIndex == files.size())
|
||
|
|
fileIndex = 0;
|
||
|
|
for (int i = 0; i < files.size(); i++)
|
||
|
|
{
|
||
|
|
if (files[i].find(cameraName) != std::string::npos)
|
||
|
|
srcImages.push_back(imread(files[i], -1));
|
||
|
|
}
|
||
|
|
if (fileIndex == files.size())
|
||
|
|
fileIndex = 0;
|
||
|
|
}
|
||
|
|
else if (!cameraName.compare("camera3N"))
|
||
|
|
{
|
||
|
|
vector<std::string>files;
|
||
|
|
getAllFiles(folders[fileIndex], files, "bmp");
|
||
|
|
if (fileIndex == folders.size() || fileIndex == files.size())
|
||
|
|
fileIndex = 0;
|
||
|
|
for (int i = 0; i < files.size(); i++)
|
||
|
|
{
|
||
|
|
if (files[i].find(cameraName) != std::string::npos)
|
||
|
|
srcImages.push_back(imread(files[i], -1));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
fileIndex++;
|
||
|
|
|
||
|
|
m_flag = true;
|
||
|
|
}
|