00001 /* 00002 * This file is part of ALVAR, A Library for Virtual and Augmented Reality. 00003 * 00004 * Copyright 2007-2012 VTT Technical Research Centre of Finland 00005 * 00006 * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi> 00007 * <http://www.vtt.fi/multimedia/alvar.html> 00008 * 00009 * ALVAR is free software; you can redistribute it and/or modify it under the 00010 * terms of the GNU Lesser General Public License as published by the Free 00011 * Software Foundation; either version 2.1 of the License, or (at your option) 00012 * any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, but WITHOUT 00015 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00016 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 00017 * for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public License 00020 * along with ALVAR; if not, see 00021 * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>. 00022 */ 00023 00024 #include "CapturePluginHighgui.h" 00025 00026 #include <sstream> 00027 00028 namespace alvar { 00029 namespace plugins { 00030 00031 CaptureHighgui::CaptureHighgui(const CaptureDevice captureDevice) 00032 : Capture(captureDevice) 00033 , mVideoCapture() 00034 , mMatrix() 00035 , mImage() 00036 { 00037 } 00038 00039 CaptureHighgui::~CaptureHighgui() 00040 { 00041 stop(); 00042 } 00043 00044 void CaptureHighgui::setResolution(const unsigned long xResolution, const unsigned long yResolution) 00045 { 00046 if (mVideoCapture.isOpened()) { 00047 mVideoCapture.set(CV_CAP_PROP_FRAME_WIDTH, xResolution); 00048 mVideoCapture.set(CV_CAP_PROP_FRAME_HEIGHT, yResolution); 00049 mXResolution = (int)mVideoCapture.get(CV_CAP_PROP_FRAME_WIDTH); 00050 mYResolution = (int)mVideoCapture.get(CV_CAP_PROP_FRAME_HEIGHT); 00051 } 00052 } 00053 00054 bool CaptureHighgui::start() 00055 { 00056 if (isCapturing()) { 00057 return isCapturing(); 00058 } 00059 00060 std::istringstream convert(captureDevice().id()); 00061 int id; 00062 convert >> id; 00063 00064 mVideoCapture.open(id); 00065 if (mVideoCapture.isOpened()) { 00066 mIsCapturing = true; 00067 } 00068 00069 return isCapturing(); 00070 } 00071 00072 void CaptureHighgui::stop() 00073 { 00074 if (isCapturing()) { 00075 mVideoCapture.release(); 00076 mIsCapturing = false; 00077 } 00078 } 00079 00080 IplImage *CaptureHighgui::captureImage() 00081 { 00082 if (!isCapturing()) { 00083 return NULL; 00084 } 00085 if (!mVideoCapture.grab()) { 00086 return NULL; 00087 } 00088 mVideoCapture.retrieve(mMatrix); 00089 mImage = mMatrix; 00090 return &mImage; 00091 } 00092 00093 bool CaptureHighgui::showSettingsDialog() 00094 { 00095 // TODO: implement this method 00096 return false; 00097 } 00098 00099 std::string CaptureHighgui::SerializeId() 00100 { 00101 return "CaptureHighgui"; 00102 } 00103 00104 bool CaptureHighgui::Serialize(Serialization *serialization) 00105 { 00106 return false; 00107 } 00108 00109 CapturePluginHighgui::CapturePluginHighgui(const std::string &captureType) 00110 : CapturePlugin(captureType) 00111 { 00112 } 00113 00114 CapturePluginHighgui::~CapturePluginHighgui() 00115 { 00116 } 00117 00118 CapturePlugin::CaptureDeviceVector CapturePluginHighgui::enumerateDevices() 00119 { 00120 CaptureDeviceVector devices; 00121 00122 bool loop = true; 00123 int id = 0; 00124 cv::VideoCapture videoCapture; 00125 00126 while (loop) { 00127 std::stringstream convert; 00128 convert << id; 00129 CaptureDevice captureDevice(mCaptureType, convert.str()); 00130 00131 videoCapture.open(id); 00132 if (videoCapture.isOpened()) { 00133 int width = (int)videoCapture.get(CV_CAP_PROP_FRAME_WIDTH); 00134 int height = (int)videoCapture.get(CV_CAP_PROP_FRAME_HEIGHT); 00135 if (width > 0 && height > 0) { 00136 devices.push_back(captureDevice); 00137 } 00138 else { 00139 loop = false; 00140 } 00141 } 00142 else { 00143 loop = false; 00144 } 00145 00146 id++; 00147 } 00148 videoCapture.release(); 00149 00150 return devices; 00151 } 00152 00153 Capture *CapturePluginHighgui::createCapture(const CaptureDevice captureDevice) 00154 { 00155 return new CaptureHighgui(captureDevice); 00156 } 00157 00158 void registerPlugin(const std::string &captureType, alvar::CapturePlugin *&capturePlugin) 00159 { 00160 capturePlugin = new CapturePluginHighgui(captureType); 00161 } 00162 00163 } // namespace plugins 00164 } // namespace alvar