zoukankan      html  css  js  c++  java
  • cxImageUser

    #pragma once
    #include <string>
    #include "ximage.h"
    #include "ximagif.h"
    #include "xiofile.h"
    
    // TODO: reference any additional headers you need in STDAFX.H
    // and not in this file
    
    #ifdef _DEBUG
    #pragma comment(lib, "cximage//db//cximage.lib")
    #pragma comment(lib, "cximage//db//jasper.lib")
    #pragma comment(lib, "cximage//db//jbig.lib")
    #pragma comment(lib, "cximage//db//jpeg.lib")
    #pragma comment(lib, "cximage//db//libdcr.lib")
    #pragma comment(lib, "cximage//db//libpsd.lib")
    #pragma comment(lib, "cximage//db//mng.lib")
    #pragma comment(lib, "cximage//db//png.lib")
    #pragma comment(lib, "cximage//db//tiff.lib")
    #pragma comment(lib, "cximage//db//zlib.lib")
    #else
    #pragma comment(lib, "cximage//cximage.lib") 
    #pragma comment(lib, "cximage//jasper.lib")
    #pragma comment(lib, "cximage//jbig.lib")
    #pragma comment(lib, "cximage//jpeg.lib")
    #pragma comment(lib, "cximage//libdcr.lib")
    #pragma comment(lib, "cximage//libpsd.lib")
    #pragma comment(lib, "cximage//mng.lib")
    #pragma comment(lib, "cximage//png.lib")
    #pragma comment(lib, "cximage//tiff.lib")
    #pragma comment(lib, "cximage//zlib.lib")
    #endif
    
    class CxImageUser
    {
    public:
        CxImageUser();
        ~CxImageUser();
    
        static bool gif2jpg(const std::wstring& strSrcFile, const std::wstring& strDstFile);
        static bool png2jpg(const std::wstring& strSrcFile, const std::wstring& strDstFile);
        static bool ResizeGif(const std::wstring& strSrcFile, const std::wstring& strDstFile, const int& nWidth, const int& nHeight);
    };
    #include "cxImageUser.h"
    #include "StdStrFile.h"
    
    bool CxImageUser::gif2jpg(const std::wstring& strSrcFile, const std::wstring& strDstFile)
    {
        CxImage  image; 
        //gif -> jpg 
        image.Load(strSrcFile.c_str(), CXIMAGE_SUPPORT_GIF); 
        if (image.IsValid())
        { 
            if(!image.IsGrayScale()) image.IncreaseBpp(24); 
            image.SetJpegQuality(80); 
            image.Save(strDstFile.c_str(),CXIMAGE_FORMAT_JPG); 
        }
    
        return true;
    }
    
    bool CxImageUser::png2jpg( const std::wstring& strSrcFile, const std::wstring& strDstFile )
    {
        CxImage  image; 
        //png -> jpg 
        image.Load(strSrcFile.c_str(), CXIMAGE_FORMAT_PNG); 
        if (image.IsValid()){ 
            if(!image.IsGrayScale()) image.IncreaseBpp(24); 
            image.SetJpegQuality(80); 
            image.Save(strDstFile.c_str(),CXIMAGE_FORMAT_JPG); 
        }
        image.GetExifInfo();
    
        return true;
    }
    
    bool CxImageUser::ResizeGif(const std::wstring& strSrcFile, const std::wstring& strDstFile, const int& nWidth, const int& nHeight)
    {
    #if CXIMAGE_SUPPORT_DECODE && CXIMAGE_SUPPORT_ENCODE && CXIMAGE_SUPPORT_GIF
        CxImage img;
        img.Load(strSrcFile.c_str(), CXIMAGE_FORMAT_GIF);
    
        if (!img.IsValid())
        {
            return false;
        }
    
        //这样做只能保留第一帧
    //     img.Resample(nWidth, nHeight);
    //     img.Save(strDstFile.c_str(), CXIMAGE_FORMAT_GIF);
    
        int iNumFrames = img.GetNumFrames();
        CxImage** imgSave = new CxImage*[iNumFrames];
    
        for (int i = 0; i < iNumFrames; i++)
        {
            CxImage* newImage = new CxImage();
            newImage->SetFrame(i);
            newImage->Load(strSrcFile.c_str(), CXIMAGE_FORMAT_GIF);
            newImage->Resample(nWidth, nHeight);
            if (0 == newImage->GetNumColors())
            {
                imgSave[i]->DecreaseBpp(8, true);
            }
            imgSave[i] = newImage;
        }
    
        CxIOFile hFile;
        std::string Method = "wb";
        std::wstring  stempmd = CStdStr::s2ws(Method);
        LPCWSTR wMethod = stempmd.c_str();
        bool BFlag = hFile.Open(strDstFile.c_str(), wMethod);
    
        CxImageGIF multiimage;
    
        multiimage.SetLoops(-1);
        multiimage.SetFrameDelay(img.GetFrameDelay());
        multiimage.SetDisposalMethod(img.GetDisposalMethod());
        multiimage.Encode(&hFile, imgSave, iNumFrames, false, false);
    
        hFile.Close();
    
        //释放资源
        for (int i = 0; i < iNumFrames; ++i)
        {
            if (imgSave[i])
            {
                delete imgSave[i];
                imgSave[i] = nullptr;
            }
        }
        if (imgSave)
        {
            delete[] imgSave;
            imgSave = nullptr;
        }
        
    #endif
    
        return true;
    }
  • 相关阅读:
    POJ 2187 Beauty Contest(凸包+旋转卡壳)
    POJ 3845 Fractal(计算几何の旋转缩放)
    POJ 1755 Triathlon(线性规划の半平面交)
    POJ 2540 Hotter Colder(半平面交)
    POJ 3525/UVA 1396 Most Distant Point from the Sea(二分+半平面交)
    POJ 3348 Cows(凸包+多边形面积)
    POJ 1228 Grandpa's Estate(凸包唯一性判断)
    POJ 2826 An Easy Problem?!(线段交点+简单计算)
    如何在ARC代码中混编非ARC代码
    给view 添加事件
  • 原文地址:https://www.cnblogs.com/autumoonchina/p/7065290.html
Copyright © 2011-2022 走看看