-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathCaptureFullPage.cpp
More file actions
93 lines (83 loc) · 2.6 KB
/
CaptureFullPage.cpp
File metadata and controls
93 lines (83 loc) · 2.6 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Author: Kang Lin <kl222@126.com>
#include <QTimer>
#include <QPainter>
#include <QLoggingCategory>
#include "CaptureFullPage.h"
static Q_LOGGING_CATEGORY(log, "WebBrowser.Capture.Page")
CCaptureFullPage::CCaptureFullPage(QObject *parent)
: QObject{parent}
{}
void CCaptureFullPage::Start(QWebEngineView *view, QUrl url, QString szFile)
{
if(!view || url.isEmpty() || szFile.isEmpty())
return;
m_szFile = szFile;
m_view = view;
stepInit();
}
void CCaptureFullPage::stepInit()
{
qDebug(log) << "contents size:" << m_view->page()->contentsSize();
// 获取页面总高度
m_totalHeight = m_view->page()->contentsSize().height();
m_viewportHeight = m_view->size().height();
m_deltaY = m_viewportHeight;
m_currentY = 0;
m_images.clear();
m_view->page()->runJavaScript(
"document.documentElement.scrollTop;",
[this](const QVariant &result) {
m_originY = result.toInt();
//qDebug(log) << "OriginY:" << m_originY;
});
stepScroll();
}
void CCaptureFullPage::stepScroll()
{
if (m_currentY >= m_totalHeight) {
composeImage();
return;
}
// 滚动到当前位置
m_view->page()->runJavaScript(
QString("window.scrollTo(0, %1);").arg(m_currentY),
[this](const QVariant &) {
// 等待渲染
QTimer::singleShot(500, this, &CCaptureFullPage::stepGrab);
}
);
}
void CCaptureFullPage::stepGrab()
{
QImage img = m_view->grab().toImage();
// 如果是最后一块,有可能要裁掉多余的部分
int expectedHeight = (m_currentY + m_deltaY > m_totalHeight) ? (m_totalHeight - m_currentY) : m_deltaY;
if (img.height() > expectedHeight) {
img = img.copy(0, 0, img.width(), expectedHeight);
}
m_images.append(img);
m_currentY += m_deltaY;
stepScroll(); // 进入下一分块
}
void CCaptureFullPage::composeImage()
{
if (!m_images.isEmpty()) {
int fullWidth = m_images[0].width();
int fullHeight = 0;
foreach (const QImage &img, m_images) fullHeight += img.height();
QImage result(fullWidth, fullHeight, m_images[0].format());
QPainter painter(&result);
int y = 0;
foreach (const QImage &img, m_images) {
painter.drawImage(0, y, img);
y += img.height();
}
painter.end();
result.save(m_szFile);
qDebug() << "Full page screenshot saved to" << m_szFile;
}
m_view->page()->runJavaScript(
QString("window.scrollTo(0, %1);").arg(m_originY),
[this](const QVariant &) {});
emit sigFinished();
}