图片缩放与旋转
来源:互联网 发布:好看的网络剧青春校园 编辑:程序博客网 时间:2024/04/29 13:09
图片缩放与旋转
图片的缩放与旋转是图像处理的常用功能。QMatrix类提供了坐标系统的2D转化功能,可以使窗体转化变形,经常在绘图程序中使用,QMatrix可以实现坐标系统的移动、缩放、变形以及旋转功能。
1、创建imgprocessor.h
#ifndef IMGPROCESSOR_H#define IMGPROCESSOR_H#include <QtGui>class ImgProcessor : public QMainWindow{ Q_OBJECTpublic : 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、资源代码
0 0
- 图片缩放与旋转
- Android 图片缩放与旋转
- android图片缩放与旋转
- 图片的缩放与旋转
- Android实现图片缩放与旋转
- Android实现图片缩放与旋转
- Android实现图片缩放与旋转
- Android实现图片缩放与旋转
- SDL图片的旋转与缩放
- Android实现图片缩放与旋转
- Android实现图片缩放与旋转
- Android实现图片缩放与旋转
- Android实现图片缩放与旋转
- Android实现图片缩放与旋转
- Android实现图片缩放与旋转
- chapter14_2图片的旋转与缩放
- 图片旋转、缩放、反转
- 图片缩放 旋转
- 信号完整性分析和印制电路板
- Axure RP 8 Beta 测试版新功能介绍
- JMS开发(二):深入PTP,Pub-Sub两种模式
- qt 程序用外部程序打开文件
- 丄滑加载,下滑更新
- 图片缩放与旋转
- 手风琴JS特效源码
- java对象转化为JSON格式
- Swift-初学
- 【HashMap】深入原理解析
- Java虚拟机垃圾回收过程
- 神经网络该如何调参
- 解决C语言中生成的EXE文件执行后窗口消失方法
- html切换面板