USBCameraAcquire.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include "USBCameraAcquire.h"
00011 #include <iostream>
00012 using namespace std;
00013 
00014 // Module specification
00015 // <rtc-template block="module_spec">
00016 static const char* usbcameraacquire_spec[] =
00017   {
00018     "implementation_id", "USBCameraAcquire",
00019     "type_name",         "USBCameraAcquire",
00020     "description",       "USB Camera Acquire component",
00021     "version",           "1.0",
00022     "vendor",            "Noriaki Ando, AIST",
00023     "category",          "example",
00024     "activity_type",     "DataFlowComponent",
00025     "max_instance",      "10",
00026     "language",          "C++",
00027     "lang_type",         "compile",
00028     ""
00029   };
00030 // </rtc-template>
00031 
00032 USBCameraAcquire::USBCameraAcquire(RTC::Manager* manager)
00033   : RTC::DataFlowComponentBase(manager),
00034     // <rtc-template block="initializer">
00035     m_outOut("out", m_out),
00036     // </rtc-template>
00037     dummy(0)
00038 {
00039   // Registration: InPort/OutPort/Service
00040   // <rtc-template block="registration">
00041   // Set InPort buffers
00042   
00043   // Set OutPort buffer
00044   registerOutPort("out", m_outOut);
00045   //registerOutPort("OutPortPicture", mOutPortPicture);
00046   
00047   // Set service provider to Ports
00048   
00049   // Set service consumers to Ports
00050   
00051   // Set CORBA Service Ports
00052   
00053   // </rtc-template>
00054   
00055 }
00056 
00057 USBCameraAcquire::~USBCameraAcquire()
00058 {
00059 }
00060 /*
00061   RTC::ReturnCode_t USBCameraAcquire::onInitialize()
00062   {
00063   
00064   return RTC::RTC_OK;
00065   }
00066 */
00067 
00068 RTC::ReturnCode_t USBCameraAcquire::onFinalize()
00069 {       
00070   return RTC::RTC_OK;
00071 }
00072 
00073 /*
00074   RTC::ReturnCode_t USBCameraAcquire::onStartup(RTC::UniqueId ec_id)
00075   {
00076   
00077   }
00078 */
00079 
00080 /*
00081   RTC::ReturnCode_t USBCameraAcquire::onShutdown(RTC::UniqueId ec_id)
00082   {
00083   return RTC::OK;
00084   }
00085 */
00086 
00087 RTC::ReturnCode_t USBCameraAcquire::onActivated(RTC::UniqueId ec_id)
00088 {
00089   //カメラデバイスの探索
00090   if(NULL==(m_capture = cvCreateCameraCapture(CV_CAP_ANY))){
00091     cout<<"カメラがみつかりません"<<endl;
00092     return RTC::RTC_ERROR;
00093   }
00094   return RTC::RTC_OK;
00095 }
00096 
00097 
00098 
00099 RTC::ReturnCode_t USBCameraAcquire::onDeactivated(RTC::UniqueId ec_id)
00100 {
00101   //カメラ用メモリの解放
00102   cvReleaseCapture(&m_capture);
00103   return RTC::RTC_OK;
00104 }
00105 
00106 
00107 
00108 RTC::ReturnCode_t USBCameraAcquire::onExecute(RTC::UniqueId ec_id)
00109 {
00110   static coil::TimeValue tm_pre;
00111   static int count = 0;
00112   IplImage* cam_frame = NULL;
00113   
00114   cam_frame = cvQueryFrame(m_capture);
00115   if(NULL == cam_frame)
00116     {
00117       std::cout << "画像がキャプチャできません!!" << std::endl;
00118       return RTC::RTC_ERROR;
00119     }
00120   
00121   IplImage* frame = cvCreateImage(cvGetSize(cam_frame), 8, 3);
00122   
00123   if(cam_frame ->origin == IPL_ORIGIN_TL)
00124     cvCopy(cam_frame, frame);
00125   else
00126     cvFlip(cam_frame, frame);
00127   
00128   int len = frame->nChannels * frame->width * frame->height;
00129   
00130   m_out.data.length(len);
00131   memcpy((void *)&(m_out.data[0]),frame->imageData,len);
00132   cvReleaseImage(&frame);
00133   
00134   m_outOut.write();
00135   
00136   if (count > 100)
00137     {
00138       count = 0;
00139       coil::TimeValue tm;
00140       tm = coil::gettimeofday();
00141       double sec(tm - tm_pre);
00142       if (sec > 1.0 && sec < 1000.0)
00143         {
00144           std::cout << 100/sec << " [FPS]" << std::endl;
00145         }
00146       tm_pre = tm;
00147     }
00148   ++count;
00149   
00150   return RTC::RTC_OK;
00151 }
00152 
00153 
00154 /*
00155   RTC::ReturnCode_t USBCameraAcquire::onAborting(RTC::UniqueId ec_id)
00156   {
00157   
00158   return RTC::RTC_OK;
00159   }
00160 */
00161 
00162 /*
00163   RTC::ReturnCode_t USBCameraAcquire::onError(RTC::UniqueId ec_id)
00164   {
00165   return RTC::OK;
00166   }
00167 */
00168 
00169 /*
00170   RTC::ReturnCode_t USBCameraAcquire::onReset(RTC::UniqueId ec_id)
00171   {
00172   return RTC::OK;
00173   }
00174 */
00175 
00176 /*
00177   RTC::ReturnCode_t USBCameraAcquire::onStateUpdate(RTC::UniqueId ec_id)
00178   {
00179   return RTC::OK;
00180   }
00181 */
00182 
00183 /*
00184   RTC::ReturnCode_t USBCameraAcquire::onRateChanged(RTC::UniqueId ec_id)
00185   {
00186   return RTC::OK;
00187   }
00188 */
00189 
00190 
00191 
00192 extern "C"
00193 {
00194   
00195   void USBCameraAcquireInit(RTC::Manager* manager)
00196   {
00197     coil::Properties profile(usbcameraacquire_spec);
00198     manager->registerFactory(profile,
00199                              RTC::Create<USBCameraAcquire>,
00200                              RTC::Delete<USBCameraAcquire>);
00201   }
00202   
00203 };
00204 
00205 


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:07