1、CxImage库下载
官网地址:https://sourceforge.net/projects/cximage/
ps:自动下载可能有点慢,耐心等一下。若无反应点击Problems Downloading?重新 选择一个镜像。
2、vs2015编译
新建文件夹,CxImg。将压缩包解压至此文件夹。打开CxImageFull_vc10.sln。VS2015提示“升级VC++编译器和库”。点确定。按照zlib,tiff,png,mng,libpsd,libdcr,jpeg,jbig,jasper,cimage,cximagecrtdll,cximagemfcdll,demodll,demo的顺序逐个编译。
编译cximage过程中会遇到:
error C2371:“int_fast16_t”:重定义;不同的基类型。
解决办法:打开vs2015安装目录。默认C盘目录:C:\Program Files (x86)\Microsoft Visual Studio 14.0\include。搜索stdint。复制此文件stdint.h到CxImg\CxImage。替换掉原有的stdint.h。
编译cximagemfcdll过程中会遇到:
error LINK1181:无法打开输入文件".\Release\png.lib"
解决办法:
将缺少的库文件从CxImg\Release复制到CxImg\CxImage\CxImageDLL\Release目录下。
至此,全部编译完成。新建inc文件夹。将CxImg\CxImage下所有.h文件复制到icn文件夹。新建lib文件夹。将CxImg\Release下所有内容复制到lib文件夹下。inc、lib复制到自己的工程目录。配置好属性。属性配置参考CxImage的demo即可。
3、图片格式转换
#include "ximage.h"
#include <iostream>
int main()
{
std::string imgPath = "E:\\1.jpg";
//string转TCHAR
TCHAR fileName[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)imgPath.c_str(), -1, fileName, MAX_PATH);
CxImage img1(fileName, CXIMAGE_FORMAT_JPG);
CxImage img2(img1);
img2.Save(_T("E:\\export.png"), CXIMAGE_FORMAT_PNG);
return 0;
}
4、压缩
压缩方式:6(best compression)、8(default compression)、2(no compression)。best compression会比default compression产生的文件大一些,但图片质量会好一些。在上一步格式转换的时候CxImage已经采用默认压缩方式对图片进行了压缩。下面附上采用其他方式的压缩代码:
CxImage img1;
img1.Load(_T("E:\\1.jpg"), CXIMAGE_FORMAT_JPG);
CxImage img2(img1);
// 增加图片分辨率
if (!img2.IsGrayScale()) img2.IncreaseBpp(24);
img2.SetTransIndex(-1);
int quality = img2.GetJpegQuality();
// best compression
img2.SetJpegQuality(quality | 6);
img2.Save(_T("E:\\compress.png"), CXIMAGE_FORMAT_PNG);
5、截取
CxImage img1;
img1.Load(_T("E:\\1.jpg"), CXIMAGE_FORMAT_JPG);
CxImage img2;
// 从(10,20)位置开始截取50x50大小的图片
int x = 10, y = 20;
int right = x + 50;
int bottom = y + 50;
img1.crop(x, y, right, bottom, &img2);
img2.Save(_T("E:\\crop.png"), CXIMAGE_FORMAT_PNG);
6、调整文件大小
CxImage img1;
img1.Load(_T("E:\\1.jpg"), CXIMAGE_FORMAT_JPG);
CxImage img2(img1);
// 调整图片文件大小为100x100
img2.Resample(100, 100);
img2.Save(_T("E:\\Resize.png"), CXIMAGE_FORMAT_PNG);
7、根据文件名转换出图片类型
bool FindType(const std::string& fileName, int& type)
{
int pos = fileName.find_last_of(".");
if (pos == std::string::npos) {
return false;
}
int num = fileName.length() - pos - 1;
std::string ext = filename.substr(pos + 1, num);
if (ext == "bmp") type = CXIMAGE_FORMAT_BMP;
#if CXIMAGE_SUPPORT_JPG
else if (ext=="jpg"||ext=="jpeg") type = CXIMAGE_FORMAT_JPG;
#endif
#if CXIMAGE_SUPPORT_GIF
else if (ext == "gif") type = CXIMAGE_FORMAT_GIF;
#endif
#if CXIMAGE_SUPPORT_PNG
else if (ext == "png") type = CXIMAGE_FORMAT_PNG;
#endif
#if CXIMAGE_SUPPORT_MNG
else if (ext=="mng"||ext=="jng") type = CXIMAGE_FORMAT_MNG;
#endif
#if CXIMAGE_SUPPORT_ICO
else if (ext == "ico") type = CXIMAGE_FORMAT_ICO;
#endif
#if CXIMAGE_SUPPORT_TIF
else if (ext=="tiff"||ext=="tif") type = CXIMAGE_FORMAT_TIF;
#endif
#if CXIMAGE_SUPPORT_TGA
else if (ext=="tga") type = CXIMAGE_FORMAT_TGA;
#endif
#if CXIMAGE_SUPPORT_PCX
else if (ext=="pcx") type = CXIMAGE_FORMAT_PCX;
#endif
#if CXIMAGE_SUPPORT_WBMP
else if (ext=="wbmp") type = CXIMAGE_FORMAT_WBMP;
#endif
#if CXIMAGE_SUPPORT_WMF
else if (ext=="wmf"||ext=="emf") type = CXIMAGE_FORMAT_WMF;
#endif
#if CXIMAGE_SUPPORT_J2K
else if (ext=="j2k"||ext=="jp2") type = CXIMAGE_FORMAT_J2K;
#endif
#if CXIMAGE_SUPPORT_JBG
else if (ext=="jbg") type = CXIMAGE_FORMAT_JBG;
#endif
#if CXIMAGE_SUPPORT_JP2
else if (ext=="jp2"||ext=="j2k") type = CXIMAGE_FORMAT_JP2;
#endif
#if CXIMAGE_SUPPORT_JPC
else if (ext=="jpc"||ext=="j2c") type = CXIMAGE_FORMAT_JPC;
#endif
#if CXIMAGE_SUPPORT_PGX
else if (ext=="pgx") type = CXIMAGE_FORMAT_PGX;
#endif
#if CXIMAGE_SUPPORT_RAS
else if (ext=="ras") type = CXIMAGE_FORMAT_RAS;
#endif
#if CXIMAGE_SUPPORT_PNM
else if (ext=="pnm"||ext=="pgm"||ext=="ppm") type = CXIMAGE_FORMAT_PNM;
#endif
else type = CXIMAGE_FORMAT_UNKNOWN;
return true;
}