日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++生成GIF小结

發(fā)布時(shí)間:2024/3/13 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++生成GIF小结 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?聲明:所有權(quán)利保留。

轉(zhuǎn)載必須說(shuō)明出處:http://blog.csdn.net/cartzhang/article/details/44020175


近來(lái)需要把BMPKinect的內(nèi)存圖片多張合成為小GIF圖片。找了找,東西不少,做個(gè)小結(jié),以備以后用到。

一、GIF.h

此方法很簡(jiǎn)單,就是一個(gè)頭文件。但是我沒(méi)有嘗試成功。可能的原因是我的BMP圖片的生成字節(jié)順序與GIF.H頭文件所要求的不一致。

Gif.h頭文件代碼如下:

?

使用方法如下:

[cpp]? view plain ?copy
  • //GifWriter?m_gif_writer;??
  • //char*?file_name?=?"E:\\aaa.gif";??
  • //int?width?=?128;??
  • //int?height?=?128;??
  • //int?delay?=?10;??
  • //GifBegin(&m_gif_writer,?file_name,?width,?height,?delay);??
  • //?代碼里面自動(dòng)從第一幀開(kāi)始。只第一幀添加GIF的頭信息??
  • //for?()??
  • //{??
  • GifWriteFrame()??
  • //}??
  • //GifEnd()??


  • 頭文件出處出處:作者:Charlie?Tangora ? ?

    github?地址:https://github.com/ginsweater/gif-h


    二、CXimage?庫(kù)

    此庫(kù)開(kāi)源,可隨便下載。

    使用下載的版本為702full版本。Vs2013編譯很順利,因?yàn)樾枰褂玫?/span>64位版本,所以使用了x64release模式。有個(gè)與mfc相關(guān)的編譯不過(guò),直接無(wú)視了,本人用不上mfc

    生成的為lib的靜態(tài)庫(kù)。

    我把所需要的頭文件和靜態(tài)庫(kù)拷貝的到自己建立的目錄下和各個(gè)對(duì)應(yīng)的文件夾下,如圖:

    ?


    Include?文件從CXimage中拷貝頭文件,lib庫(kù)文件為編譯后生成的x64文件里面的,包括Debug版本和Release版本。

    網(wǎng)上找了個(gè)代碼,對(duì)CXimageGIF寫(xiě)了兩個(gè)函數(shù)。本人在基礎(chǔ)上稍微添加和修改了代碼。

    其實(shí)主要是處理相關(guān)文件夾方便來(lái)調(diào)用的。非常感謝網(wǎng)友提供,頭文件和CPP文件如下:(文件出處為:http://blog.csdn.net/fengbingchun/article/details/43538081

    若有問(wèn)題,請(qǐng)隨時(shí)聯(lián)系,非常感謝!

    mGif.h頭文件:

    ?

    [cpp]? view plain ?copy
  • #pragma?once??
  • #ifndef?_MGIF_H__??
  • #define?_MGIF_H__??
  • ??
  • #include?<string>??
  • ??
  • using?namespace?std;??
  • ??
  • ??
  • void?decoding_gif(string?strGifName,?string?strSavePath);??
  • void?encoding_gif(string?strImgPath,?string?strGifName);??
  • ??
  • ??
  • #endif?//??

  • mGif.CPP文件:

    ?

    [cpp]? view plain ?copy
  • //Cartzhang??
  • #include?"mGif.h"??
  • ??
  • #include?"stdafx.h"??
  • #include?"mGif.h"??
  • #include?<iostream>??
  • #include?"ximagif.h"??
  • #include?<io.h>??
  • ??
  • using?namespace?std;??
  • ??
  • std::wstring?s2ws(const?std::string&?s)??
  • {??
  • ????int?len;??
  • ????int?slength?=?(int)s.length()?+?1;??
  • ????len?=?MultiByteToWideChar(CP_ACP,?0,?s.c_str(),?slength,?0,?0);??
  • ????wchar_t*?buf?=?new?wchar_t[len];??
  • ????MultiByteToWideChar(CP_ACP,?0,?s.c_str(),?slength,?buf,?len);??
  • ????std::wstring?r(buf);??
  • ????delete[]?buf;??
  • ????return?r;??
  • }??
  • ??
  • void?decoding_gif(string?strGifName,?string?strSavePath)??
  • {??
  • ????CxImage?img;??
  • ??
  • ????std::wstring?stemp?=?s2ws(strGifName);?//?Temporary?buffer?is?required??
  • ????LPCWSTR?PicName?=?stemp.c_str();??
  • ????img.Load(PicName,?CXIMAGE_FORMAT_GIF);??
  • ??
  • ????int?iNumFrames?=?img.GetNumFrames();??
  • ????cout?<<?"frames?num?=?"?<<?iNumFrames?<<?endl;??
  • ??
  • ????CxImage*?newImage?=?new?CxImage();??
  • ??
  • ????for?(int?i?=?0;?i?<?iNumFrames;?i++)?{??
  • ????????newImage->SetFrame(i);??
  • ????????newImage->Load(PicName,?CXIMAGE_FORMAT_GIF);??
  • ??
  • ????????char?tmp[64];??
  • ????????sprintf(tmp,?"%d",?i);??
  • ??
  • ????????string?tmp1;??
  • ????????tmp1?=?tmp1.insert(0,?tmp);??
  • ??
  • ????????tmp1?=?strSavePath?+?tmp1?+?".png";??
  • ????????stemp?=?s2ws(tmp1);?//?Temporary?buffer?is?required??
  • ????????PicName?=?stemp.c_str();??
  • ????????newImage->Save(PicName,?CXIMAGE_FORMAT_PNG);??
  • ????}??
  • ??
  • ????if?(newImage)?delete?newImage;??
  • }??
  • ??
  • int?TraverseFolder(const?string?strFilePath,?string?strImageNameSets[])??
  • {??
  • ????int?iImageCount?=?0;??
  • ??
  • ????_finddata_t?fileInfo;??
  • ??
  • ????long?handle?=?_findfirst(strFilePath.c_str(),?&fileInfo);??
  • ??
  • ????if?(handle?==?-1L)?{??
  • ????????cerr?<<?"failed?to?transfer?files"?<<?endl;??
  • ????????return?-1;??
  • ????}??
  • ??
  • ????do?{??
  • ????????//cout?<<?fileInfo.name?<<endl;??
  • ????????strImageNameSets[iImageCount]?=?(string)fileInfo.name;??
  • ??
  • ????????iImageCount++;??
  • ??
  • ????}?while?(_findnext(handle,?&fileInfo)?==?0);??
  • ??
  • ????return?iImageCount;??
  • }??
  • ??
  • void?encoding_gif(string?strImgPath,?string?strGifName)??
  • {??
  • ????string?strImgSets[100]?=?{};??
  • ??
  • ????int?iImgCount?=?TraverseFolder(strImgPath,?strImgSets);??
  • ??
  • ????string?strTmp?=?strImgPath.substr(0,?strImgPath.find_last_of("/")?+?1);??
  • ??
  • ????CxImage**?img?=?new?CxImage*[iImgCount];??
  • ????if?(img?==?NULL)?{??
  • ????????cout?<<?"new?Cximage?error!"?<<?endl;??
  • ????????return;??
  • ????}??
  • ????std::wstring?stemp;??
  • ????LPCWSTR?PicName;??
  • ????for?(int?i?=?0;?i?<?iImgCount;?i++)?{??
  • ????????string?tmp1;??
  • ????????tmp1?=?strTmp?+?strImgSets[i];??
  • ????????stemp?=?s2ws(tmp1);?//?Temporary?buffer?is?required??
  • ????????PicName?=?stemp.c_str();??
  • ????????img[i]?=?new?CxImage;??
  • ????????img[i]->Load(PicName,?CXIMAGE_FORMAT_BMP);??
  • ????????//bpp?=?1;??bpp?=?4;?????????????bpp?=?8;??
  • ????????if?(0?==?img[i]->GetNumColors())??
  • ????????{??
  • ????????????img[i]->DecreaseBpp(8,?true);??
  • ????????}?????????
  • ????}??
  • ??
  • ????CxIOFile?hFile;??
  • ????stemp?=?s2ws(strGifName);?//?Temporary?buffer?is?required??
  • ????PicName?=?stemp.c_str();??
  • ??
  • ????string?Method?=?"wb";??
  • ????std::wstring??stempmd?=?s2ws(Method);??
  • ????LPCWSTR?wMethod?=?stempmd.c_str();??
  • ????bool?BFlag?=?hFile.Open(PicName,?wMethod);??
  • ??
  • ????CxImageGIF?multiimage;??
  • ??
  • ????multiimage.SetLoops(-1);??
  • ????multiimage.SetFrameDelay(300);??
  • ????multiimage.SetDisposalMethod(2);??
  • ????multiimage.Encode(&hFile,?img,?iImgCount,?false,?false);??
  • ??
  • ????hFile.Close();??
  • ??
  • ????delete[]?img;??
  • }??

  • main測(cè)試代碼:

    ?

    [cpp]? view plain ?copy
  • string?strImgPath?=?"img/*.bmp";??
  • ??
  • string?strGifName?=?"img/test.gif";??
  • encoding_gif(strImgPath,?strGifName);??

  • 測(cè)試結(jié)果是可以生成gif圖片。再次表示感謝!


    中途有個(gè)事情說(shuō)下:在編譯測(cè)試的過(guò)程中有個(gè)錯(cuò)誤提示

    cximage.lib(ximapsd.obj) :?error LNK2001: 無(wú)法解析的外部符號(hào) _psd_image_free? cximage.lib(ximapsd.obj) : error LNK2019: 無(wú)法解析的外部符號(hào) _psd_main_loop
    解決方案: libdcr.lib libpsd.lib 將這兩個(gè)包括進(jìn)來(lái)就可以了。

    三、CreateGIF

    Csdn上資源:http://download.csdn.net/detail/iamshuke/2567835

    非常感謝!若有問(wèn)題,請(qǐng)隨時(shí)聯(lián)系。

    本程序是用基于MFC的,對(duì)于我來(lái)使用,我不用MFC

    其中重要的文件,其他的都是調(diào)用過(guò)程:

    主要函數(shù)貼下:

    ?

    ?

    [html]? view plain ?copy
  • BOOL?GetData(HBITMAP?hBmp,BYTE?**ppPalette,BYTE?**ppData,BYTE?*pBitsPixel,int?*pWidth,int?*pHeight);??
  • ??
  • void?CreateGIFHeard(CFile?&file,WORD?nImageWidth,WORD?nImageHeight,BYTE?bitsPixel);??
  • ??
  • void?AddImageToGIF(CFile?&file,BYTE?*pData,BYTE?*palette,WORD?nImageWidth,WORD?nImageHeight,BYTE?bitsPixel,WORD?nDelay,??
  • ???????????????????short?int?nTransparentColorIndex);??
  • ??
  • void?CloseGIF(CFile?&file);??
  • 總結(jié)

    以上是生活随笔為你收集整理的C++生成GIF小结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。