QImage 图片分割、保存 | 您所在的位置:网站首页 › 如何截取jpg格式图片的一部分文件 › QImage 图片分割、保存 |
QImage 图片分割、保存
简介
之前一直只用Qt做图片显示,这次突发奇想想用Qt做做图像相关的,就尝试了一下图片切割,保存。 QImage介绍QImage类为Qt提供的一个支持图像算法处理的类库,可以精确到像素及单位。在我理解不是提供了什么算法,而是为图像计算提供了基础。 构造函数 QImage() QImage(const QSize &size, QImage::Format format) QImage(int width, int height, QImage::Format format) QImage(uchar *data, int width, int height, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr) QImage(const uchar *data, int width, int height, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr) QImage(uchar *data, int width, int height, int bytesPerLine, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr) QImage(const uchar *data, int width, int height, int bytesPerLine, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr) QImage(const char *const [] xpm) QImage(const QString &fileName, const char *format = nullptr) QImage(const QImage &image) QImage(QImage &&other)提供了以上构造方法,支持 uchar* ,const uchar*,filename 等元素的构造。 Format FormatDescriptionQt’s supportBMPWindows BitmapRead/writeGIFGraphic Interchange Format (optional)ReadJPGJoint Photographic Experts GroupRead/writeJPEGJoint Photographic Experts GroupRead/writePNGPortable Network GraphicsRead/writePBMPortable BitmapReadPGMPortable GraymapReadPPMPortable PixmapRead/writeXBMX11 BitmapRead/writeXPMX11 PixmapRead/writeQImage 支持的图片格式如上表所示,部分支持读写特性。 相关方法: QImage::Format format() const QImage convertToFormat(QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor) const & QImage convertToFormat(QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor) && QImage convertToFormat(QImage::Format format, const QVector &colorTable, Qt::ImageConversionFlags flags = Qt::AutoColor) const图像操作:像素、RGB、位深度、color、size 图像载入: bool load(const QString &fileName, const char *format = nullptr) bool load(QIODevice *device, const char *format) bool loadFromData(const uchar *data, int len, const char *format = nullptr) bool loadFromData(const QByteArray &data, const char *format = nullptr)图像数据: uchar *bits() const uchar *bits() const int bytesPerLine() const const uchar *constBits() const const uchar *constScanLine(int i) constRGB: QRgb color(int i) const QVector colorTable() const QRgb pixel(const QPoint &position) const QRgb pixel(int x, int y) const 思路介绍图像切割思路如下: 1.载入图片 2.切割图片 ->2.1 设置切割属性:将图片切割为 n*m 个图片 ->2.2 计算每个图片的rect,保存为rect列表 ->2.3 利用copy(Rect)函数将指定区域图片copy出来,存储到切割列表中 3.显示图片 -> 按照rect列表显示切割后的图片 4.保存图片 -> 将切割后的图片存储到指定位置 程序部分类图 主要由三个类组成:Image类,负责图像操作部分。MainWindow,主线调度类,负责功能性调度,或者说逻辑模块,业务部分。splite类。图片剪切设置框
图像载入 //利用load方法载入图像 void image::imageLoad(const QString path) { r_image.load(path); } void image::imageLoad(const QString path, int &_width, int &_height) { r_image.load(path); _width = r_image.width(); _height = r_image.height(); }图像切割 void image::iamgeItems(int x, int y, QList &imageList) { int r_width = r_image.width(); int r_height = r_image.height(); int cellWidth = r_width / x; int cellHeight = r_height / y; qDebug() imageData(); this->resize(imageWidth,imageHeight); }图片切割 void MainWindow::sltSpliteImage() { splite *_splite = new splite(); _splite->show(); connect(_splite,&splite::sign_spliteinfo,[=](int x,int y) { QList imageItems; ui->iamge->iamgeItems(x,y,imageItems); ui->iamge->imageShow(); }); }图像保存 void MainWindow::sltSaveImage() { ui->iamge->imageSave(); }图像算法选择 void MainWindow::sltcalcImage() { //设计中选择图像算法,注册对应图像算法,选择实现对应的图像算法 //算法模块中,特意提供了将图像数据列表,方便算法操作。 //近期会找一两个比较简单的图像算法来实现 } 其他函数说明主函数 int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }splite构造函数 splite::splite(QWidget *parent) : QWidget(parent), ui(new Ui::splite) { ui->setupUi(this); connect(ui->pushButton_right,&QPushButton::clicked,[=]() { emit sign_spliteinfo(ui->lineEdit_x->text().toInt(),ui->lineEdit_y->text().toInt()); this->close(); }); connect(ui->pushButton_cancel,&QPushButton::clicked,[=]() { this->close(); }); } 运行效果主程序界面 图像载入 图像切割 图像保存 Gitee |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |