| 1 |
/* |
| 2 |
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. |
| 3 |
* |
| 4 |
* Author: ZhangYong <[email protected]> |
| 5 |
* |
| 6 |
* Maintainer: ZhangYong <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
#include "imagedataservice.h" |
| 22 |
#include "application.h" |
| 23 |
#include <QMetaType> |
| 24 |
#include <QDirIterator> |
| 25 |
#include <QStandardPaths> |
| 26 |
#include <QSqlDatabase> |
| 27 |
#include <QSqlError> |
| 28 |
#include <QSqlQuery> |
| 29 |
#include "utils/unionimage.h" |
| 30 |
#include "utils/baseutils.h" |
| 31 |
#include "albumgloabl.h" |
| 32 |
#include "imageengineapi.h" |
| 33 |
#include "baseutils.h" |
| 34 |
#include "imageutils.h" |
| 35 |
#include "movieservice.h" |
| 36 |
|
| 37 |
ImageDataService *ImageDataService::s_ImageDataService = nullptr; |
| 38 |
|
| 39 |
ImageDataService *ImageDataService::instance(QObject *parent) |
| 40 |
{ |
| 41 |
Q_UNUSED(parent); |
| 42 |
if (!s_ImageDataService) { |
| 43 |
s_ImageDataService = new ImageDataService(); |
| 44 |
} |
| 45 |
return s_ImageDataService; |
| 46 |
} |
| 47 |
|
| 48 |
ImageDataService::~ImageDataService() |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
bool ImageDataService::add(const QStringList &paths, bool reLoadThumbnail) |
| 53 |
{ |
| 54 |
QMutexLocker locker(&m_imgDataMutex); |
| 55 |
m_requestQueue.clear(); |
| 56 |
for (int i = 0; i < paths.size(); i++) { |
| 57 |
if (reLoadThumbnail || !m_AllImageMap.contains(paths.at(i))) { |
| 58 |
m_requestQueue.append(paths.at(i)); |
| 59 |
} |
| 60 |
} |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
bool ImageDataService::add(const QString &path) |
| 65 |
{ |
| 66 |
QMutexLocker locker(&m_imgDataMutex); |
| 67 |
if (!path.isEmpty()) { |
| 68 |
if (!m_AllImageMap.contains(path)) { |
| 69 |
m_requestQueue.append(path); |
| 70 |
} |
| 71 |
} |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
QString ImageDataService::pop() |
| 76 |
{ |
| 77 |
QMutexLocker locker(&m_imgDataMutex); |
| 78 |
if (m_requestQueue.empty()) |
| 79 |
return QString(); |
| 80 |
QString res = m_requestQueue.first(); |
| 81 |
m_requestQueue.pop_front(); |
| 82 |
return res; |
| 83 |
} |
| 84 |
|
| 85 |
bool ImageDataService::isRequestQueueEmpty() |
| 86 |
{ |
| 87 |
QMutexLocker locker(&m_imgDataMutex); |
| 88 |
return m_requestQueue.isEmpty(); |
| 89 |
} |
| 90 |
|
| 91 |
int ImageDataService::getCount() |
| 92 |
{ |
| 93 |
return m_AllImageMap.count(); |
| 94 |
} |
| 95 |
|
| 96 |
bool ImageDataService::readThumbnailByPaths(QStringList files, bool isFinishFilter, bool reLoadThumbnail) |
| 97 |
{ |
| 98 |
QStringList image_video_list; |
| 99 |
if (!isFinishFilter) { |
| 100 |
foreach (QString path, files) { |
| 101 |
QFileInfo file(path); |
| 102 |
if (file.isDir()) { |
| 103 |
QFileInfoList infos; |
| 104 |
QDirIterator dirIterator(path, |
| 105 |
QDir::Files, |
| 106 |
QDirIterator::Subdirectories); |
| 107 |
while (dirIterator.hasNext()) { |
| 108 |
dirIterator.next(); |
| 109 |
QFileInfo fi = dirIterator.fileInfo(); |
| 110 |
image_video_list << fi.absoluteFilePath(); |
| 111 |
} |
| 112 |
} else if (file.exists()) { //文件存在 |
| 113 |
image_video_list << path; |
| 114 |
} |
| 115 |
} |
| 116 |
} else { |
| 117 |
image_video_list = files; |
| 118 |
} |
| 119 |
|
| 120 |
if (image_video_list.isEmpty()) |
| 121 |
return true; |
| 122 |
|
| 123 |
bool empty = isRequestQueueEmpty(); |
| 124 |
|
| 125 |
if (empty) { |
| 126 |
ImageDataService::instance()->add(image_video_list, reLoadThumbnail); |
| 127 |
int needCoreCounts = static_cast<int>(std::thread::hardware_concurrency()); |
| 128 |
needCoreCounts = needCoreCounts / 2; |
| 129 |
if (image_video_list.size() < needCoreCounts) { |
| 130 |
needCoreCounts = image_video_list.size(); |
| 131 |
} |
| 132 |
if (needCoreCounts < 1) |
| 133 |
needCoreCounts = 1; |
| 134 |
QList<QThread *> threads; |
| 135 |
for (int i = 0; i < needCoreCounts; i++) { |
| 136 |
readThumbnailThread *thread = new readThumbnailThread; |
| 137 |
thread->start(); |
| 138 |
threads.append(thread); |
| 139 |
} |
| 140 |
// for (auto thread : threads) { |
| 141 |
// thread->wait(); |
| 142 |
// thread->deleteLater(); |
| 143 |
// } |
| 144 |
} else { |
| 145 |
ImageDataService::instance()->add(image_video_list, reLoadThumbnail); |
| 146 |
} |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
void ImageDataService::addImage(const QString &path, const QImage &image) |
| 151 |
{ |
| 152 |
QMutexLocker locker(&m_imgDataMutex); |
| 153 |
m_AllImageMap[path] = image; |
| 154 |
m_AllImageDataHashMap[path] = utils::base::filePathToThumbnailPath(path); |
| 155 |
|
| 156 |
// if (!m_AllImageMap.contains(path)) { |
| 157 |
// m_AllImageMap[path] = image; |
| 158 |
// while (m_AllImageMap.size() > 1000) { |
| 159 |
// //保证缓存占用,始终只占用1000张缩略图缓存 |
| 160 |
// QString res = m_imageKey.first(); |
| 161 |
// m_imageKey.pop_front(); |
| 162 |
// m_AllImageMap.remove(res); |
| 163 |
// } |
| 164 |
// } |
| 165 |
} |
| 166 |
|
| 167 |
void ImageDataService::addMovieDurationStr(const QString &path, const QString &durationStr) |
| 168 |
{ |
| 169 |
QMutexLocker locker(&m_imgDataMutex); |
| 170 |
m_movieDurationStrMap[path] = durationStr; |
| 171 |
} |
| 172 |
|
| 173 |
QString ImageDataService::getMovieDurationStrByPath(const QString &path) |
| 174 |
{ |
| 175 |
QMutexLocker locker(&m_imgDataMutex); |
| 176 |
return m_movieDurationStrMap.contains(path) ? m_movieDurationStrMap[path] : QString() ; |
| 177 |
} |
| 178 |
|
| 179 |
void ImageDataService::setVisualIndex(int row) |
| 180 |
{ |
| 181 |
QMutexLocker locker(&m_imgDataMutex); |
| 182 |
m_visualIndex = row; |
| 183 |
} |
| 184 |
|
| 185 |
int ImageDataService::getVisualIndex() |
| 186 |
{ |
| 187 |
QMutexLocker locker(&m_imgDataMutex); |
| 188 |
return m_visualIndex; |
| 189 |
} |
| 190 |
|
| 191 |
QImage ImageDataService::getThumnailImageByPath(const QString &path) |
| 192 |
{ |
| 193 |
QMutexLocker locker(&m_imgDataMutex); |
| 194 |
if (path.isEmpty()) { |
| 195 |
return QImage(); |
| 196 |
} |
| 197 |
return m_AllImageMap.contains(path) ? m_AllImageMap[path] : QImage(); |
| 198 |
} |
| 199 |
|
| 200 |
bool ImageDataService::imageIsLoaded(const QString &path) |
| 201 |
{ |
| 202 |
QMutexLocker locker(&m_imgDataMutex); |
| 203 |
return m_AllImageMap.contains(path); |
| 204 |
} |
| 205 |
|
| 206 |
ImageDataService::ImageDataService(QObject *parent) |
| 207 |
{ |
| 208 |
Q_UNUSED(parent); |
| 209 |
} |
| 210 |
|
| 211 |
//缩略图读取线程 |
| 212 |
readThumbnailThread::readThumbnailThread(QObject *parent): QThread(parent) |
| 213 |
{ |
| 214 |
} |
| 215 |
|
| 216 |
readThumbnailThread::~readThumbnailThread() |
| 217 |
{ |
| 218 |
} |
| 219 |
|
| 220 |
void readThumbnailThread::readThumbnail(QString path) |
| 221 |
{ |
| 222 |
if (!QFileInfo(path).exists()) { |
| 223 |
return; |
| 224 |
} |
| 225 |
using namespace UnionImage_NameSpace; |
| 226 |
QImage tImg; |
| 227 |
QString srcPath = path; |
| 228 |
QString thumbnailPath = utils::base::filePathToThumbnailPath(path); |
| 229 |
|
| 230 |
QFileInfo thumbnailFile(thumbnailPath); |
| 231 |
QString errMsg; |
| 232 |
if (thumbnailFile.exists()) { |
| 233 |
if (!loadStaticImageFromFile(thumbnailPath, tImg, errMsg, "PNG")) { |
| 234 |
qDebug() << errMsg; |
| 235 |
} |
| 236 |
|
| 237 |
if (ImageEngineApi::instance()->isItemLoadedFromDB(path)) { |
| 238 |
if (ImageEngineApi::instance()->isVideo(path)) { |
| 239 |
//获取视频信息 demo |
| 240 |
MovieInfo mi = MovieService::instance()->getMovieInfo(QUrl::fromLocalFile(path)); |
| 241 |
ImageDataService::instance()->addMovieDurationStr(path, mi.durationStr()); |
| 242 |
} |
| 243 |
} else if (utils::base::isVideo(path)) { |
| 244 |
//获取视频信息 demo |
| 245 |
MovieInfo mi = MovieService::instance()->getMovieInfo(QUrl::fromLocalFile(path)); |
| 246 |
ImageDataService::instance()->addMovieDurationStr(path, mi.durationStr()); |
| 247 |
} |
| 248 |
} else { |
| 249 |
//读图 |
| 250 |
if (ImageEngineApi::instance()->isItemLoadedFromDB(path) && ImageEngineApi::instance()->isVideo(path)) { |
| 251 |
tImg = MovieService::instance()->getMovieCover(QUrl::fromLocalFile(path)); |
| 252 |
//获取视频信息 demo |
| 253 |
MovieInfo mi = MovieService::instance()->getMovieInfo(QUrl::fromLocalFile(path)); |
| 254 |
ImageDataService::instance()->addMovieDurationStr(path, mi.durationStr()); |
| 255 |
} else if (utils::base::isVideo(path)) { |
| 256 |
tImg = MovieService::instance()->getMovieCover(QUrl::fromLocalFile(path)); |
| 257 |
|
| 258 |
//获取视频信息 demo |
| 259 |
MovieInfo mi = MovieService::instance()->getMovieInfo(QUrl::fromLocalFile(path)); |
| 260 |
ImageDataService::instance()->addMovieDurationStr(path, mi.durationStr()); |
| 261 |
} else { |
| 262 |
if (!loadStaticImageFromFile(srcPath, tImg, errMsg)) { |
| 263 |
qDebug() << errMsg; |
| 264 |
ImageDataService::instance()->addImage(path, tImg); |
| 265 |
return; |
| 266 |
} |
| 267 |
} |
| 268 |
//裁切 |
| 269 |
if (!tImg.isNull() && 0 != tImg.height() && 0 != tImg.width() && (tImg.height() / tImg.width()) < 10 && (tImg.width() / tImg.height()) < 10) { |
| 270 |
bool cache_exist = false; |
| 271 |
if (tImg.height() != 200 && tImg.width() != 200) { |
| 272 |
if (tImg.height() >= tImg.width()) { |
| 273 |
cache_exist = true; |
| 274 |
tImg = tImg.scaledToWidth(200, Qt::FastTransformation); |
| 275 |
} else if (tImg.height() <= tImg.width()) { |
| 276 |
cache_exist = true; |
| 277 |
tImg = tImg.scaledToHeight(200, Qt::FastTransformation); |
| 278 |
} |
| 279 |
} |
| 280 |
if (!cache_exist) { |
| 281 |
if ((static_cast<float>(tImg.height()) / (static_cast<float>(tImg.width()))) > 3) { |
| 282 |
tImg = tImg.scaledToWidth(200, Qt::FastTransformation); |
| 283 |
} else { |
| 284 |
tImg = tImg.scaledToHeight(200, Qt::FastTransformation); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
utils::base::mkMutiDir(thumbnailPath.mid(0, thumbnailPath.lastIndexOf('/'))); |
| 289 |
tImg.save(thumbnailPath, "PNG"); |
| 290 |
} |
| 291 |
if (!tImg.isNull()) { |
| 292 |
|
| 293 |
int width = tImg.width(); |
| 294 |
int height = tImg.height(); |
| 295 |
if (abs((width - height) * 10 / width) >= 1) { |
| 296 |
QRect rect = tImg.rect(); |
| 297 |
int x = rect.x() + width / 2; |
| 298 |
int y = rect.y() + height / 2; |
| 299 |
if (width > height) { |
| 300 |
x = x - height / 2; |
| 301 |
y = 0; |
| 302 |
tImg = tImg.copy(x, y, height, height); |
| 303 |
} else { |
| 304 |
y = y - width / 2; |
| 305 |
x = 0; |
| 306 |
tImg = tImg.copy(x, y, width, width); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
ImageDataService::instance()->addImage(path, tImg); |
| 311 |
} |
| 312 |
|
| 313 |
void readThumbnailThread::setQuit(bool quit) |
| 314 |
{ |
| 315 |
m_quit = quit; |
| 316 |
} |
| 317 |
|
| 318 |
void readThumbnailThread::run() |
| 319 |
{ |
| 320 |
while (!ImageDataService::instance()->isRequestQueueEmpty()) { |
| 321 |
if (m_quit) { |
| 322 |
break; |
| 323 |
} |
| 324 |
QString res = ImageDataService::instance()->pop(); |
| 325 |
if (!res.isEmpty()) { |
| 326 |
readThumbnail(res); |
| 327 |
} |
| 328 |
} |
| 329 |
emit ImageDataService::instance()->sigeUpdateListview(); |
| 330 |
this->deleteLater(); |
| 331 |
} |