声明:所有权利保留。

转载必须说明出处:http://blog.csdn.net/cartzhang/article/details/44020175


近来需要把BMPKinect的内存图片多张合成为小GIF图片。找了找,东西不少,做个小结,以备以后用到。

一、GIF.h

此方法很简单,就是一个头文件。但是我没有尝试成功。可能的原因是我的BMP图片的生成字节顺序与GIF.H头文件所要求的不一致。

Gif.h头文件代码如下:

 

使用方法如下:

[cpp] view plain copy
  1. //GifWriter m_gif_writer;  
  2. //char* file_name = "E:\\aaa.gif";  
  3. //int width = 128;  
  4. //int height = 128;  
  5. //int delay = 10;  
  6. //GifBegin(&m_gif_writer, file_name, width, height, delay);  
  7. // 代码里面自动从第一帧开始。只第一帧添加GIF的头信息  
  8. //for ()  
  9. //{  
  10. ////GifWriteFrame()  
  11. //}  
  12. //GifEnd()  


头文件出处出处:作者:Charlie Tangora    

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


二、CXimage 

此库开源,可随便下载。

使用下载的版本为702full版本。Vs2013编译很顺利,因为需要使用的64位版本,所以使用了x64release模式。有个与mfc相关的编译不过,直接无视了,本人用不上mfc

生成的为lib的静态库。

我把所需要的头文件和静态库拷贝的到自己建立的目录下和各个对应的文件夹下,如图:

 C++生成GIF小结


Include 文件从CXimage中拷贝头文件,lib库文件为编译后生成的x64文件里面的,包括Debug版本和Release版本。

网上找了个代码,对CXimageGIF写了两个函数。本人在基础上稍微添加和修改了代码。

