CCD camera类设计及简易实现

上一篇文章中介绍了关于CCD camera实现的一般流程CCD camera的一般控制流程及些许困惑,现在想用类来实现这个过程。该类设计以及实现的目的是用于相机拍照,成像,存储。目前仅考虑拍照与实时成像两个过程。由于从简单上进行设计,实现的一般流程中的一些步骤能够省略。具体代码如下:

   1: // ccd_class.h
   2: class CCDClass
   3: {
   4: private:
   5:     HANDLE      m_hCam;               // handle of camera
   6:     SHORT       m_sBufNr;
   7:     WORD       *m_imgBuf;             // store img from camera
   8:     WORD        m_wXResAct;           // x resolution of img
   9:     WORD        m_wYResAct;           // y resolution of img
  10:     DWORD       m_imgSize;            // x resolution * y resolution
  11:     HANDLE      m_hEvent;             // will be used in WaitForSingleObjects()
  12:     unsigned char *m_imgRGB;          // convert data in imgBuf to RGB
  13:     
  14: public:
  15:     CCDClass();
  16:     ~CCDClass();
  17:  
  18:     HANDLE GethEvent();
  19:     unsigned char* GetImgRGB();
  20:  
  21:     void Conv16to24();
  22:  
  23:     int OpenCamera();             // just open camera
  24:     int InitialCamera();          // allocate buffer and set recording on
  25:     int RunCamera();              // record img to allocated buffer
  26:     int StopCamera();             // set recording off
  27:     int CloseCamera();            // close camera and free pic buffer
  28: };

 

   1: // ccd_class.cpp
   2: #include "ccd_class.h"
   3:  
   4: CCDClass::CCDClass()
   5: {
   6:     // initial variables
   7:     m_hCam = NULL;
   8:     m_hEvent = NULL;
   9:     m_sBufNr = -1;
  10:     m_imgBuf = 0;
  11:     m_wXResAct = 1392;            // the default x resolution is 1392
  12:     m_wYResAct = 1040;            // the default y resolution is 1040
  13:     m_imgRGB = new unsigned char[1392*1040*3];
  14: }
  15:  
  16: CCDClass::~CCDClass()
  17: {
  18:     delete [] m_imgRGB;
  19:     imgRGB = 0;
  20: }
  21:  
  22: // Open camera
  23: int CCDClass::OpenCamera()
  24: {
  25:     PCO_OpenCamera(&m_hCam, 0);          // default open successfully
  26:     return 0;
  27: }
  28:  
  29: // allocate buffer and set recording on
  30: int CCDClass::InitialCamera()
  31: {
  32:     m_sBufNr = -1;            // mean the buffer will be a new buffer
  33:     PCO_ArmCamera(m_hCam);
  34:  
  35:     DWORD bufSize;            // size of buffer in byte
  36:     int errCode;
  37:  
  38:     m_imgSize = m_wXResAct * m_wYResAct;
  39:     bufSize = 2*m_imgSize;
  40:     m_imgBuf = new WORD[m_imgSize];
  41:  
  42:     if (PCO_AllocateBuffer(m_hCam, &m_sBufNr, m_bufSize, &m_imgBuf, &m_hEvent) != 0) {
  43:         errCode = CloseCamera();
  44:         return errCode;
  45:     }
  46:  
  47:     if (PCO_SetRecordingState(m_hCam, 0x0001) != 0) {
  48:         errCode = CloseCamera();
  49:         return errCode;
  50:     }
  51:     
  52:     return 0;
  53: }
  54:  
  55: // record img in allocated buffer
  56: int CCDClass::RunCamera()
  57: {
  58:     if (PCO_AddBufferEx(m_hCam, 0, 0, m_sBufNr, m_wXResAct,
  59:                         m_wYResAct, 14) != 0) {    // 14 is bytes in one pixel
  60:         wxMessageBox(wxT("PCO_AddBufferEx"));
  61:         errCode = CloseCamera();
  62:         return errCode;
  63:     }
  64:     
  65:     return 0;
  66: }
  67:  
  68: // set recording off
  69: int CCDClass::StopCamera()
  70: {
  71:     if (PCO_SetRecordingState(m_hCam, 0x0000) != 0) {
  72:         errCode = CloseCamera();
  73:         return errCode;
  74:     }
  75:     
  76:     return 0;
  77: }
  78:  
  79: // close camera and free img buffer
  80: int CCDClass::CloseCamera()
  81: {
  82:     if (m_sBufNr >= 0) {
  83:         PCO_FreeBuffer(m_hCam, m_sBufNr);
  84:     }
  85:  
  86:     if (m_hCam != NULL) {
  87:         PCO_CloseCamera(m_hCam);
  88:     }
  89:     delete [] m_imgBuf;
  90:     m_imgBuf = 0;
  91:     return 999;
  92: }
  93:  
  94: // convert 16bit to 24bit rgb img
  95: void CCDClass::Conv16to24()
  96: {
  97:     unsigned char *m_cData = new unsigned char[m_imgSize];
  98:     
  99:     // convert 16bit img to 8bit
 100:     for (size_t i = 0; i < m_imgSize; ++i) {
 101:         m_cData[i] = (unsigned char)(m_imgBuf[i] >> 6);
 102:     }
 103:     
 104:     unsigned char *ptr1 = m_cData;
 105:     unsigned char *ptr2 = m_imgRBG;
 106:     for (size_t i = 0; i < m_imgSize; ++i) {
 107:         *ptr2++ = *ptr1;
 108:         *ptr2++ = *ptr1;
 109:         *ptr2++ = *ptr1++;
 110:     }
 111:     
 112:     delete [] m_cData;
 113:     m_cData = 0;
 114: }
 115:  
 116: HANDLE CCDClass::GethEvent()
 117: {
 118:     return m_hEvent;
 119: }
 120:  
 121:  
 122: unsigned char* CCDClass::GetImgRGB()
 123: {
 124:     return m_imgRGB;
 125: }

将函数RunCamera单独起来主要是为了实现实时成像。

不过还不清楚如何进行实时成像,采用onTimer吗?

posted on 2012-07-04 11:16 钟谢伟 阅读(1012) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2012年7月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿(1)

随笔档案

IT网站

My Friends

搜索

最新评论

阅读排行榜

评论排行榜