diff --git a/libimageviewer/service/aimodelservice.cpp b/libimageviewer/service/aimodelservice.cpp index d6330093..8761891f 100644 --- a/libimageviewer/service/aimodelservice.cpp +++ b/libimageviewer/service/aimodelservice.cpp @@ -48,16 +48,21 @@ AIModelServiceData::AIModelServiceData(AIModelService *q) supportNameToModel = initDBusModelList(); qInfo() << qPrintable("Support image enhance models:") << supportNameToModel; - if (!enhanceTemp.isValid()) { - qWarning() << qPrintable("Create enhance temp dir failed") << enhanceTemp.errorString(); - } else { - qInfo() << qPrintable("Enhance temp dir:") << enhanceTemp.path(); - } + // QTemporaryDir::isValid() 会创建临时文件路径,在没有模型数据时不进行路径判断 + if (!supportNameToModel.isEmpty()) { + enhanceTemp.reset(new QTemporaryDir); + if (!enhanceTemp->isValid()) { + qWarning() << qPrintable("Create enhance temp dir failed") << enhanceTemp->errorString(); + } else { + qInfo() << qPrintable("Enhance temp dir:") << enhanceTemp->path(); + } - if (!convertTemp.isValid()) { - qWarning() << qPrintable("Create convert temp dir failed") << convertTemp.errorString(); - } else { - qInfo() << qPrintable("Convert temp dir:") << convertTemp.path(); + convertTemp.reset(new QTemporaryDir); + if (!convertTemp->isValid()) { + qWarning() << qPrintable("Create convert temp dir failed") << convertTemp->errorString(); + } else { + qInfo() << qPrintable("Convert temp dir:") << convertTemp->path(); + } } } @@ -82,7 +87,7 @@ QList> AIModelServiceData::initDBusModelList() if (modelList.isEmpty()) { auto error = interface.lastError(); - qWarning() << QString("[Enhance DBus] Get model list failed, %1: %2").arg(error.name()).arg(error.message()); + qInfo() << QString("[Enhance DBus] No AI models on device? Get model list failed, %1: %2").arg(error.name()).arg(error.message()); return {}; } @@ -237,7 +242,7 @@ AIModelService *AIModelService::instance() */ bool AIModelService::isValid() const { - return !dptr->supportNameToModel.isEmpty(); + return dptr && !dptr->supportNameToModel.isEmpty(); } /** @@ -337,7 +342,10 @@ QString AIModelService::imageProcessing(const QString &filePath, int modelID, co // 生命周期交由子线程维护 QImage caputureImage = image.copy(); - dptr->lastOutput = dptr->enhanceTemp.filePath(QString("%1.png").arg(dptr->enhanceCache.size())); + if (!dptr->enhanceTemp) { + return {}; + } + dptr->lastOutput = dptr->enhanceTemp->filePath(QString("%1.png").arg(dptr->enhanceCache.size())); QString model = dptr->mapModelInfo.value(modelID)->model; EnhancePtr ptr(new EnhanceInfo(sourceFile, dptr->lastOutput, model)); @@ -449,7 +457,7 @@ void AIModelService::cancelProcess(const QString &output) */ bool AIModelService::isTemporaryFile(const QString &filePath) { - return dptr->enhanceCache.contains(filePath); + return isValid() && dptr->enhanceCache.contains(filePath); } /** @@ -457,7 +465,7 @@ bool AIModelService::isTemporaryFile(const QString &filePath) */ QString AIModelService::sourceFilePath(const QString &filePath) { - if (dptr->enhanceCache.contains(filePath)) { + if (isValid() && dptr->enhanceCache.contains(filePath)) { auto ptr = dptr->enhanceCache.value(filePath); return ptr->source; } @@ -719,8 +727,12 @@ QString AIModelService::checkConvertFile(const QString &filePath, const QImage & return {}; } + if (!dptr->convertTemp) { + return {}; + } + QString cvtFile; - cvtFile = dptr->convertTemp.filePath(QString("%1_%2.png").arg(dptr->convertCache.size()).arg(QFileInfo(filePath).fileName())); + cvtFile = dptr->convertTemp->filePath(QString("%1_%2.png").arg(dptr->convertCache.size()).arg(QFileInfo(filePath).fileName())); _locker.unlock(); if (!image.save(cvtFile, "PNG")) { diff --git a/libimageviewer/service/aimodelservice_p.h b/libimageviewer/service/aimodelservice_p.h index ba06451b..3ba4eb77 100644 --- a/libimageviewer/service/aimodelservice_p.h +++ b/libimageviewer/service/aimodelservice_p.h @@ -89,11 +89,11 @@ class AIModelServiceData QList> supportNameToModel; // 缓存的支持模型列表<模型ID,名称> QString lastOutput; // 最近的图像增强输出文件 - QTemporaryDir enhanceTemp; // 图像增强文件临时目录 + QScopedPointer enhanceTemp; // 图像增强文件临时目录 QHash enhanceCache; // 图像增强缓存信息(仅主线程访问) QMutex cacheMutex; - QTemporaryDir convertTemp; // 图像类型转换文件临时目录 + QScopedPointer convertTemp; // 图像类型转换文件临时目录 QHash convertCache; // 缓存的信息,可能多个线程访问 QFutureWatcher enhanceWatcher;