方案一
参数描述:
参数一filename表示直接定义好文件路径+文件名(/xxx/xxx/xxx/a.jpg);
参数二pSrcFrame表示需要被保存的数据,我这里使用AVFrame类型,你们使用时换成QImage或者string类型都是可以的。
| bool SaveAsJPEG(string fileName, AVFrame * pSrcFrame) |
| { |
| if(fileName == "" || pSrcFrame == YNULL || pSrcFrame->format != AV_PIX_FMT_BGRA) |
| return false; |
| |
| |
| int32_t position = fileName.find_last_of('/'); |
| QString tmpFilePath = QString(fileName.substr(0, position).c_str()); |
| |
| if(tmpFilePath == "") |
| return false; |
| |
| |
| QDir dirPath(tmpFilePath); |
| if(!dirPath.exists(tmpFilePath)) |
| dirPath.mkpath(tmpFilePath); |
| |
| |
| QFileInfoList fileList = dirPath.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoSymLinks); |
| for(auto iter = fileList.begin(); iter != fileList.end(); iter++) |
| iter->dir().remove(iter->fileName()); |
| |
| |
| QImage image(pSrcFrame->data[0], pSrcFrame->width, pSrcFrame->height, QImage::Format_ARGB32); |
| image.save(QString::fromStdString(fileName),"JPG",100); |
| return true; |
| } |
方案二
描述:
将文件的名字保存为“日期+时间”,之后使用轮询判断文件夹内部指定格式文件,如发现文件超过指定时间,则清理。
由于windows系统下,冒号":"会被识别成盘符(C: D:),所以命名"时分秒"我使用横杠符号"-";linux系统可以正常使用冒号命名;
windows系统下保存的文件名格式为:ordinary_20230320_18-33-29.779.txt(文件内容类型__ 日期 时间.txt)
linux系统下保存的文件名格式为:ordinary_20230320_18:33:29.779.txt(文件内容类型 日期 __时间.txt)
头文件:
SaveFile.h专门写了一个类,里面有4个公共函数接口:设置目录和超时时间、需要保存的内容、清理超时文件、释放对象;
| #ifndef SAVEFILE_H |
| #define SAVEFILE_H |
| |
| #include <QDir> |
| #include <QDateTime> |
| |
| class SaveFile |
| { |
| public: |
| |
| SaveFile(QString savePath = QDir::currentPath(), QString ContentType = "ordinary"); |
| |
| public: |
| |
| void SetDirAndTime(QString dirName = "./", int timeOut = 0); |
| |
| void SaveFileToLocal(QString strContent); |
| |
| void ClearTimeOutFileToLocal(); |
| |
| void Release(); |
| |
| private: |
| QString m_strCurrentPath; |
| QString m_strDirName; |
| QString m_strContentType; |
| int m_iTimeOut; |
| }; |
| |
| #endif |
函数实现:
SaveFile.cpp
| |
| void SaveFile::SetDirAndTime(QString dirName, int timeOut) |
| { |
| m_strDirName = dirName; |
| m_iTimeOut = timeOut; |
| } |
| |
| void SaveFile::SaveFileToLocal(QString strContent) |
| { |
| if(strContent == "") |
| return; |
| |
| if(m_strDirName == "" || m_strCurrentPath == "" || m_strContentType == "") |
| return; |
| |
| QDateTime curDateTime = QDateTime::currentDateTime(); |
| #ifdef linux |
| QString curDate = curDateTime.toString("yyyyMMdd_hh:mm:ss.zzz"); |
| #elif WIN32 |
| |
| QString curDate = curDateTime.toString("yyyyMMdd_hh-mm-ss.zzz"); |
| #endif |
| |
| |
| QDir dir(m_strCurrentPath); |
| if(!dir.exists(m_strDirName)) |
| dir.mkdir(m_strDirName); |
| dir.cd(m_strDirName); |
| |
| |
| QString filePath = QString("%1/%2_%3.txt").arg(m_strDirName).arg(m_strContentType).arg(curDate); |
| QFile sFile(filePath); |
| |
| if(!sFile.open(QIODevice::Truncate | QIODevice::WriteOnly|QIODevice::Text)) |
| printf("Open jsonfile failed!\n"); |
| |
| sFile.write(strContent.toStdString().c_str()); |
| sFile.close(); |
| } |
| |
| void SaveFile::ClearTimeOutFileToLocal() |
| { |
| if(m_iTimeOut <= 0 || m_strCurrentPath == "" || m_strDirName == "") |
| return; |
| |
| QDateTime curDateTime = QDateTime::currentDateTime(); |
| QDir dir(m_strCurrentPath); |
| if(!dir.exists(m_strDirName)) |
| dir.mkdir(m_strDirName); |
| dir.cd(m_strDirName); |
| |
| |
| |
| QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoSymLinks); |
| for(auto iter = fileList.begin(); iter != fileList.end(); iter++) |
| { |
| QString tmpFileName = iter->fileName(); |
| |
| if(tmpFileName.contains(".txt") && tmpFileName.contains(m_strContentType)) |
| { |
| |
| QString tmpRemove = QString("%1_").arg(m_strContentType); |
| QString slitfileName = ((iter->fileName().remove(".txt")).remove(tmpRemove)); |
| #ifdef linux |
| QDateTime oldTime(QDateTime::fromString(slitfileName,"yyyyMMdd_hh:mm:ss.zzz")); |
| #elif WIN32 |
| QDateTime oldTime(QDateTime::fromString(slitfileName,"yyyyMMdd_hh-mm-ss.zzz")); |
| #endif |
| |
| slitfileName = (tmpRemove + slitfileName) + ".txt"; |
| |
| int timeDiff = curDateTime.toTime_t() - oldTime.toTime_t(); |
| if(timeDiff > m_iTimeOut) |
| (*iter).dir().remove(slitfileName); |
| } |
| } |
| } |
| |
| void SaveFile::Release() |
| { |
| delete this; |
| } |
具体使用
| #define JSON_DIRNAME "JsonMessage" |
| #define TIMOUT (60 * 5) |
| |
| SaveFile * m_pSaveJsonFile = new SaveFile(); |
| m_pSaveJsonFile->SetDirAndTime(JSON_DIRNAME,TIMOUT); |
| if(m_pSaveJsonFile) |
| { |
| m_pSaveJsonFile->Release(); |
| m_pSaveJsonFile = nullptr; |
| } |
| |
| m_pSaveJsonFile->SaveFileToLocal(QString(agentJson.toStyledString().c_str())); |
| m_pSaveJsonFile->ClearTimeOutFileToLocal(); |
希望能写明白,感谢包容!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库