概述
CString CVisionSetting::GetDirectory()
{
char szPath[255];
memset(szPath, 0, 255);
GetDirectory(szPath);
//::GetCurrentDirectory(255, szPath);
return CString(szPath);
}
bool CVisionSetting::IsDirectoryExists(CString const& path,BOOL bCreate)
{
CFileFind m_FileFind;;
if(!m_FileFind.FindFile(path)) //路径不存在则创建该路径
{
if(bCreate) CreateDirectory(path,NULL);
return false;
}
return true;
}
void CVisionSetting::GetDirectory(LPCSTR pExcutePath)
{
TCHAR sFilename[_MAX_PATH];
TCHAR sDrive[_MAX_DRIVE];
TCHAR sDir[_MAX_DIR];
TCHAR sFname[_MAX_FNAME];
TCHAR sExt[_MAX_EXT];
//GetModuleFileName(AfxGetInstanceHandle(), sFilename, _MAX_PATH);
GetModuleFileName(GetModuleHandle(NULL), sFilename, _MAX_PATH);
//TRACE(_T("Application handle is 0x%0Xn",AfxGetInstanceHandle()));
_splitpath_s(sFilename, sDrive, sDir, sFname, sExt);
CString csVal(CString(sDrive) + CString(sDir));
if (csVal.Right(1) != _T('\'))
csVal += _T("\");
strcpy((LPSTR)pExcutePath,csVal.GetBuffer());
csVal.ReleaseBuffer();
}
bool CVisionSetting::SaveImg(HalconCpp::HObject& ObjImg, bool bIsOK,bool bMultiMode)
{
if(ObjImg.CountObj() < 1)
return false;
// 2天一删除
int tYear = 1;
int tMonth = 1;
int tDay = 1;
SYSTEMTIME st1;
GetLocalTime(&st1);
if(st1.wDay<=2 && st1.wMonth==1) //当前1月份
{
tYear = st1.wYear - 1;
tMonth = 12;
tDay = 28 + st1.wDay;
}
else if(st1.wDay<=2) //一个月的前3天
{
tYear = st1.wYear;
tMonth = st1.wMonth-1;
if(4 == tMonth || 6 == tMonth || 9 == tMonth || 11 == tMonth ) //前一月份4 6 9 11
{
tDay = 28 + st1.wDay;
}
else if(2 == tMonth)
{
if(0 == st1.wYear % 4 || 0 == st1.wYear % 100 ) //闰年
{
tDay = 27 + st1.wDay;
}
else
{
tDay = 26 + st1.wDay;
}
}
else
{
tDay = 29 + st1.wDay;
}
}
else
{
tYear = st1.wYear;
tMonth = st1.wMonth;
tDay = st1.wDay-2;
}
try{
CString str;
bool bFlag = false;
DWORD dw;
// 删除OK目录
str.Format("%sImageFile\%04d%02d%02d\OK\*.*",GetDirectory(),tYear,tMonth,tDay);
WIN32_FIND_DATAA tFindData;
HANDLE hFirstFile = FindFirstFile(str,&tFindData);
while(FindNextFile(hFirstFile,&tFindData))
{
str.Format("%sImageFile\%04d%02d%02d\OK\%s",GetDirectory(),tYear,tMonth,tDay,tFindData.cFileName);
::DeleteFileA(str);
}
bFlag = FindClose(hFirstFile);
if(!bFlag)
{
dw = GetLastError();
}
str.Format("%sImageFile\%04d%02d%02d\OK",GetDirectory(),tYear,tMonth,tDay);
if(IsDirectoryExists(str))
{
bFlag = RemoveDirectory(str);
if(!bFlag)
{
dw = GetLastError();
}
}
// 删除NG目录
str.Format("%sImageFile\%04d%02d%02d\NG\*.*",GetDirectory(),tYear,tMonth,tDay);
hFirstFile = FindFirstFile(str,&tFindData);
while(FindNextFile(hFirstFile,&tFindData))
{
str.Format("%sImageFile\%04d%02d%02d\NG\%s",GetDirectory(),tYear,tMonth,tDay,tFindData.cFileName);
::DeleteFileA(str);
}
bFlag = FindClose(hFirstFile);
if(!bFlag)
{
dw = GetLastError();
}
str.Format("%sImageFile\%04d%02d%02d\NG",GetDirectory(),tYear,tMonth,tDay);
if(IsDirectoryExists(str))
RemoveDirectory(str);
// 删除目录
str.Format("%sImageFile\%04d%02d%02d\",GetDirectory(),tYear,tMonth,tDay);
//if(IsDirectoryExists(str))
RemoveDirectory(str);
// 创建当天文件夹
CString CurDirectory;
CurDirectory.Format("%sImageFile",GetDirectory());
IsDirectoryExists(CurDirectory,TRUE);
CurDirectory.Format("%sImageFile\%04d%02d%02d",GetDirectory(),st1.wYear,st1.wMonth,st1.wDay);
bFlag = IsDirectoryExists(CurDirectory,TRUE);
if(0==bIsOK)
{
CurDirectory += "\NG";
}
else
{
CurDirectory += "\OK";
}
bFlag = IsDirectoryExists(CurDirectory,TRUE);
str.Format("%s\%2d-%02d-%02d",CurDirectory,st1.wHour,st1.wMinute,st1.wSecond);
str += ".jpg";
HalconCpp::WriteImage(ObjImg,"jpeg",0,HTuple(str));
}
catch(HalconCpp::HException&e)
{
CString str(e.ErrorMessage().Text());
//AfxMessageBox(str);
return false;
}
catch(...)
{
return false;
}
return true;
}
最后
以上就是彩色小白菜为你收集整理的保存图片,自动删除图片及目录的全部内容,希望文章能够帮你解决保存图片,自动删除图片及目录所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复