其实主要是处理相关文件夹方便来调用的。非常感谢网友提供,头文件和CPP文件如下:(文件出处为:http://blog.csdn.net/fengbingchun/article/details/43538081

若有问题,请随时联系,非常感谢!

mGif.h头文件:

 

[cpp] view plain copy
  1. #pragma once  
  2. #ifndef _MGIF_H__  
  3. #define _MGIF_H__  
  4.   
  5. #include <string>  
  6.   
  7. using namespace std;  
  8.   
  9.   
  10. void decoding_gif(string strGifName, string strSavePath);  
  11. void encoding_gif(string strImgPath, string strGifName);  
  12.   
  13.   
  14. #endif //  

mGif.CPP文件:

 

[cpp] view plain copy
  1. //Cartzhang  
  2. #include "mGif.h"  
  3.   
  4. #include "stdafx.h"  
  5. #include "mGif.h"  
  6. #include <iostream>  
  7. #include "ximagif.h"  
  8. #include <io.h>  
  9.   
  10. using namespace std;  
  11.   
  12. std::wstring s2ws(const std::string& s)  
  13. {  
  14.     int len;  
  15.     int slength = (int)s.length() + 1;  
  16.     len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);  
  17.     wchar_t* buf = new wchar_t[len];  
  18.     MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);  
  19.     std::wstring r(buf);  
  20.     delete[] buf;  
  21.     return r;  
  22. }  
  23.   
  24. void decoding_gif(string strGifName, string strSavePath)  
  25. {  
  26.     CxImage img;  
  27.   
  28.     std::wstring stemp = s2ws(strGifName); // Temporary buffer is required  
  29.     LPCWSTR PicName = stemp.c_str();  
  30.     img.Load(PicName, CXIMAGE_FORMAT_GIF);  
  31.   
  32.     int iNumFrames = img.GetNumFrames();  
  33.     cout << "frames num = " << iNumFrames << endl;  
  34.   
  35.     CxImage* newImage = new CxImage();  
  36.   
  37.     for (int i = 0; i < iNumFrames; i++) {  
  38.         newImage->SetFrame(i);  
  39.         newImage->Load(PicName, CXIMAGE_FORMAT_GIF);  
  40.   
  41.         char tmp[64];  
  42.         sprintf(tmp, "%d", i);  
  43.   
  44.         string tmp1;  
  45.         tmp1 = tmp1.insert(0, tmp);  
  46.   
  47.         tmp1 = strSavePath + tmp1 + ".png";  
  48.         stemp = s2ws(tmp1); // Temporary buffer is required  
  49.         PicName = stemp.c_str();  
  50.         newImage->Save(PicName, CXIMAGE_FORMAT_PNG);  
  51.     }  
  52.   
  53.     if (newImage) delete newImage;  
  54. }  
  55.   
  56. int TraverseFolder(const string strFilePath, string strImageNameSets[])  
  57. {  
  58.     int iImageCount = 0;  
  59.   
  60.     _finddata_t fileInfo;  
  61.   
  62.     long handle = _findfirst(strFilePath.c_str(), &fileInfo);  
  63.   
  64.     if (handle == -1L) {  
  65.         cerr << "failed to transfer files" << endl;  
  66.         return -1;  
  67.     }  
  68.   
  69.     do {  
  70.         //cout << fileInfo.name <<endl;  
  71.         strImageNameSets[iImageCount] = (string)fileInfo.name;  
  72.   
  73.         iImageCount++;  
  74.   
  75.     } while (_findnext(handle, &fileInfo) == 0);  
  76.   
  77.     return iImageCount;  
  78. }  
  79.   
  80. void encoding_gif(string strImgPath, string strGifName)  
  81. {  
  82.     string strImgSets[100] = {};  
  83.   
  84.     int iImgCount = TraverseFolder(strImgPath, strImgSets);  
  85.   
  86.     string strTmp = strImgPath.substr(0, strImgPath.find_last_of("/") + 1);  
  87.   
  88.     CxImage** img = new CxImage*[iImgCount];  
  89.     if (img == NULL) {  
  90.         cout << "new Cximage error!" << endl;  
  91.         return;  
  92.     }  
  93.     std::wstring stemp;  
  94.     LPCWSTR PicName;  
  95.     for (int i = 0; i < iImgCount; i++) {  
  96.         string tmp1;  
  97.         tmp1 = strTmp + strImgSets[i];  
  98.         stemp = s2ws(tmp1); // Temporary buffer is required  
  99.         PicName = stemp.c_str();  
  100.         img[i] = new CxImage;  
  101.         img[i]->Load(PicName, CXIMAGE_FORMAT_BMP);  
  102.         //bpp = 1;  bpp = 4;             bpp = 8;  
  103.         if (0 == img[i]->GetNumColors())  
  104.         {  
  105.             img[i]->DecreaseBpp(8, true);  
  106.         }         
  107.     }  
  108.   
  109.     CxIOFile hFile;  
  110.     stemp = s2ws(strGifName); // Temporary buffer is required  
  111.     PicName = stemp.c_str();  
  112.   
  113.     string Method = "wb";  
  114.     std::wstring  stempmd = s2ws(Method);  
  115.     LPCWSTR wMethod = stempmd.c_str();  
  116.     bool BFlag = hFile.Open(PicName, wMethod);  
  117.   
  118.     CxImageGIF multiimage;  
  119.   
  120.     multiimage.SetLoops(-1);  
  121.     multiimage.SetFrameDelay(300);  
  122.     multiimage.SetDisposalMethod(2);  
  123.     multiimage.Encode(&hFile, img, iImgCount, falsefalse);  
  124.   
  125.     hFile.Close();  
  126.   
  127.     delete[] img;  
  128. }  

main测试代码:

 

[cpp] view plain copy
  1. string strImgPath = "img/*.bmp";  
  2.   
  3. string strGifName = "img/test.gif";  
  4. encoding_gif(strImgPath, strGifName);  

测试结果是可以生成gif图片。再次表示感谢!


中途有个事情说下:在编译测试的过程中有个错误提示

 
cximage.lib(ximapsd.obj) : error LNK2001: 无法解析的外部符号 _psd_image_free 
cximage.lib(ximapsd.obj) : error LNK2019: 无法解析的外部符号 _psd_main_loop
解决方案:
libdcr.lib
libpsd.lib
将这两个包括进来就可以了。

三、CreateGIF

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

非常感谢!若有问题,请随时联系。

本程序是用基于MFC的,对于我来使用,我不用MFC

其中重要的文件,其他的都是调用过程:

主要函数贴下:

 

 

[html] view plain copy
  1. BOOL GetData(HBITMAP hBmp,BYTE **ppPalette,BYTE **ppData,BYTE *pBitsPixel,int *pWidth,int *pHeight);  
  2.   
  3. void CreateGIFHeard(CFile &file,WORD nImageWidth,WORD nImageHeight,BYTE bitsPixel);  
  4.   
  5. void AddImageToGIF(CFile &file,BYTE *pData,BYTE *palette,WORD nImageWidth,WORD nImageHeight,BYTE bitsPixel,WORD nDelay,  
  6.                    short int nTransparentColorIndex);  
  7.   
  8. void CloseGIF(CFile &file);  

 声明:所有权利保留。

转载必须说明出处:http://blog.csdn.net/cartzhang/article/details/44020175


