Qt+OpenCV实现图片压缩(JPEG、PNG)
一、概述
需求:
1.编写一个小工具实现图片压缩
2.图片仅支持JPEG和PNG格式
3.目的是压缩图片在磁盘中所占用的大小
4.使用的开发语言是Qt、C++、OpenCV
5.压缩的质量可以动态调节
如下图所示:
ps:
1.如果图片是jpeg压缩质量可以设置10~100,值越大质量越好,图片所占用的磁盘空间也就越大。综合测试下来值设置30的时候压缩的最为合理,且图片不失真,如果图片要求质量较高建议设置40以上。
2.如果图片是png格式,压缩质量是从0~9,值越大压缩率越高,如果没有特殊要求直接设置压缩率为9,直接压缩到最小就行。
二、代码示例
点击查看代码
#include "ImageCompressTool.h"
ImageCompressTool::ImageCompressTool(QWidget* parent)
: CommonGraphicsView{ parent }
{
this->setWindowTitle("图片压缩");
this->setFixedSize(QSize(320, 800));
QVBoxLayout* vLayout = new QVBoxLayout(this);
QHBoxLayout* hLayout = new QHBoxLayout(this);
EditText* et = new EditText(this);
et->setEnabled(false);
et->setFixedHeight(30);
Button* btnChoiceBtn = new Button(this);
btnChoiceBtn->setText("请选择图片");
hLayout->addWidget(et);
hLayout->addWidget(btnChoiceBtn);
hLayout->setAlignment(Qt::AlignTop);
QHBoxLayout* radioTypeHLayout = new QHBoxLayout(this);
QButtonGroup* group = new QButtonGroup(this);
QRadioButton* radioButtonJPEG = new QRadioButton("JPEG", this);
radioButtonJPEG->setFixedSize(60, 30);
QRadioButton* radioButton2PNG = new QRadioButton("PNG", this);
radioButton2PNG->setFixedSize(60, 30);
group->addButton(radioButtonJPEG, 1);
group->addButton(radioButton2PNG, 2);
radioTypeHLayout->addWidget(radioButtonJPEG);
radioTypeHLayout->addWidget(radioButton2PNG);
radioTypeHLayout->setAlignment(Qt::AlignLeft);
//图片质量(JPEG)
qualitLabel = new QLabel(this);
qualitLabel->setText("图片质量:" + QString::number(mKsize));
qualitLabel->setFixedHeight(15);
//显示原图大小(KB)
srcImageKb = new QLabel(this);
srcImageKb->setText("原图大小:");
srcImageKb->setFixedHeight(15);
//显示压缩后图片大小(KB)
compressImageKb = new QLabel(this);
compressImageKb->setText("压缩后图片大小:");
compressImageKb->setFixedHeight(15);
//开始压缩按钮
Button* startCompressBtn = new Button(this);
startCompressBtn->resize(60, 30);
startCompressBtn->setText("开始压缩");
QLabel* comBoxLabel = new QLabel(this);
comBoxLabel->setText("请选择图片质量:");
comBoxLabel->setFixedHeight(15);
//ComBox选择图片质量
comBox = new ComboBox(this);
for (int i = 10;i <= 100;i += 10) {
comBox->addItem(QString::number(i));
}
QHBoxLayout* comBoxHLayout2 = new QHBoxLayout(this);
comBoxHLayout2->addWidget(comBoxLabel);
comBoxHLayout2->addWidget(comBox);
imageViewResult = new QLabel(this);
vLayout->addLayout(hLayout);
vLayout->addLayout(radioTypeHLayout);
//vLayout->addWidget(imageViewSrc);
vLayout->addWidget(imageViewResult);
vLayout->setAlignment(Qt::AlignTop);
vLayout->addWidget(qualitLabel);
vLayout->addWidget(srcImageKb);
vLayout->addWidget(compressImageKb);
vLayout->addLayout(comBoxHLayout2);
vLayout->addWidget(startCompressBtn);
this->setLayout(vLayout);
connect(btnChoiceBtn, &Button::clicked, this, [=]() {
QFileInfo fileInfo;
filePath = QFileDialog::getOpenFileName(this, tr("请选择图片"), "C:/Users/DBF-DEV-103/Downloads/", tr("Image Files(*.jpg *.png *.jpeg)"));
et->setText(filePath);
fileInfo = QFileInfo(filePath);
//获取文件后缀名
QString fileSuffix = fileInfo.suffix();
qDebug() << "文件的后缀名为:" << fileSuffix;
if (fileSuffix == "jpeg" || fileSuffix == "jpg") {
imageType = 0;
radioButtonJPEG->setChecked(true);
comBox->clear();
for (int i = 10;i <= 100; i += 10) {
comBox->addItem(QString::number(i));
}
}
else if (fileSuffix == "png") {
imageType = 1;
radioButton2PNG->setChecked(true);
comBox->clear();
for (int i = 0;i <= 10; i++) {
comBox->addItem(QString::number(i));
}
}
//compressImage();
});
//选择了JPEG
connect(radioButtonJPEG, &QRadioButton::clicked, this, [=]() {
imageType = 0;
qDebug() << "图片类型" << imageType;
});
//选择了PNG
connect(radioButton2PNG, &QRadioButton::clicked, this, [=]() {
imageType = 1;
qDebug() << "图片类型:" << imageType;
});
//开始压缩按钮信号槽(点击事件)
connect(startCompressBtn, &Button::clicked, this, [=]() {
compressImage();
});
//选择图片质量信号槽
connect(comBox, &ComboBox::currentTextChanged, [=](const QString value) {
qDebug() << "选择的value值:" << value;
mKsize = value.toInt();
qualitLabel->setText("图片质量:" + QString::number(mKsize));
});
}
//压缩图片
void ImageCompressTool::compressImage() {
cv::Mat src = cv::imread(this->filePath.toStdString().c_str());
//imshow("src", src);
double srcKb = matSizeInKB(filePath.toStdString());
srcImageKb->setText("原图大小:" + QString::number(srcKb));
std::cout << "原图大小:" << srcKb << std::endl;
std::vector<int> compression_params;
if (imageType == 0) {
compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
//压缩级别范围从0~100,值越小压缩率越高,即在磁盘占用的空间越小,相应的图片的质量越差
compression_params.push_back(mKsize);
}
else {
compression_params.push_back(cv::IMWRITE_PNG_COMPRESSION);
if (mKsize > 10) {
mKsize = 3;
}
compression_params.push_back(mKsize); // 压缩级别范围从0到9,9是最高压缩
}
//将图片以jpg的格式存入文件
char* compressImgPath = "E://opencv_compress_img.jpeg";
if (imageType == 1) {
compressImgPath = "E://opencv_compress_img.png";
}
bool result = cv::imwrite(compressImgPath, src, compression_params);
if (!result) {
std::cout << "压缩图片失败" << endl;
return;
}
QFile compressFile = QFile(compressImgPath);
double compressSizeKb = matSizeInKB(compressImgPath);
compressImageKb->setText("压缩后的图片大小:" + QString::number(compressSizeKb));
std::cout << "压缩后的图片大小为:" << compressSizeKb << std::endl;
std::cout << "压缩图片成功,存储路径为:" << compressImgPath << std::endl;
}
long ImageCompressTool::matSizeInKB(std::string filePath) {
std::ifstream mFile(filePath, std::ifstream::binary);
if (mFile) {
mFile.seekg(0, mFile.end);
long size = mFile.tellg(); // 获取文件大小(字节)
mFile.close();
return size / 1024; // 转换为KB
}
return -1; // 文件打开失败,返回-1
}
ImageCompressTool::~ImageCompressTool()
{
}
Qt+OpenCV实现图片压缩(JPEG、PNG)的更多相关文章
- 使用 opencv 将图片压缩到指定文件尺寸
前言 图片压缩应用很广泛,如生成缩略图等.前期我在进行图片处理的过程中碰到了一个问题,就是如何将图片压缩到指定尺寸,此处尺寸指的是生成图片文件的大小. 我使用 opencv 进行图片处理,于是想着直接 ...
- 搭建Android+QT+OpenCV环境,实现“单色图片着色”效果
OpenCV是我们大家非常熟悉的图像处理开源类库:在其新版本将原本在Contrib分库中的DNN模块融合到了主库中,并且更新了相应文档.这样我们就能够非常方便地利用OpenCV实 ...
- Qt OpenCV Support Image Type 支持读写的图像格式
Qt 支持的图片格式如下: Format Description Qt's support BMP Windows Bitmap Read/write GIF Graphic Interchange ...
- Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩
目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...
- 三款不错的图片压缩上传插件(webuploader+localResizeIMG4+LUploader)
涉及到网页图片的交互,少不了图片的压缩上传,相关的插件有很多,相信大家都有用过,这里我就推荐三款,至于好处就仁者见仁喽: 1.名气最高的WebUploader,由Baidu FEX 团队开发,以H5为 ...
- Java中图片压缩处理
原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...
- android 图片压缩
引用:http://104zz.iteye.com/blog/1694762 第一:我们先看下质量压缩方法: private Bitmap compressImage(Bitmap image) { ...
- HTML5 CANVAS 实现图片压缩和裁切
原文地址:http://leonshi.com/2015/10/31/html5-canvas-image-compress-crop/?utm_source=tuicool&utm_medi ...
- C# 图片压缩
/// <summary> /// 图片压缩方法 /// </summary> /// <param name="sF ...
- Html5+asp.net mvc 图片压缩上传
在做图片上传时,大图片如果没有压缩直接上传时间会非常长,因为有的图片太大,传到服务器上再压缩太慢了,而且损耗流量. 思路是将图片抽样显示在canvas上,然后用通过canvas.toDataURL方法 ...
随机推荐
- 如何使用特定的SSH Key提交GIT
问题提出 最近在自己的MAC上面提交Github代码的时候发现居然失败了: $ git push origin master Permission denied (publickey). fatal: ...
- Spring boot 配置文件位置
Spring boot 的Application.properties 配置文件可以是以下几个地方:classpath:/,classpath:/config/,file:./,file:./conf ...
- 龙哥量化:TB交易开拓者_趋势跟踪策略_多策略对单品种_A00011880206期货量化策略,严格的用样本内参数, 跑样本外数据,滚动测试未来行情
如果您需要代写技术指标公式, 请联系我. 龙哥QQ:591438821 龙哥微信:Long622889 也可以把您的通达信,文华技术指标改成TB交易开拓者的自动交易量化策略. 量化策略介绍 投资标的: ...
- 智谱开源CogAgent的最新模型CogAgent-9B-20241220,全面领先所有开闭源GUI Agent模型
在现代数字世界中,图形用户界面(GUI)是人机交互的核心.然而,尽管大型语言模型(LLM)如ChatGPT在处理文本任务上表现出色,但在理解和操作GUI方面仍面临挑战,因此最近一年来,在学界和大模型社 ...
- Qt音视频开发04-保存音频文件(pcm/wav/aac)
一.前言 音频的保存相对来说比视频的要简单,具有通用性,不需要经过ffmpeg的编码,ffmpeg解码出来后一般会转换成pcm原始的数据用来播放,所以对数据直接写文件即可,但是这种格式是无法用播放器直 ...
- Qt开源作品39-日志输出增强版V2022
一.前言 之前已经开源过基础版本,近期根据客户需求和自己的项目需求,提炼出通用需求部分,对整个日志重定向输出类重新规划和重写代码. 用Qt这个一站式超大型GUI超市做开发已经十二年了,陆陆续续开发过至 ...
- Qt音视频开发49-通用截图截屏
一.前言 采用了回调方式的视频通道,截图只需要对解析好的QImage对象直接保存即可,而对于句柄的形式,需要调用不同的处理策略,比如vlc需要用它自己提供的api接口函数libvlc_video_ta ...
- python SQLAlchemy ORM——从零开始学习 02简单的增删查改
02 简单的增删查改 前情提要:承接了01中的engine以及User类 2-1 了解会话机制 个人理解 在SQLAlchemy 增删查改中是依赖会话(Session)这个机制进行操作的,我个人的理解 ...
- Win2D 投影效果 ShadowEffect
<Page x:Class="Win2DDemo.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/ ...
- Transformer 原理图解
转载:小白看得懂的 Transformer (图解) 引言 谷歌推出的BERT模型在11项NLP任务中夺得SOTA结果,引爆了整个NLP界.而BERT取得成功的一个关键因素是Transformer的强 ...