代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
QT实现截图功能并识别其中的二维码
作者:
theboy
/ 发布于
2013/7/1
/
2312
/* 1.按下ctrl+alt+z截图 2.按下ctrl+alt+x识别二维码 3.按下ctrl+alt+s保存图片 使用了zxing库实现二维码识别 使用QT第三方库libQtx中的QxtGlobalShortcut实现监听全局热键*/ #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
试试其它关键字
识别二维码
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
theboy
贡献的其它代码
(
2
)
.
QT实现截图功能并识别其中的二维码
.
三目运算
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3