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

FreeImage库如何转换图片格式?的更多相关文章

  1. java批量转换图片格式

    废话不多直接上代码,代码其实也不多.... package com.qiao.testImage; import java.awt.image.BufferedImage; import java.i ...

  2. 使用IMAGEMAGICK的CONVERT工具批量转换图片格式

    使用IMAGEMAGICK的CONVERT工具批量转换图片格式 http://www.qiansw.com/linux-imagemagick-convert-img.html Home > 文 ...

  3. Mac电脑如何转换图片格式?ImageWell for Mac转换图片格式教程

    想用Mac电脑转换图片格式?我想你可以借助ImageWell for Mac软件!ImageWell是一款简单好用的的图像处理工具,具有显示,编辑,处理,保存等功能.下面小编来为大家演示在Mac电脑上 ...

  4. 【最简单】不用ps也可以批量转换图片格式

    不废话直接开始~ 1.新建文件夹,把需要转换的图片放进去,如图: 2.文件夹里建一txt文本,重点来了!txt文本的内容,如果是jpg转为png,则输入“ren *.jpg *.png”,同理png转 ...

  5. ubuntu 转换图片格式的方法(sam2p, imagemagick)

    (1) 终端:sudo apt-get install sam2p sam2p [原图片名.格式] [目标图片名.格式] 即可在同一目录下生成目标图片格式 (2) 终端: sudo apt-get i ...

  6. python转换图片格式

    在图片所在的路径下,打开命令窗口 bmeps -c picturename.png picturename.eps

  7. 使用WIC组件转换图片格式

    #include <windows.h>#include <Wincodec.h>#pragma comment(lib, "Windowscodecs.lib&qu ...

  8. 利用C#转换图片格式及转换为ico

    注意:转换为ICO后效果不好. 源代码: using System;using System.Collections.Generic;using System.Text;using System.Dr ...

  9. ImageMagick 转换图片格式

    [root@ drawable-hdpi-v4]# convert ic_launcher.jpeg ic_launcher.png [root@ drawable-hdpi-v4]# file ic ...

随机推荐

  1. dBm与Vpp相互转换公式

    dBm = 10 + 20lg(0.5Vpp) Vpp = 2x10 以上公式均为阻抗为50欧的情况下计算得出的

  2. File类实现文件夹和文件复制

    package com.jcy.copy; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFou ...

  3. linux_通配符

    通配符和正则表达式区别? 通配符用在用户命令行bash环境,而正则表达式用于linux三剑客(awk, sed, grep) 那,有哪些通配符? * 所有字符    五星 ls *.txt # 列举目 ...

  4. Linxu指令--crond

    前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个 ...

  5. 【高并发简单解决方案】redis队列缓存 + mysql 批量入库 + php离线整合

    需求背景:有个调用统计日志存储和统计需求,要求存储到mysql中:存储数据高峰能达到日均千万,瓶颈在于直接入库并发太高,可能会把mysql干垮. 问题分析 思考:应用网站架构的衍化过程中,应用最新的框 ...

  6. 提取位于<title>...</title>内的文本标题内容

    #vim title.txt <title>nhlinkin</title> # cat title.txt  | sed 's:.*<title>\([^< ...

  7. 取IP的几个方法

    ifconfig eth0|grep " inet add"|cut -d":" -f2|cut -d " " -f1 ifconfig e ...

  8. mysql(4)—— 表连接查询与where后使用子查询的性能分析。

    子查询就是在一条查询语句中还有其它的查询语句,主查询得到的结果依赖于子查询的结果. 子查询的子语句可以在一条sql语句的FROM,JOIN,和WHERE后面,本文主要针对在WHERE后面使用子查询与表 ...

  9. squid安装及运行指南

    squid安装及运行指南 0. What is squid Squid是一个高性能的代理缓存服务器,Squid支持FTP.gopher.HTTPS和HTTP协议.和一般的代理缓存软件不同,Squid用 ...

  10. 夏令营讲课内容整理Day 0.

    今年没有发纸质讲义是最气的.还好我留了点课件. 第一次用这个估计也不怎么会用,但尝试一下新事物总是好的. 前四天gty哥哥讲的内容和去年差不多,后三天zhn大佬讲的内容有点难,努力去理解吧. 毕竟知识 ...