#ifndef SCREENSHOTHELPER_H
#define SCREENSHOTHELPER_H
#include
#include
#include
#include
#include
#include
class ScreenshotHelper {
public:
ScreenshotHelper();
~ScreenshotHelper();
enum StitchDirection {
Horizontal,
Vertical
};
static QPixmap capture(QWidget *widget);
static bool save(const QPixmap &pixmap, const QString &filepath);
static bool captureAndSave(QWidget *widget, const QString &filePath);
static QPixmap captureAndScale(QWidget *widget, int width, int height);
static QPixmap scale(const QPixmap &pixmap, int width, int height);
static QPixmap stitch(const QList
};
#endif //SCREENSHOTHELPER_H
ScreenshotHelper.cpp
#include "ScreenshotHelper.h"
ScreenshotHelper::ScreenshotHelper() {
}
ScreenshotHelper::~ScreenshotHelper() {
}
/**
* 截取给定QWidget的截图
* @param widget
* @return
*/
QPixmap ScreenshotHelper::capture(QWidget *widget) {
if (widget) {
QPoint point = widget->mapToGlobal(QPoint(0, 0)); // 获取窗口左上角的位置
QSize size = widget->size(); // 获取窗口的大小
QRect rect(point, size); // 创建一个QRect对象,指定区域位置和大小
QScreen *screen = QGuiApplication::primaryScreen();
QPixmap fullScreenPixmap = screen->grabWindow(0); // 捕获整个屏幕
return fullScreenPixmap.copy(rect); // 截取指定区域
}
return QPixmap();
}
/**
* 将给定的QPixmap保存到指定文件路径
* @param pixmap
* @param filepath
* @return
*/
bool ScreenshotHelper::save(const QPixmap &pixmap, const QString &filepath) {
if (!pixmap.isNull() && !filepath.isEmpty()) {
return pixmap.save(filepath);
}
return false;
}
/**
* 截取并保存截图
* @param widget
* @param filePath
* @return
*/
bool ScreenshotHelper::captureAndSave(QWidget *widget, const QString &filePath) {
QPixmap pixmap = capture(widget);
return save(pixmap, filePath);
}
/**
* 截取并缩放截图到指定大小
* @param widget
* @param width
* @param height
* @return
*/
QPixmap ScreenshotHelper::captureAndScale(QWidget *widget, int width, int height) {
QPixmap pixmap = capture(widget);
if (!pixmap.isNull()) {
return pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
return QPixmap();
}
QPixmap ScreenshotHelper::scale(const QPixmap &pixmap, int width, int height) {
if (!pixmap.isNull()) {
return pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
return QPixmap();
}
/**
* 拼接多个QWidget的截图,方向可选水平或垂直
* @param widgets
* @param direction
* @return
*/
QPixmap ScreenshotHelper::stitch(const QList
QList
int totalWidth = 0;
int totalHeight = 0;
for (QWidget *widget: widgets) {
QPixmap pixmap = capture(widget);
if (!pixmap.isNull()) {
if (direction == Horizontal) {
totalWidth += pixmap.width();
totalHeight = qMax(totalHeight, pixmap.height());
} else {
totalWidth = qMax(totalWidth, pixmap.width());
totalHeight += pixmap.height();
}
pixmaps.append(pixmap);
}
}
if (pixmaps.isEmpty()) {
return QPixmap();
}
QPixmap stitchedPixmap;
if (direction == Horizontal) {
stitchedPixmap = QPixmap(totalWidth, totalHeight);
} else {
stitchedPixmap = QPixmap(totalWidth, totalHeight);
}
stitchedPixmap.fill(Qt::white);
QPainter painter(&stitchedPixmap);
int currentPos = 0;
for (const QPixmap &pixmap: pixmaps) {
if (direction == Horizontal) {
painter.drawPixmap(currentPos, 0, pixmap);
currentPos += pixmap.width();
} else {
painter.drawPixmap(0, currentPos, pixmap);
currentPos += pixmap.height();
}
}
return stitchedPixmap;
}