QT实现截图功能并识别其中的二维码
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
1.按下ctrl+alt+z截图 2.按下ctrl+alt+x识别二维码 3.按下ctrl+alt+s保存图片 使用了zxing库实现二维码识别 使用QT第三方库libQtx中的QxtGlobalShortcut实现监听全局热键 //mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { this->setWindowTitle("BusyBoy.snap with qr[按下H键显示帮助]"); setMinimumSize(360, 80); setWindowIcon(QIcon(":images/icon")); clipboard = QApplication::clipboard(); QxtGlobalShortcut *sc_z = new QxtGlobalShortcut(QKeySequence("Ctrl+Alt+Z"), this); connect(sc_z, SIGNAL(activated()), this, SLOT(onSnap())); QxtGlobalShortcut *sc_x = new QxtGlobalShortcut(QKeySequence("Ctrl+Alt+X"), this); connect(sc_x, SIGNAL(activated()), this, SLOT(onQr())); QxtGlobalShortcut *sc_s = new QxtGlobalShortcut(QKeySequence("Ctrl+Alt+S"), this); connect(sc_s, SIGNAL(activated()), this, SLOT(onSave())); QxtGlobalShortcut *sc_c = new QxtGlobalShortcut(QKeySequence("Ctrl+Alt+C"), this); connect(sc_c, SIGNAL(activated()), this, SLOT(genQr())); label = new QLabel(this); infoLabel = new QLabel(this); helpMsg.append("按下Ctrl+Alt+Z截图\n"); helpMsg.append("按下Ctrl+Alt+X识别二维码\n"); helpMsg.append("按下Ctrl+Alt+S保存图片\n"); helpMsg.append("按下h显示本帮助\n"); helpMsg.append("Esc退出本程序\n"); infoLabel->setText(helpMsg); infoLabel->adjustSize(); isOnSnapping = false; rubberBand = new QRubberBand(QRubberBand::Rectangle, label); isLeftButtonPressed = false; iMsg.append("原谅我这一生不羁放纵爱自由!"); } MainWindow::~MainWindow() { delete ui; } void MainWindow::keyPressEvent(QKeyEvent *ev){ if(ev->key() == Qt::Key_Escape){ if(isOnSnapping == true){ this->showNormal(); isOnSnapping = false; }else if(isOnSnapping == false){ QApplication::exit(0); } }else if(ev->key() == Qt::Key_H){ this->showNormal(); label->setText(helpMsg); label->adjustSize(); infoLabel->hide(); this->adjustSize(); }else if(ev->key() == Qt::Key_I){ QMessageBox::about(this,"i", iMsg); } } void MainWindow::onSnap(){ fullScreen = QPixmap::grabWindow(QApplication::desktop()->winId()); this->showNormal(); this->showFullScreen(); label->setPixmap(fullScreen); label->show(); label->adjustSize(); isOnSnapping = true; } void MainWindow::mouseMoveEvent(QMouseEvent *ev){ QPoint pos = ev->pos(); // 截图状态显示截图信息 if(isOnSnapping){ int x = pos.x(); int y = pos.y(); int w = infoLabel->width(); int h = infoLabel->height(); QString info = ""; info.append("x:").append(QString::number(start.x())); info.append("\ny:").append(QString::number(start.y())); info.append("\n*\nwidth:").append(QString::number(pos.x() - start.x())); info.append("\nheight:").append(QString::number(pos.y() - start.y())); infoLabel->setText(info); infoLabel->setGeometry(QRect(x, y, w, h)); infoLabel->adjustSize(); infoLabel->setStyleSheet("background:#eaeaea;"); infoLabel->show(); }else{ infoLabel->hide(); } if(ev->type() == QEvent::MouseMove&& isLeftButtonPressed){ if(rubberBand){ rubberBand->setGeometry(QRect(start, ev->pos()).normalized()); rubberBand->show(); } } } void MainWindow::mousePressEvent(QMouseEvent *ev){ if(ev->button() == Qt::LeftButton&& ev->type() == QEvent::MouseButtonPress&& isOnSnapping){ isLeftButtonPressed = true; start = ev->pos(); if(!rubberBand){ rubberBand = new QRubberBand(QRubberBand::Rectangle, label); } rubberBand->setGeometry(QRect(start, QSize())); rubberBand->show(); } } void MainWindow::mouseReleaseEvent(QMouseEvent *ev){ if(isLeftButtonPressed&& isOnSnapping){ isLeftButtonPressed = false; end = ev->pos(); QPixmap pixmap = fullScreen.copy(QRect(start, end)); clipboard->setImage(pixmap.toImage()); this->showNormal(); label->setPixmap(pixmap); label->adjustSize(); this->resize(label->size()); rubberBand->hide(); isOnSnapping = false; } } void MainWindow::onQr(){ QPixmap pixmap = fullScreen.copy(QRect(start, end)); if(pixmap.isNull()){ QMessageBox::warning(this, "提示", "还未截图"); }else{ QZXing decoder; QString qrmsg = decoder.decodeImage(pixmap.toImage()); if(qrmsg.isEmpty()){ QMessageBox::warning(this, "识别失败", "请截取完整的二维码"); }else{ QMessageBox::information(this, "识别成功-已经复制到剪切板",qrmsg); clipboard->setText(qrmsg); } } } void MainWindow::onSave(){ QPixmap pixmap = fullScreen.copy(QRect(start, end)); if(pixmap.isNull()){ QMessageBox::warning(this, "提示", "还未截图"); }else{ QString savePath = QFileDialog::getSaveFileName(this, tr("Open Image"), ".", tr("Image Files(*.jpg,*.png,*.bmp)")); if(savePath.endsWith(".png") || savePath.endsWith(".jpg") || savePath.endsWith(".bmp")){ }else{ if(QFileInfo(savePath).fileName().isEmpty()){ savePath += "image"; } savePath.append(".png"); } QFileInfo finfo(savePath); if(finfo.exists()){ QMessageBox::StandardButton reply = QMessageBox::question(this,"提示","["+finfo.fileName()+"]文件已经存在是否要覆盖?", QMessageBox::Yes | QMessageBox::No); if(reply == QMessageBox::No){ return; } } pixmap.save(savePath); } } void MainWindow::genQr(){ // qDebug()<<"gen qr"; // QString qrmsg = clipboard->text(); // if(qrmsg.isEmpty()){ // QMessageBox::warning(this, "提示", "请先将信息复制到剪切板中."); // return; // } // qDebug()<<qrmsg; } //mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QtGui> #include <QKeyEvent> #include <QMouseEvent> #include <QDebug> #include <QPoint> #include "qxtglobalshortcut/qxtglobalshortcut.h" #include "QZXing.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; QLabel *label; QLabel *infoLabel; bool isOnSnapping; QPixmap fullScreen; QString helpMsg; QString iMsg; QPoint start; QPoint end; bool isLeftButtonPressed; QRubberBand *rubberBand; QClipboard *clipboard; void keyPressEvent(QKeyEvent *ev); void mouseMoveEvent(QMouseEvent *ev); void mousePressEvent(QMouseEvent *ev); void mouseReleaseEvent(QMouseEvent *ev); private slots: void onSnap(); void onQr(); void onSave(); void genQr(); }; #endif // MAINWINDOW_H QT += core gui TARGET = QReader TEMPLATE = app RC_FILE += logo.rc CONFIG += -static include(qxtglobalshortcut/qxtglobalshortcut.pri) win32{ INCLUDEPATH += D:/lib/qt/xzing LIBS += -LD:/lib/qt/xzing \ -lQZXing1 } SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui RESOURCES += \ res.qrc |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=479