图片缩放与旋转
2015-08-18 11:21
295 查看
图片缩放与旋转
图片的缩放与旋转是图像处理的常用功能。QMatrix类提供了坐标系统的2D转化功能,可以使窗体转化变形,经常在绘图程序中使用,QMatrix可以实现坐标系统的移动、缩放、变形以及旋转功能。1、创建imgprocessor.h
#ifndef IMGPROCESSOR_H #define IMGPROCESSOR_H #include <QtGui> class ImgProcessor : public QMainWindow { Q_OBJECT public : ImgProcessor(); void createMenus(); void createActions(); void createToolBars(); private: QImage img; QLabel *imageLabel; QMenu *fileMenu; QMenu *zoomMenu; QMenu *rotateMenu; QMenu *mirrorMenu; QAction *openFileAction; QAction *zoomInAction; QAction *zoomOutAction; QAction *rotate90Action; QAction *rotate180Action; QAction *rotate270Action; QAction *mirrorVerticalAction; QAction *mirrorHorizontalAction; QToolBar *fileTool; QToolBar *zoomTool; QToolBar *rotateTool; QToolBar *mirrorTool; protected slots: void slotOpenFile(); void slotZoomIn(); void slotZoomOut(); void slotRotate90(); void slotRotate180(); void slotRotate270(); void slotMirrorVertical(); void slotMirrorHorizontal(); }; #endif // IMGPROCESSOR_H
2、创建imgprocessor.cpp
#include "imgprocessor.h" ImgProcessor::ImgProcessor() : QMainWindow() { setWindowTitle(tr("Image Processor")); imageLabel = new QLabel(this); imageLabel->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored); imageLabel->setScaledContents(true); setCentralWidget(imageLabel); createActions(); createMenus(); createToolBars(); } void ImgProcessor::createActions() { openFileAction = new QAction(QIcon(":/images/openfile.png"),tr("Open"),this); openFileAction->setShortcut(tr("Ctrl+O")); openFileAction->setStatusTip(tr("Open File")); connect(openFileAction,SIGNAL(triggered()),this,SLOT(slotOpenFile())); // zoom in action zoomInAction = new QAction(QIcon(":/images/zoomin.png"),tr("ZoomIn"),this); zoomInAction->setShortcut(tr("PgUp")); zoomInAction->setStatusTip(tr("Zoom In")); connect(zoomInAction,SIGNAL(triggered()),this,SLOT(slotZoomIn())); // zoom out action zoomOutAction = new QAction(QIcon(":/images/zoomout.png"),tr("ZoomOut"),this); zoomOutAction->setShortcut(tr("PgDown")); zoomOutAction->setStatusTip(tr("Zoom Out")); connect(zoomOutAction,SIGNAL(triggered()),this,SLOT(slotZoomOut())); // rotate 90 action rotate90Action = new QAction(QIcon(":/images/rotate90.png"),tr("Rotate90"), this); rotate90Action->setShortcut(tr("Ctrl+Right")); rotate90Action->setStatusTip(tr("Rotate 90")); connect(rotate90Action, SIGNAL(triggered()), this, SLOT(slotRotate90())); // rotate 180 action rotate180Action = new QAction(QIcon(":/images/rotate180.png"),tr("Rotate180"), this); rotate180Action->setShortcut(tr("Ctrl+Down")); rotate180Action->setStatusTip(tr("Rotate 180")); connect(rotate180Action, SIGNAL(triggered()), this, SLOT(slotRotate180())); // rotate 270 action rotate270Action = new QAction(QIcon(":/images/rotate270.png"),tr("Rotate270"), this); rotate270Action->setShortcut(tr("Ctrl+Left")); rotate270Action->setStatusTip(tr("Rotate 270")); connect(rotate270Action, SIGNAL(triggered()), this, SLOT(slotRotate270())); // mirror vertical action mirrorVerticalAction = new QAction(QIcon(":/images/mirrorvertical.png"),tr("MirrorVertical"), this); mirrorVerticalAction->setShortcut(tr("Ctrl+V")); mirrorVerticalAction->setStatusTip(tr("Mirror Vertical")); connect(mirrorVerticalAction, SIGNAL(triggered()), this, SLOT(slotMirrorVertical())); // mirror horizontal action mirrorHorizontalAction = new QAction(QIcon(":/images/mirrorhorizontal.png"),tr("MirrorHorizontal"), this); mirrorHorizontalAction->setShortcut(tr("Ctrl+H")); mirrorHorizontalAction->setStatusTip(tr("Mirror Horizontal")); connect(mirrorHorizontalAction, SIGNAL(triggered()), this, SLOT(slotMirrorHorizontal())); } void ImgProcessor::createMenus() { fileMenu = menuBar()->addMenu(tr("File")); zoomMenu = menuBar()->addMenu(tr("zoom")); rotateMenu = menuBar()->addMenu(tr("rotate")); mirrorMenu = menuBar()->addMenu(tr("mirror")); fileMenu->addAction(openFileAction); zoomMenu->addAction(zoomInAction); zoomMenu->addAction(zoomOutAction); rotateMenu->addAction(rotate90Action); rotateMenu->addAction(rotate180Action); rotateMenu->addAction(rotate270Action); mirrorMenu->addAction(mirrorVerticalAction); mirrorMenu->addAction(mirrorHorizontalAction); } void ImgProcessor::createToolBars() { fileTool = addToolBar("File"); zoomTool = addToolBar("Zoom"); rotateTool = addToolBar("Rotate"); mirrorTool = addToolBar("Mirror"); fileTool->addAction(openFileAction); zoomTool->addAction(zoomInAction); zoomTool->addAction(zoomOutAction); rotateTool->addAction(rotate90Action); rotateTool->addAction(rotate180Action); rotateTool->addAction(rotate270Action); mirrorTool->addAction(mirrorVerticalAction); mirrorTool->addAction(mirrorHorizontalAction); } void ImgProcessor::slotOpenFile() { QString s = QFileDialog::getOpenFileName(this, "open image file", ".", "Image files (*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);;All files (*.*))"); if(s != "") { if(img.load(s)) { imageLabel->setPixmap(QPixmap::fromImage(img)); resize(img.width(),img.height()); } } } void ImgProcessor::slotZoomIn() { if(img.isNull()) { return; } else { QMatrix matrix; matrix.scale(2,2); img = img.transformed(matrix); imageLabel->setPixmap(QPixmap::fromImage(img)); resize(img.width(),img.height()); } } void ImgProcessor::slotZoomOut() { if(img.isNull()) { return; } QMatrix matrix; matrix.scale(0.5,0.5); img = img.transformed(matrix); imageLabel->setPixmap(QPixmap::fromImage(img)); resize(img.width(),img.height()); } void ImgProcessor::slotRotate90 () { if(img.isNull()) { return; } QMatrix matrix; matrix.rotate(90); img = img.transformed(matrix); imageLabel->setPixmap(QPixmap::fromImage(img)); resize(img.width(),img.height()); } void ImgProcessor::slotRotate180 () { if(img.isNull()) { return; } QMatrix matrix; matrix.rotate(180); img = img.transformed(matrix); imageLabel->setPixmap(QPixmap::fromImage(img)); resize(img.width(),img.height()); } void ImgProcessor::slotRotate270 () { if(img.isNull()) { return; } QMatrix matrix; matrix.rotate(270); img = img.transformed(matrix); imageLabel->setPixmap(QPixmap::fromImage(img)); resize(img.width(),img.height()); } void ImgProcessor::slotMirrorVertical () { if(img.isNull()) { return; } img = img.mirrored(false,true); imageLabel->setPixmap(QPixmap::fromImage(img)); resize(img.width(),img.height()); } void ImgProcessor::slotMirrorHorizontal () { if(img.isNull()) { return; } img = img.mirrored(true,false); imageLabel->setPixmap(QPixmap::fromImage(img)); resize(img.width(),img.height()); }
3、创建main.cpp
#include <QApplication> #include "imgprocessor.h" int main(int argc,char *argv[]) { QApplication app(argc,argv); ImgProcessor *img = new ImgProcessor; img->show(); return app.exec(); }
4、创建资源文件imgprocessor.qrc
5、运行程序,读取一张图片,可以试一下放大,缩小等功能。
6、资源代码
相关文章推荐
- How to get md5 and SHA1 in objective c (iOS sdk)
- C# 未将对象引用设置到对象的实例
- 企业管理器备份和还原SQL Server数据库
- 使Eclipse或AndroidStudio的代码模板自动生成类名和方法名
- 职场奇葩说:不得不了解的11个奇葩现象
- R语言实战之关系展示
- 手风琴JS特效源码
- 解决pom文件依赖冲突
- 信号完整性分析和印制电路板
- ZOJ 3810 A Volcanic Island (2014年牡丹江赛区网络赛B题)
- iOS中表视图(UITableView)使用详解
- 数据库,表,字段,类型等查询
- 剑指offer-第四章解决面试题思路(判断一个数组是否为二叉搜索树的后序遍历序列)
- 用命令行cmd 编译小程序
- TCP协议通信补充:多线程
- webservice之简单创建和发布(一个加法运算)
- ZedBoard Linux开发 --- TFTP&NFS启动
- string byte[] 互转
- hdu 1864 dp 01背包
- hdu1116&&poj1386