近来需要把BMPKinect的内存图片多张合成为小GIF图片。找了找,东西不少,做个小结,以备以后用到。

一、GIF.h

此方法很简单,就是一个头文件。但是我没有尝试成功。可能的原因是我的BMP图片的生成字节顺序与GIF.H头文件所要求的不一致。

Gif.h头文件代码如下:

 

使用方法如下:

[cpp] view plain copy
  1. //GifWriter m_gif_writer;  
  2. //char* file_name = "E:\\aaa.gif";  
  3. //int width = 128;  
  4. //int height = 128;  
  5. //int delay = 10;  
  6. //GifBegin(&m_gif_writer, file_name, width, height, delay);  
  7. // 代码里面自动从第一帧开始。只第一帧添加GIF的头信息  
  8. //for ()  
  9. //{  
  10. ////GifWriteFrame()  
  11. //}  
  12. //GifEnd()  


头文件出处出处:作者:Charlie Tangora    

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


二、CXimage 

此库开源,可随便下载。

使用下载的版本为702full版本。Vs2013编译很顺利,因为需要使用的64位版本,所以使用了x64release模式。有个与mfc相关的编译不过,直接无视了,本人用不上mfc

生成的为lib的静态库。

我把所需要的头文件和静态库拷贝的到自己建立的目录下和各个对应的文件夹下,如图:

 C++生成GIF小结


Include 文件从CXimage中拷贝头文件,lib库文件为编译后生成的x64文件里面的,包括Debug版本和Release版本。

网上找了个代码,对CXimageGIF写了两个函数。本人在基础上稍微添加和修改了代码。

其实主要是处理相关文件夹方便来调用的。非常感谢网友提供,头文件和CPP文件如下:(文件出处为:http://blog.csdn.net/fengbingchun/article/details/43538081

若有问题,请随时联系,非常感谢!

mGif.h头文件:

 

[cpp] view plain copy
  1. #pragma once  
  2. #ifndef _MGIF_H__  
  3. #define _MGIF_H__  
  4.   
  5. #include <string>  
  6.   
  7. using namespace std;  
  8.   
  9.   
  10. void decoding_gif(string strGifName, string strSavePath);  
  11. void encoding_gif(string strImgPath, string strGifName);  
  12.   
  13.   
  14. #endif //  

mGif.CPP文件:

 

[cpp] view plain copy
  1. //Cartzhang  
  2. #include "mGif.h"  
  3.   
  4. #include "stdafx.h"  
  5. #include "mGif.h"  
  6. #include <iostream>  
  7. #include "ximagif.h"  
  8. #include <io.h>  
  9.   
  10. using namespace std;  
  11.   
  12. std::wstring s2ws(const std::string& s)  
  13. {  
  14.     int len;  
  15.     int slength = (int)s.length() + 1;  
  16.     len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);  
  17.     wchar_t* buf = new wchar_t[len];  
  18.     MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);  
  19.     std::wstring r(buf);  
  20.     delete[] buf;  
  21.     return r;  
  22. }  
  23.   
  24. void decoding_gif(string strGifName, string strSavePath)  
  25. {  
  26.     CxImage img;  
  27.   
  28.     std::wstring stemp = s2ws(strGifName); // Temporary buffer is required  
  29.     LPCWSTR PicName = stemp.c_str();  
  30.     img.Load(PicName, CXIMAGE_FORMAT_GIF);  
  31.   
  32.     int iNumFrames = img.GetNumFrames();  
  33.     cout << "frames num = " << iNumFrames << endl;  
  34.   
  35.     CxImage* newImage = new CxImage();  
  36.   
  37.     for (int i = 0; i < iNumFrames; i++) {  
  38.         newImage->SetFrame(i);  
  39.         newImage->Load(PicName, CXIMAGE_FORMAT_GIF);  
  40.   
  41.         char tmp[64];  
  42.         sprintf(tmp, "%d", i);  
  43.   
  44.         string tmp1;  
  45.         tmp1 = tmp1.insert(0, tmp);  
  46.   
  47.         tmp1 = strSavePath + tmp1 + ".png";  
  48.         stemp = s2ws(tmp1); // Temporary buffer is required  
  49.         PicName = stemp.c_str();  
  50.         newImage->Save(PicName, CXIMAGE_FORMAT_PNG);  
  51.     }  
  52.   
  53.     if (newImage) delete newImage;  
  54. }  
  55.   
  56. int TraverseFolder(const string strFilePath, string strImageNameSets[])  
  57. {  
  58.     int iImageCount = 0;  
  59.   
  60.     _finddata_t fileInfo;  
  61.   
  62.     long handle = _findfirst(strFilePath.c_str(), &fileInfo);  
  63.   
  64.     if (handle == -1L) {  
  65.         cerr << "failed to transfer files" << endl;  
  66.         return -1;  
  67.     }  
  68.   
  69.     do {  
  70.         //cout << fileInfo.name <<endl;  
  71.         strImageNameSets[iImageCount] = (string)fileInfo.name;  
  72.   
  73.         iImageCount++;  
  74.   
  75.     } while (_findnext(handle, &fileInfo) == 0);  
  76.   
  77.     return iImageCount;  
  78. }  
  79.   
  80. void encoding_gif(string strImgPath, string strGifName)  
  81. {  
  82.     string strImgSets[100] = {};  
  83.   
  84.     int iImgCount = TraverseFolder(strImgPath, strImgSets);  
  85.   
  86.     string strTmp = strImgPath.substr(0, strImgPath.find_last_of("/") + 1);  
  87.   
  88.     CxImage** img = new CxImage*[iImgCount];  
  89.     if (img == NULL) {  
  90.         cout << "new Cximage error!" << endl;  
  91.         return;  
  92.     }  
  93.     std::wstring stemp;  
  94.     LPCWSTR PicName;  
  95.     for (int i = 0; i < iImgCount; i++) {  
  96.         string tmp1;  
  97.         tmp1 = strTmp + strImgSets[i];  
  98.         stemp = s2ws(tmp1); // Temporary buffer is required  
  99.         PicName = stemp.c_str();  
  100.         img[i] = new CxImage;  
  101.         img[i]->Load(PicName, CXIMAGE_FORMAT_BMP);  
  102.         //bpp = 1;  bpp = 4;             bpp = 8;  
  103.         if (0 == img[i]->GetNumColors())  
  104.         {  
  105.             img[i]->DecreaseBpp(8, true);  
  106.         }         
  107.     }  
  108.   
  109.     CxIOFile hFile;  
  110.     stemp = s2ws(strGifName); // Temporary buffer is required  
  111.     PicName = stemp.c_str();  
  112.   
  113.     string Method = "wb";  
  114.     std::wstring  stempmd = s2ws(Method);  
  115.     LPCWSTR wMethod = stempmd.c_str();  
  116.     bool BFlag = hFile.Open(PicName, wMethod);  
  117.   
  118.     CxImageGIF multiimage;  
  119.   
  120.     multiimage.SetLoops(-1);  
  121.     multiimage.SetFrameDelay(300);  
  122.     multiimage.SetDisposalMethod(2);  
  123.     multiimage.Encode(&hFile, img, iImgCount, falsefalse);  
  124.   
  125.     hFile.Close();  
  126.   
  127.     delete[] img;  
  128. }  

