FreeImage下载地址:http://freeimage.sourceforge.net/
//freeimagemain.h
#ifndef FREEIMAGEMAIN_H
#define FREEIMAGEMAIN_H
#include <QWidget>
#include <QtCore>
#include <QStatusBar>
#include <QFileDialog>
#include "FreeImage.h"
namespace Ui {
class FreeImageMain;
}
class FreeImageMain : public QWidget
{
Q_OBJECT
public:
explicit FreeImageMain(QWidget *parent = 0);
~FreeImageMain();
bool ImageTransformationImage(QString sourcePath,QString destinationPath) const;
//根据文件路径获取格式
int GetImagePathSuffix(QString path) const;
private:
Ui::FreeImageMain *ui;
QTextCodec *textCodec;
QString m_sourcePath;
QString m_destinationPath;
QStatusBar *statusBar;
};
#endif // FREEIMAGEMAIN_H
//freeimagemain.cpp
#include "freeimagemain.h"
#include "ui_freeimagemain.h"
#include <stdio.h>
#include <stdlib.h>
FreeImageMain::FreeImageMain(QWidget *parent) :
QWidget(parent),
ui(new Ui::FreeImageMain)
{
ui->setupUi(this);
textCodec = QTextCodec::codecForName("GBK");
statusBar = new QStatusBar;
ui->verticalLayout->addWidget(statusBar);
QObject::connect(ui->pushButton_load,&QPushButton::pressed,[=](){
this->m_sourcePath = QFileDialog::getOpenFileName(this,tr(textCodec->toUnicode("加载位图").toStdString().data()), "./", tr("Image Files (*ico *.png *.jpg *.bmp)"));
if(this->m_sourcePath.isEmpty())
{
statusBar->showMessage(textCodec->toUnicode("位图加载路径不能为空!"),3000);
}
else
{
statusBar->showMessage(textCodec->toUnicode("位图加载路径获取成功!"),3000);
}
});
QObject::connect(ui->pushButton_save,&QPushButton::pressed,[=](){
//源路径为空直接返回
if(this->m_sourcePath.isEmpty())
{
statusBar->showMessage(textCodec->toUnicode("请先加载位图路径!"),3000);
return;
}
this->m_destinationPath = QFileDialog::getSaveFileName(this,tr(textCodec->toUnicode("加载位图").toStdString().data()), "./", tr("Image Files (*ico *.png *.jpg *.bmp)"));
if(this->m_destinationPath.isEmpty())
{
statusBar->showMessage(textCodec->toUnicode("存储位图路径不能为空!"),3000);
return;
}
else
{
statusBar->showMessage(textCodec->toUnicode("位图存储路径获取成功!"),3000);
this->ImageTransformationImage(m_sourcePath,m_destinationPath);
}
});
}
FreeImageMain::~FreeImageMain()
{
statusBar->deleteLater();
delete ui;
}
int FreeImageMain::GetImagePathSuffix(QString path) const
{
// QString::compare(path.split(".").constLast(), "BMP", Qt::CaseInsensitive);
QString Suffix = path.split(".").constLast().toUpper();
if(Suffix == "BMP")
{
return FIF_BMP;
}
else if (Suffix == "ICO")
{
return FIF_ICO;
}
else if (Suffix == "JPEG")
{
return FIF_JPEG;
}
else if (Suffix == "JNG")
{
return FIF_JNG;
}
else if (Suffix == "KOALA")
{
return FIF_KOALA;
}
else if (Suffix == "LBM")
{
return FIF_LBM;
}
else if (Suffix == "IFF")
{
return FIF_IFF;
}
else if (Suffix == "MNG")
{
return FIF_MNG;
}
else if (Suffix == "PBM")
{
return FIF_PBM;
}
else if (Suffix == "PBMRAW")
{
return FIF_PBMRAW;
}
else if (Suffix == "PCD")
{
return FIF_PCD;
}
else if (Suffix == "PCX")
{
return FIF_PCX;
}
else if (Suffix == "PGM")
{
return FIF_PGM;
}
else if (Suffix == "PGMRAW")
{
return FIF_PGMRAW;
}
else if (Suffix == "PNG")
{
return FIF_PNG;
}
else if (Suffix == "PPM")
{
return FIF_LBM;
}
else if (Suffix == "PPMRAW")
{
return FIF_PPMRAW;
}
else if (Suffix == "RAS")
{
return FIF_RAS;
}
else if (Suffix == "TARGA")
{
return FIF_TARGA;
}
else if (Suffix == "TIFF")
{
return FIF_TIFF;
}
else if (Suffix == "WBMP")
{
return FIF_WBMP;
}
else if (Suffix == "PSD")
{
return FIF_PSD;
}
else if (Suffix == "CUT")
{
return FIF_CUT;
}
else if (Suffix == "XBM")
{
return FIF_XBM;
}
else if (Suffix == "XPM")
{
return FIF_XPM;
}
else if (Suffix == "DDS")
{
return FIF_DDS;
}
else if (Suffix == "GIF")
{
return FIF_GIF;
}
else if (Suffix == "HDR")
{
return FIF_HDR;
}
else if (Suffix == "FAXG3")
{
return FIF_FAXG3;
}
else if (Suffix == "SGI")
{
return FIF_SGI;
}
else if (Suffix == "EXR")
{
return FIF_EXR;
}
else if (Suffix == "J2K")
{
return FIF_J2K;
}
else if (Suffix == "JP2")
{
return FIF_JP2;
}
else if (Suffix == "PFM")
{
return FIF_PFM;
}
else if (Suffix == "PICT")
{
return FIF_PICT;
}
else if (Suffix == "RAW")
{
return FIF_RAW;
}
else if (Suffix == "WEBP")
{
return FIF_WEBP;
}
else if (Suffix == "JXR")
{
return FIF_JXR;
}
return FIF_UNKNOWN;
}
bool FreeImageMain::ImageTransformationImage(QString sourcePath, QString destinationPath) const
{
int imageFormat = FreeImage_GetFIFFromFilename(sourcePath.toStdString().data());//获取文件格式
//static_cast<FREE_IMAGE_FORMAT>(imageFormat) 把 int 转换成 FREE_IMAGE_FORMAT 枚举类型
FIBITMAP *dib = FreeImage_Load(static_cast<FREE_IMAGE_FORMAT>(imageFormat),sourcePath.toStdString().data());
if(dib == NULL)
{
// qDebug()<<textCodec->toUnicode("图像加载失败!");
statusBar->showMessage(textCodec->toUnicode("图像加载失败!"),3000);
return false;
}
// qDebug()<<textCodec->toUnicode("ImageType:")<<FreeImage_GetImageType(dib);//获取位图类型
// qDebug()<<textCodec->toUnicode("BPP:")<<FreeImage_GetBPP(dib);//获取位深
unsigned int width = FreeImage_GetWidth(dib);//获取图像的宽
unsigned int height = FreeImage_GetHeight(dib);//获取图像的高
if(imageFormat == FIF_ICO && width!=height)
{
// qDebug()<<textCodec->toUnicode("ICO-请确保位图的宽和高相等!");
statusBar->showMessage(textCodec->toUnicode("ICO-请确保位图的宽和高相等!"),3000);
}
if(FreeImage_Save(static_cast<FREE_IMAGE_FORMAT>(GetImagePathSuffix(destinationPath)),dib,destinationPath.toStdString().data()))
{
qDebug()<<textCodec->toUnicode("位图格式转换成功");
statusBar->showMessage(textCodec->toUnicode("位图格式转换成功"),3000);
return true;
}
else
{
qDebug()<<textCodec->toUnicode("位图格式转换失败");
statusBar->showMessage(textCodec->toUnicode("位图格式转换失败"),3000);
return false;
}
}
//RC_ICONS = ./LOGO.ico