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. jdbc与java.sql

    JDBC连接操作数据库流程:1.将数据库驱动jar包放在lib文件夹下. 2.定义驱动名(driver),数据库url,username,password字符串常量 3.注册数据库驱动Class.fo ...

  2. android之间传递list

    Intent intent = new Intent(getActivity(), Activity_Character.class); intent.putExtra("mlTrait&q ...

  3. NodeJs实现他人项目实例

    1.简单实例,参考 https://github.com/alsotang/node-lessons/tree/master/lesson2 2.express一个新项目 ,但出现警告 发现少了nod ...

  4. 并发思考-actor和thread那个好点?

    实验课题:测试actor和thread那个好? 实验方法:利用数据库连接池创建连接,交由线程去工作,在回收,看看程序运行状况. 实验步骤: 1.创建数据连接工具类: import java.sql.{ ...

  5. OSSEC初探

    OSSEC初探 概念: OSSEC是一款开源的基于主机的入侵检测系统(HIDS),它可以执行日志分析.完整性检验.windows注册表监控.隐匿性检测和实时告警.它可以运行在各种不同的操作系统上,包括 ...

  6. C++ explicit关键字详解(转载)

    转载:https://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函 ...

  7. spring之setter注入

    setter注入分为2种 第一:普通属性注入 <bean id="userAction" class="com.xx.action.UserAction" ...

  8. java中的二叉树排序问题

    原创:转载请注明出处 目的:想用java实现二叉树排序算法 思想:利用java中面向对象的思想,即: Tree:类 树根Tree:root //static所属于每一个Tree 左节点Tree:lef ...

  9. JAVA设计模式---迭代器模式

    1.定义: 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 2.实例:1)需求: 菜单(煎饼屋菜单.餐厅菜单和咖啡菜单)采用不同的集合存取(ArrayList,String[] ...

  10. jQuery控制input只能输入两位数字和小数(金额)

    function num(obj){ obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和". ...