首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

[原创]利用LabWindows/CVI实现图像的压缩

[原创]利用LabWindows/CVI实现图像的压缩

我又找了一下我大约是在2003年末用过的一个控件,叫“ImageOcx”,是个商用控件,非常不错,我用它把一个BMP文件(1.4M)压缩到了10K左右,感觉还行,所以推荐给大家!
用到的控件函数为:
 
函数原型:
RESULT CVIFUNC IMAGEOCXLib__DImageOcxCompressImageOut (CAObjHandle objectHandle, ERRORINFO *errorInfo, const char *pFile1,
 const char *pFile2, float fTime, long lType, long *returnValue);
函数功能:压缩位图
函数说明:
     pFile1---------源文件
     pFile2---------压缩文件
     fTime----------倍率
     lType----------图像类型 (1=bmp 2=tif)
     returnValue----返回值
函数原型:
HRESULT CVIFUNC IMAGEOCXLib__DImageOcxDeCompressImageOut (CAObjHandle objectHandle,
 ERRORINFO *errorInfo, const char *pFile1, const char *pFile2,
 long *returnValue);
函数功能:还原位图
函数说明:
     pFile1---------压缩文件
     pFile2---------解压文件
     returnValue----返回值
界面为:
 
 
 
代码为:
 
 
#include "ImageOcx.h"
#include
#include "toolbox.h"
#include
#include   
#include
#include "图形压缩.h"
static CAObjHandle objecthandle;
static int bitmapID;
static int panelHandle;
int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "图形压缩.uir", PANEL)) < 0)
  return -1;
 
 GetObjHandleFromActiveXCtrl (panelHandle, PANEL_IMAGEOCX, &objecthandle);
 
 
 DisplayPanel (panelHandle);
 RunUserInterface ();
 DiscardPanel (panelHandle);
 
 if (bitmapID)
 {
  DiscardBitmap (bitmapID);
 }
 
 return 0;
}
int CVICALLBACK bmpCB (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int sel;
 char path[MAX_PATHNAME_LEN];
 long filesize;     
 
 switch (event)
 {
  case EVENT_COMMIT:
   sel = FileSelectPopup ("", "*.bmp", "*.bmp", "打开位图", VAL_LOAD_BUTTON, 0, 1, 1, 1, path);
   if (sel > 0)
   {
    
    GetBitmapFromFile (path, &bitmapID);
    
    
    CanvasDrawBitmap (panelHandle, PANEL_CANVAS_ORGINAL, bitmapID, VAL_ENTIRE_OBJECT, VAL_ENTIRE_OBJECT);
    
    GetFileSize (path, &filesize);  
    SetCtrlVal (panelHandle, PANEL_NUMERIC_BMP, filesize);
   }
   break;
 }
 return 0;
}
int CVICALLBACK jpegCB (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int sel;
 char path[MAX_PATHNAME_LEN];
 long filesize;    
 
 switch (event)
 {
  case EVENT_COMMIT:
   if (bitmapID)
   {
    int ration;
    GetCtrlVal (panelHandle, PANEL_NUMERIC_RATION, &ration);
    sel = FileSelectPopup ("", "*.jpeg", "*.jpeg", "保存JPG图像", VAL_SAVE_BUTTON, 0, 1, 1, 1, path);
    if (sel > 0)
    {
     SaveBitmapToJPEGFile (bitmapID, path, JPEG_PROGRESSIVE, ration);
     DisplayImageFile (panelHandle, PANEL_PICTURE_CONVERT, path);
   
     GetFileSize (path, &filesize);  
     SetCtrlVal (panelHandle, PANEL_NUMERIC_JPGE, filesize);
    }
   }
   break;
 }
 return 0;
}
int CVICALLBACK QuitCallback (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   QuitUserInterface (0);
   break;
 }
 return 0;
}
int CVICALLBACK compress (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int sel;
 char path[MAX_PATHNAME_LEN];
 long ret;
 int ration; 
 switch (event)
 {
  case EVENT_COMMIT:
   sel = FileSelectPopup ("", "*.bmp", "*.bmp", "打开位图", VAL_OK_BUTTON, 0, 1, 1, 1, path);
   if (sel > 0)
   {
    GetCtrlVal (panelHandle, PANEL_NUMERIC_COMRAT, &ration);
    IMAGEOCXLib__DImageOcxCompressImageOut (objecthandle, NULL, &path[0], "sample1.cmp", ration, 1, &ret);
    DisplayImageFile (panelHandle, PANEL_PICTURE_ORGINAL, path); 
   }
   break;
 }
 return 0;
}
int CVICALLBACK decompress (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int sel;
 char path[MAX_PATHNAME_LEN]; 
 long ret;
 switch (event)
 {
  case EVENT_COMMIT:
   sel = FileSelectPopup ("", "*.cmp", "*.cmp", "打开压缩位图", VAL_OK_BUTTON, 0, 1, 1, 1, path);
   if (sel > 0)
   {
    IMAGEOCXLib__DImageOcxDeCompressImageOut (objecthandle, NULL, &path[0], "3.bmp", &ret);
    DisplayImageFile (panelHandle, PANEL_PICTURE_COMMPRESS, "3.bmp"); 
   }
   break;
 }
 return 0;
}
int CVICALLBACK size (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int pheight;
 int width;
 int height;
 static int i = 0;
 switch (event)
 {
  case EVENT_COMMIT:
   if ( i == 0)
   {
    SetPanelAttribute (panelHandle, ATTR_HEIGHT, 700);
    SetCtrlAttribute (panelHandle, PANEL_CMD_SIZE, ATTR_LABEL_TEXT, "详细 <<");
    i = 1;
   }
   else
   {
    SetPanelAttribute (panelHandle, ATTR_HEIGHT, 480);
    SetCtrlAttribute (panelHandle, PANEL_CMD_SIZE, ATTR_LABEL_TEXT, "详细 >>");
    i = 0; 
   }
   
   GetScreenSize (&height, &width);
   GetPanelAttribute (panelHandle, ATTR_HEIGHT, &pheight);
   SetPanelAttribute (panelHandle, ATTR_TOP, (int)((height-pheight)/2)+15);
   break;
 }
 return 0;
}
 
转自:小信的博客
网址:http://blog.sina.com.cn/u/1265070197
返回列表