#include "common.h" #include #include "stdarg.h" #include #include #include std::vector string_split(std::string str, std::string pattern) { std::string::size_type pos; std::vector result; str += pattern; int size = str.size(); for (int i = 0; i chars(nLength); _vsnprintf(chars.data(), nLength, pszFmt, args); str.assign(chars.data()); } va_end(args); return str; } QImage cvMatToQImage(const cv::Mat& mat) { const unsigned char* data = mat.data; int width = mat.cols; int height = mat.rows; int bytesPerLine = static_cast(mat.step); switch (mat.type()) { // 8bit, ARGB case CV_8UC4: { QImage image(data, width, height, bytesPerLine, QImage::Format_ARGB32); return image; } // 8bit, BGR case CV_8UC3: { QImage image(data, width, height, bytesPerLine, QImage::Format_RGB888); //swap blue and red channel return image.rgbSwapped(); } // 8bit, Grayshale case CV_8UC1: { QImage image(data, width, height, bytesPerLine, QImage::Format_Grayscale8); return image; } default: { return QImage(); } } } void getMinMax(std::vector points, cv::Point3i &min, cv::Point3i &max) { int minx=0,miny=0,minz=0; int maxx=0,maxy=0,maxz=0; for(int i=0;imaxx)maxx = points[i].x; if(points[i].y>maxy)maxy = points[i].y; if(points[i].z>maxz)maxz = points[i].z; } min.x = minx; min.y = miny; min.z = minz; max.x = maxx; max.y = maxy; max.z = maxz; } QString SecondToQStrTime(qint64 time_sec) { int days = (time_sec) / (24 * 3600); int hours = (time_sec) % (24 * 3600) / 3600; int minutes = (time_sec) % 3600 / 60; int second = (time_sec) % 60; return QString(QStringLiteral("%1天%2时%3分%4秒")).arg(days).arg(hours).arg(minutes).arg(second); } void getFiles(std::string path, std::vector& files) { //文件句柄 intptr_t hFile = 0; //文件信息 struct _finddata_t fileinfo; std::string p; if ((hFile = _findfirst(p.assign(path).c_str(), &fileinfo)) != -1) { do { if (!(fileinfo.attrib & _A_SUBDIR)) { files.push_back(fileinfo.name); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } } std::string WstringToString(std::wstring wstr) { int nLen = wcslen(wstr.c_str()); std::string str; str.resize(nLen * 2, ' '); int nResult = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstr.c_str(), -1, (LPSTR)str.c_str(), nLen * 2, NULL, NULL); return str; } /* TCHAR*转char* */ char* LPWSTR2LPSTR(LPWSTR lpwszStrIn) { LPSTR pszOut = NULL; if (lpwszStrIn != NULL) { int nInputStrLen = wcslen(lpwszStrIn); int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2; pszOut = new char[nOutputStrLen]; if (pszOut != NULL) { memset(pszOut, 0x00, nOutputStrLen); WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0); } } return pszOut; } //从string s的位置pos开始,向后找,找到第一个等于x的位置返回其位置 //找到,返回找到的位置,找不到返回-1 int __find(const std::string s, const int start, const char x) { if (start >= s.length())return -1; for (int i = start; i < s.length(); ++i) { if (s[i] == x) return i; } return -1; } //双斜杠转单斜杠 void pathConvert_Double2Single(std::string& s) { int start = 0; while (start < s.length()) { int pos = s.find('\\', start); if (pos == -1)break; s.replace(pos, 1,"/"); start = pos + 1; } } int CALLBACK BrowseCallbackProc( HWND hwnd, UINT uMsg, LPARAM /*lParam*/, LPARAM lpData) { LPWSTR buf[1000]; GetCurrentDirectory(1000, (LPWSTR)buf); if (uMsg == BFFM_INITIALIZED) { SendMessage(hwnd, BFFM_SETSELECTION, (WPARAM)TRUE, (LPARAM)buf); } return 0; } std::string SelectDirBRI() { BROWSEINFO bi; bi.hwndOwner = NULL; bi.pidlRoot = CSIDL_DESKTOP; bi.pszDisplayName = NULL; bi.lpszTitle = NULL;//显?位于对话框左上部的提?信息 bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;//有新建?件夹按钮 bi.lpfn = BrowseCallbackProc; bi.iImage = 0; LPITEMIDLIST pidl = SHBrowseForFolder(&bi);//调?选择对话框 if (pidl == NULL) { std::cout << "没有选择目录" << std::endl; return ""; } TCHAR strFolder[MAX_PATH]; SHGetPathFromIDList(pidl, strFolder); std::string sFolder = ""; sFolder = WstringToString(strFolder); int pos = sFolder.find('\0'); if (pos) { sFolder = sFolder.substr(0, pos); } return sFolder; } std::string SelectFileOFN() { OPENFILENAME ofn;//#include TCHAR szOpenFileNames[80 * MAX_PATH] = { 0 }; TCHAR szPath[MAX_PATH]; TCHAR szFileName[80 * MAX_PATH]; int nLen = 0; TCHAR* p = NULL; ZeroMemory(&ofn, sizeof(ofn)); // 结构体大小 ofn.lStructSize = sizeof(ofn); // 拥有着窗口句柄 ofn.hwndOwner = NULL; // 接收返回的文件名,注意第一个字符需要为NULL ofn.lpstrFile = szOpenFileNames; // 缓冲区长度 ofn.nMaxFile = sizeof(szOpenFileNames); // _T可替换为TEXT,使用_T需要引tchar.h ofn.lpstrFile[0] = _T('\0'); // 设置过滤 ofn.lpstrFilter = _T("All\0*.*\0.mp4\0*.mp4\0.avi\0*.avi\0.mkv\0*.mkv\0.rmvb\0*.rmvb\0.f4v\0*.f4v\0.flv\0*.flv\0.m4v\0*.m4v\0.mpg\0*.mpg\0\0"); // 过滤器索引 ofn.nFilterIndex = 1; // 窗口标题 ofn.lpstrTitle = _T("请选择图片"); // 文件必须存在、允许多选、隐藏只读选项、对话框使用资源管理器风格的用户界面 // 官方文档:https://docs.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-openfilenamea ofn.Flags = OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY | OFN_EXPLORER; // 如果打开文件失败,则不操作 if (!::GetOpenFileName(&ofn)) { return ""; } // 把第一个文件名前的复制到szPath,即: // 如果只选了一个文件,就复制到最后一个'/' // 如果选了多个文件,就复制到第一个NULL字符 lstrcpyn(szPath, szOpenFileNames, ofn.nFileOffset); // 当只选了一个文件时,下面这个NULL字符是必需的. // 这里不区别对待选了一个和多个文件的情况 szPath[ofn.nFileOffset] = '\0'; nLen = lstrlen(szPath); // 如果选了多个文件,则必须加上'//' if (szPath[nLen - 1] != '\\') { lstrcat(szPath, _T("\\")); } // 把指针移到第一个文件 p = szOpenFileNames + ofn.nFileOffset; // 对szFileName进行清零 ZeroMemory(szFileName, sizeof(szFileName)); // 定义字符串,用于拼接所选的所有文件的完整路径 std::string str = ""; while (*p) { // 读取文件名 std::string fileName = LPWSTR2LPSTR(p); // 读取文件所在文件夹路径 std::string filePath = LPWSTR2LPSTR(szPath); // 拼接文件完整路径 std::string completePath = filePath + fileName; // 拼接字符串 str += completePath + "***"; //移至下一个文件 p += lstrlen(p) + 1; } return str; // 将string转为char* //char* strc = new char[strlen(str.c_str()) + 1]; //const char* cc = str.c_str(); //strcpy_s(strc, str.size() + 1, cc); } //https://learn.microsoft.com/en-us/windows/win32/shell/common-file-dialog?redirectedfrom=MSDN std::string SelectDirIFD() { std::string folderpath = ""; // CoCreate the File Open Dialog object. IFileDialog* pfd = NULL; HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd)); if (SUCCEEDED(hr)) { // Set the options on the dialog. DWORD dwFlags; // Before setting, always get the options first in order // not to override existing options. hr = pfd->GetOptions(&dwFlags); if (SUCCEEDED(hr)) { // In this case, get shell items only for file system items. hr = pfd->SetOptions(dwFlags | FOS_PICKFOLDERS); if (SUCCEEDED(hr)) { // Show the dialog hr = pfd->Show(NULL); if (SUCCEEDED(hr)) { // Obtain the result once the user clicks // the 'Open' button. // The result is an IShellItem object. IShellItem* psiResult; hr = pfd->GetResult(&psiResult); if (SUCCEEDED(hr)) { // We are just going to print out the // name of the file for sample sake. PWSTR pszFilePath = NULL; hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH,&pszFilePath); if (SUCCEEDED(hr)) { folderpath = LPWSTR2LPSTR(pszFilePath); CoTaskMemFree(pszFilePath); } psiResult->Release(); } } } } pfd->Release(); } return folderpath; }