You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Cigarette/CigaretteSingle/common.cpp

351 lines
8.5 KiB
C++

#include "common.h"
#include <string>
#include "stdarg.h"
#include <io.h>
#include <Windows.h>
#include <shlobj.h>
std::vector<std::string> string_split(std::string str, std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str += pattern;
int size = str.size();
for (int i = 0; i<size; i++)
{
pos = str.find(pattern, i);
if (pos<size)
{
std::string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;
}
std::string format(const char *pszFmt, ...)
{
std::string str;
va_list args;
va_start(args, pszFmt);
{
int nLength = _vscprintf(pszFmt, args);
nLength += 1;
std::vector<char> 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<int>(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<cv::Point3i> 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;i<points.size();i++){
if(points[i].x<minx)minx = points[i].x;
if(points[i].y<miny)miny = points[i].y;
if(points[i].z<minz)minz = points[i].z;
if(points[i].x>maxx)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<><31>%2ʱ%3<><33>%4<><34>")).arg(days).arg(hours).arg(minutes).arg(second);
}
void getFiles(std::string path, std::vector<std::string>& files)
{
//<2F>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
intptr_t hFile = 0;
//<2F>ļ<EFBFBD><C4BC><EFBFBD>Ϣ
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;
}
//<2F><>string s<><73>λ<EFBFBD><CEBB>pos<6F><73>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD><D2A3>ҵ<EFBFBD><D2B5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>x<EFBFBD><78>λ<EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
//<2F>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>λ<EFBFBD>ã<EFBFBD><C3A3>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-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;
}
//˫б<CBAB><D0B1>ת<EFBFBD><D7AA>б<EFBFBD><D0B1>
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;//<2F><><>ڶԻ<DAB6><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD>?<3F><>Ϣ
bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;//<2F><><EFBFBD>½<EFBFBD>?<3F><><EFBFBD>а<EFBFBD>ť
bi.lpfn = BrowseCallbackProc;
bi.iImage = 0;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);//<2F><>?ѡ<><D1A1><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD>
if (pidl == NULL)
{
std::cout << "û<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>Ŀ¼" << 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 <Commdlg.h>
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));
// <20><EFBFBD><E1B9B9><EFBFBD><EFBFBD>С
ofn.lStructSize = sizeof(ofn);
// ӵ<><D3B5><EFBFBD>Ŵ<EFBFBD><C5B4>ھ<EFBFBD><DABE><EFBFBD>
ofn.hwndOwner = NULL;
// <20><><EFBFBD>շ<EFBFBD><D5B7>ص<EFBFBD><D8B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ҪΪNULL
ofn.lpstrFile = szOpenFileNames;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ofn.nMaxFile = sizeof(szOpenFileNames);
// _T<5F><54><EFBFBD>滻ΪTEXT<58><54>ʹ<EFBFBD><CAB9>_T<5F><54>Ҫ<EFBFBD><D2AA>tchar.h
ofn.lpstrFile[0] = _T('\0');
// <20><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD>
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");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ofn.nFilterIndex = 1;
// <20><><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD>
ofn.lpstrTitle = _T("<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD>ͼƬ");
// <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڡ<EFBFBD><DAA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>ѡ<EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>
// <20>ٷ<EFBFBD><D9B7>ĵ<EFBFBD><C4B5><EFBFBD>https://docs.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-openfilenamea
ofn.Flags = OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY | OFN_EXPLORER;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3>򲻲<EFBFBD><F2B2BBB2><EFBFBD>
if (!::GetOpenFileName(&ofn)) {
return "";
}
// <20>ѵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ǰ<EFBFBD>ĸ<EFBFBD><C4B8>Ƶ<EFBFBD>szPath,<2C><>:
// <20><><EFBFBD><EFBFBD>ֻѡ<D6BB><D1A1>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD>,<2C>͸<EFBFBD><CDB8>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>'/'
// <20><><EFBFBD><EFBFBD>ѡ<EFBFBD>˶<EFBFBD><CBB6><EFBFBD><EFBFBD>ļ<EFBFBD>,<2C>͸<EFBFBD><CDB8>Ƶ<EFBFBD><C6B5><EFBFBD>һ<EFBFBD><D2BB>NULL<4C>ַ<EFBFBD>
lstrcpyn(szPath, szOpenFileNames, ofn.nFileOffset);
// <20><>ֻѡ<D6BB><D1A1>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD>ʱ,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>NULL<4C>ַ<EFBFBD><D6B7>DZ<EFBFBD><C7B1><EFBFBD><EFBFBD><EFBFBD>.
// <20><><EFBFBD><EFBFBD><EFB2BB><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ѡ<EFBFBD><D1A1>һ<EFBFBD><D2BB><EFBFBD>Ͷ<EFBFBD><CDB6><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
szPath[ofn.nFileOffset] = '\0';
nLen = lstrlen(szPath);
// <20><><EFBFBD><EFBFBD>ѡ<EFBFBD>˶<EFBFBD><CBB6><EFBFBD><EFBFBD>ļ<EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>'//'
if (szPath[nLen - 1] != '\\') {
lstrcat(szPath, _T("\\"));
}
// <20><>ָ<EFBFBD><D6B8><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD>
p = szOpenFileNames + ofn.nFileOffset;
// <20><>szFileName<6D><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ZeroMemory(szFileName, sizeof(szFileName));
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><C6B4><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
std::string str = "";
while (*p) {
// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
std::string fileName = LPWSTR2LPSTR(p);
// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>·<EFBFBD><C2B7>
std::string filePath = LPWSTR2LPSTR(szPath);
// ƴ<><C6B4><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
std::string completePath = filePath + fileName;
// ƴ<><C6B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
str += completePath + "***";
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD>
p += lstrlen(p) + 1;
}
return str;
// <20><>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;
}