main测试代码:

 

[cpp] view plain copy
  1. string strImgPath = "img/*.bmp";  
  2.   
  3. string strGifName = "img/test.gif";  
  4. encoding_gif(strImgPath, strGifName);  

测试结果是可以生成gif图片。再次表示感谢!


中途有个事情说下:在编译测试的过程中有个错误提示

 
cximage.lib(ximapsd.obj) : error LNK2001: 无法解析的外部符号 _psd_image_free 
cximage.lib(ximapsd.obj) : error LNK2019: 无法解析的外部符号 _psd_main_loop
解决方案:
libdcr.lib
libpsd.lib
将这两个包括进来就可以了。

三、CreateGIF

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

非常感谢!若有问题,请随时联系。

本程序是用基于MFC的,对于我来使用,我不用MFC

其中重要的文件,其他的都是调用过程:

主要函数贴下:

 

 

[html] view plain copy
  1. BOOL GetData(HBITMAP hBmp,BYTE **ppPalette,BYTE **ppData,BYTE *pBitsPixel,int *pWidth,int *pHeight);  
  2.   
  3. void CreateGIFHeard(CFile &file,WORD nImageWidth,WORD nImageHeight,BYTE bitsPixel);  
  4.   
  5. void AddImageToGIF(CFile &file,BYTE *pData,BYTE *palette,WORD nImageWidth,WORD nImageHeight,BYTE bitsPixel,WORD nDelay,  
  6.                    short int nTransparentColorIndex);  
  7.   
  8. void CloseGIF(CFile &file);  

相关文章:

  • 2021-11-06
  • 2022-02-25
  • 2022-12-23
  • 2022-12-23
  • 2021-07-23
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-01
  • 2021-06-22
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-05-18
相关资源
相似解决方案