123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #include "stdafx.h"
- #include <io.h>
- #include <time.h>
- #include <stdio.h>
- #include "imstorage_manager.h"
- //namespace graft_gcv
- //{
- CRITICAL_SECTION g_cs;
- void getFiles( string path, vector<string>& files, time_t clean_time )
- {
- //文件句柄
- long hFile = 0;
- //文件信息
- struct _finddata_t fileinfo;
- string p;
- if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
- {
- do
- {
- //如果是目录,迭代之
- //如果不是,加入列表
- if((fileinfo.attrib & _A_SUBDIR))
- {
- if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
- getFiles( p.assign(path).append("\\").append(fileinfo.name), files ,clean_time);
- }
- else
- {
- if(clean_time>fileinfo.time_create){
- files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
- }
- }
- }while(_findnext(hFile, &fileinfo) == 0);
- _findclose(hFile);
- }
- }
- bool g_thread_run=false;
- bool g_thread_run_saver=true;
- int WINAPI TheadFuncClearn(LPVOID lpParam)
- {
- ThreadParamClean* threadParam = (ThreadParamClean *) lpParam;
- string folder =threadParam->folder;
- int store_days = threadParam->store_days;
- bool* state = threadParam->state;
- int cnt=0;
- __time64_t dtime = store_days*86400;
- //cout<<"folder: "<<folder<<endl;
- time_t scan_time = time(NULL);
- while(folder.size()>0)
- {
- if(!(*state)){break;}
- if(time(NULL)-scan_time < 3600000){
- Sleep(500);
- continue;
- }
- vector<string>filenames;
- time_t clean_time = time(NULL) - dtime;
- getFiles(folder,filenames,clean_time);
- scan_time = time(NULL);
- for(size_t i=0;i<filenames.size();++i){
- //delete the file
- remove(filenames[i].c_str());
- }
-
- }
- return 0;
- }
- int WINAPI TheadFuncSave(LPVOID lpParam)
- {
- ThreadParamSave* threadParam = (ThreadParamSave *) lpParam;
- int nIndex = threadParam->nIndex;
- queue<ImgParam>* imgQ =threadParam->imgQ;
- while(true)
- {
- if(!g_thread_run_saver){break;}
- if(imgQ->size()==0){
- Sleep(50);
- continue;
- }
- try{
- EnterCriticalSection(&g_cs);
- ImgParam& im_info = imgQ->front();
- cv::imwrite(im_info.img_name,im_info.image);
- //cout<<im_info.img_name<<"\t"<<imgQ->size()<<endl;
- imgQ->pop();
- }
- catch(...){
- //int tes = 0;
- }
- LeaveCriticalSection(&g_cs);
- }
- return 0;
- }
-
- CImStoreManager::CImStoreManager()
- :m_storeDays(7)
- ,m_storeDir(""),
- m_workHandle(0),
- m_workHandleSave(0)
- {
- InitializeCriticalSection(&g_cs);
- ThreadParamSave paramSave;
- paramSave.nIndex = 0;
- //paramSave.state = &g_thread_run;
- paramSave.imgQ = &m_images;
- m_workHandleSave = CreateThread(
- NULL,
- 0,
- (LPTHREAD_START_ROUTINE)TheadFuncSave,
- (LPVOID)¶mSave,
- NULL,
- NULL);
- Sleep(500);
- }
- CImStoreManager::~CImStoreManager(){
- g_thread_run=false;
- g_thread_run_saver=false;
- HANDLE handles[2];
- handles[0]=m_workHandle;
- handles[1]=m_workHandleSave;
- WaitForMultipleObjects(2,handles,TRUE,500);
- DeleteCriticalSection(&g_cs);
- }
- void CImStoreManager::restart_start_worker()
- {
- ThreadParamClean param_clean;
- param_clean.folder = m_storeDir;
- param_clean.store_days = m_storeDays;
- param_clean.state = &g_thread_run;
- g_thread_run=false;
- if(m_workHandle){
- WaitForSingleObject(m_workHandle,INFINITE);
- CloseHandle(m_workHandle);
- }
- g_thread_run=true;
- m_workHandle = CreateThread(
- NULL,
- 0,
- (LPTHREAD_START_ROUTINE)TheadFuncClearn,
- (LPVOID)¶m_clean,
- NULL,
- NULL);
- Sleep(500);
- }
- int CImStoreManager::setStoreDir(string& folder)
- {
- int stat = access(folder.c_str(),0);
- if (stat==0){
- m_storeDir = folder;
- g_thread_run=false;
- restart_start_worker();
- }
- else{
- return 1;
- }
- return 0;
- }
- void CImStoreManager::getStoreDir(string& folder)
- {
- folder = m_storeDir;
- }
- void CImStoreManager::setStoreDays(int days)
- {
- if(days>0){
- m_storeDays = days;
- g_thread_run=false;
- restart_start_worker();
- }
- }
- bool CImStoreManager::is_valid_folder()
- {
- int stat = access(m_storeDir.c_str(),0);
- if(stat==0){return true;}
- else{return false;}
- }
- int CImStoreManager::saveImage(Mat&img,string name_id)
- {
- if(!is_valid_folder()){return 1;}
- if(img.empty()){return 1;}
- string tar_file = m_storeDir+"/"+name_id+".jpg";
- ImgParam imgp;
- imgp.img_name=tar_file;
- imgp.image = img.clone();
- EnterCriticalSection(&g_cs);
- m_images.push(imgp);
- //cout<<"=========="<<imgp.img_name<<endl;
- LeaveCriticalSection(&g_cs);
-
- return 0;
- }
- //};
|