-
CxImage庫下載
官網地址:https://sourceforge.net/projects/cximage/
ps:自動下載可能有點慢,耐心等一下。若無反應點擊Problems Downloading?重新 選擇一個鏡像。 -
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即可。 -
圖片格式轉換
#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;
}
- 壓縮
壓縮方式: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);
- 截取
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);
- 調整文件大小
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);
最後附上他人代碼,稍稍改動了下。根據文件名轉換出圖片類型。
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;
}