-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageProcessor.cpp
More file actions
79 lines (62 loc) · 2.27 KB
/
Copy pathImageProcessor.cpp
File metadata and controls
79 lines (62 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "ImageProcessor.hpp"
#include "Logger.hpp"
#include <QBuffer>
#include <QImage>
ImageProcessor *ImageProcessor::get() {
static ImageProcessor instance;
return &instance;
}
ImageProcessor::ImageProcessor() {}
QByteArray ImageProcessor::compressImage(const QString path, int maxBytes) {
QImage image(path);
if (image.isNull()) {
Logger::Tag("ImageProcessor")
.eFmt("Failed to load image: %s", path.toStdString().c_str());
return {};
}
const QSize originalSize = image.size();
// 统一转换为 ARGB32 格式,确保 PNG 编码兼容性
if (image.format() != QImage::Format_ARGB32 &&
image.format() != QImage::Format_ARGB32_Premultiplied) {
image = image.convertToFormat(QImage::Format_ARGB32);
}
// 阶梯降分辨率:1920 → 1600 → 1280 → 1024 → 800
constexpr int dimens[] = {1920, 1600, 1280, 1024, 800};
constexpr int maxCompression = 100; // PNG 最高压缩级别
QByteArray outputData;
QSize resultSize = originalSize;
int usedDimension = 0;
for (int dim : dimens) {
QImage scaled = (image.width() > dim || image.height() > dim)
? image.scaled(dim, dim, Qt::KeepAspectRatio,
Qt::SmoothTransformation)
: image;
QBuffer buffer(&outputData);
if (!buffer.open(QIODevice::WriteOnly)) {
Logger::Tag("ImageProcessor").e("Failed to open buffer for writing");
return {};
}
scaled.save(&buffer, "PNG", maxCompression);
buffer.close();
resultSize = scaled.size();
usedDimension = dim;
if (outputData.size() <= maxBytes) {
break;
}
outputData.clear();
// 已经是最后一档了,不再降,接受当前结果
if (dim == dimens[sizeof(dimens) / sizeof(dimens[0]) - 1]) {
// 重新编码一次保留数据
QBuffer lastBuffer(&outputData);
lastBuffer.open(QIODevice::WriteOnly);
scaled.save(&lastBuffer, "PNG", 100);
lastBuffer.close();
break;
}
}
Logger::Tag("ImageProcessor")
.dFmt("Compressed: %dx%d -> %dx%d, %d bytes (target=%d, maxDim=%d)",
originalSize.width(), originalSize.height(), resultSize.width(),
resultSize.height(), outputData.size(), maxBytes, usedDimension);
return outputData;
}