保存图片,自动删除图片及目录

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%0X\n",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;	
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值