1. 程式人生 > >IJL庫之JPEG圖片壓縮

IJL庫之JPEG圖片壓縮

長度 def 釋放 dll rom tmp 一級目錄 加載 ever

  如何將比較大的圖片壓縮成比較小的圖片,通常在相機一直拍圖且需要將圖片網絡傳輸時,必須壓縮圖片。相機一般幾十FPS,每張幾M,只能用JPEG有順壓縮才能到可以實時傳輸要求。 還有就是這種特定情況壓縮需要保證 壓縮率 與 壓縮速度,那就是用Intel的IJL JPEG壓縮庫,網上有IJL15.DLL IJL20.DLL,15版本存在內存泄漏,用20版本即可。

  例子:

exe執行程序 , ijl20x64.dll 放在同一級目錄下。

  typedef IJLERR (*pInitFunc)(JPEG_CORE_PROPERTIES*); // ijlInit函數類型


  typedef IJLERR (*pWriteFunc)(JPEG_CORE_PROPERTIES*,IJLIOTYPE); // ijlWrite函數類型
  typedef IJLERR (*pFreeFunc)(JPEG_CORE_PROPERTIES*); // ijlFree函數類型

  pInitFunc funcInit = NULL; // ijlInit 函數指針
  pWriteFunc funcWrite = NULL; // ijlWrite 函數指針
  pFreeFunc funcFree = NULL; // ijlFree 函數指針

  char lpExeName[512] = {0};
  HMODULE h = GetModuleHandle( NULL );
  ::GetModuleFileName( h,lpExeName,512 );
  CString strExeName(lpExeName);
  int nPos = strExeName.ReverseFind( ‘\\‘ );
  CString strLeft,slibLeft,sDllName;
  strLeft = strExeName.Left(nPos);
  int n = strLeft.ReverseFind(‘\\‘);
  slibLeft = strLeft.Left(n);


  sDllName = slibLeft + "\\lib\\ijl20x64.dll" ; // ijl20x64.dll 的全路徑

  m_hIjlModule = LoadLibrary(sDllName); // loadlibrary 

  // 加載IJL庫的函數
  funcInit = (pInitFunc)GetProcAddress(m_hIjlModule,"ijlInit");
  funcWrite = (pWriteFunc)GetProcAddress(m_hIjlModule,"ijlWrite");
  funcFree = (pFreeFunc)GetProcAddress(m_hIjlModule,"ijlFree");

  // 設置圖片參數 

  JPEG_CORE_PROPERTIES image;
  IJLERR err;
  IJLIOTYPE iotype = IJL_JBUFF_WRITEWHOLEIMAGE;

  // JPEG 壓縮 Basler 4096*2048 的單通道黑白圖

  unsigned char* saveBasler = (unsigned char*)malloc(4096*2048);
  ZeroMemory(saveBasler,4096*2048);
  ZeroMemory( &image, sizeof(JPEG_CORE_PROPERTIES));
  funcInit( &image ); // 此處會開辟內存,後面對應要用funFree釋放
  image.DIBWidth = 4096;
  image.DIBHeight = 2048;
  image.JPGBytes = (unsigned char*)saveBasler;
  image.JPGSizeBytes = 8388608;     // 4096*2048
  image.JPGWidth = 4096;
  image.JPGHeight = 2048;
  image.jquality = 90;
  image.DIBColor = IJL_G;
  image.DIBChannels = 1; // 黑白色,如果是RGB真彩色,channel=3 圖片SIZE = width*height*3
  image.DIBPadBytes = IJL_DIB_PAD_BYTES(image.DIBWidth,1);
  image.JPGColor = IJL_G;
  image.JPGChannels = 1;
  image.JPGSubsampling = IJL_NONE;
  image.DIBBytes = tmpBasler;
  err = funcWrite (&image, iotype);

  int nJpgLen = image.JPGSizeBytes; // 壓縮完成後 JPEG圖片的大小, 壓縮後數據在saveBasler 裏面,直接將saveBasler 寫入一個JPG文件即可,長度為nJpgLen.

  funcFree(&image); // 釋放內存

IJL庫之JPEG圖片壓縮