QT 5.15.2 实现截图、拼图

QT 5.15.2 实现截图、拼图

#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 &widgets, StitchDirection direction);

};

#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 &widgets, StitchDirection direction) {

QList pixmaps;

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;

}

你可能也喜欢

聊一聊那些你不知道的妖邪附体相关事
365bet官网平台网址

聊一聊那些你不知道的妖邪附体相关事

📅 07-12 👀 2745
3吨等于多少千克?
怎样获得免费office365

3吨等于多少千克?

📅 08-31 👀 7495
百度手机卫士如何退出?百度手机卫士关闭方法
beat365手机版官方网站正规

百度手机卫士如何退出?百度手机卫士关闭方法

📅 08-30 👀 2809
中脘痛是怎么回事
beat365手机版官方网站正规

中脘痛是怎么回事

📅 07-31 👀 6939
足彩如何拿下高赔率冷门,以大小球为心水胆
365bet官网平台网址

足彩如何拿下高赔率冷门,以大小球为心水胆

📅 08-19 👀 6298
12部高分欧美硬汉动作大片,肾上腺素狂飙到爆!
beat365手机版官方网站正规

12部高分欧美硬汉动作大片,肾上腺素狂飙到爆!

📅 07-01 👀 2223