Project

General

Profile

BitmapToolkit Scol plugin
ICameraInput.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25#include "ICameraInput.h"
26
27#ifdef _WIN32
28 #include <dshow.h>
29#endif
30
31#ifdef APPLE_IOS
32 #include "CameraTorchIOS.h"
33#endif
34
35std::list<ICameraInput*> ICameraInput::cameraInputsList;
36
38{
39 mIndex = index;
40 mMirrorMode = false;
41 cameraInputsList.push_back(this);
42}
43
48
50{
51 return mMirrorMode;
52}
53
55{
56 mMirrorMode = mode;
57}
58
59std::vector<std::string> ICameraInput::GetDevicesList()
60{
61 std::vector<std::string> result;
62
63#ifdef _WIN32
64 IBaseFilter *p_base_filter = NULL;
65 IMoniker *p_moniker = NULL;
66 ULONG i_fetched;
67 HRESULT hr;
68
69 /*Create the system device enumerator*/
70 ICreateDevEnum *p_dev_enum = NULL;
71
72 hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **)&p_dev_enum);
73
74 if (FAILED(hr))
75 {
76 return result;
77 }
78
79 /* Create an enumerator for the video capture devices */
80 IEnumMoniker *p_class_enum = NULL;
81 hr = p_dev_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &p_class_enum, 0);
82
83 p_dev_enum->Release();
84 if (FAILED(hr))
85 {
86 return result;
87 }
88
89 /* If there are no enumerators for the requested type, then
90 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
91 if (p_class_enum == NULL)
92 {
93 return result;
94 }
95
96 /* Enumerate the devices */
97
98 /* Note that if the Next() call succeeds but there are no monikers,
99 * it will return S_FALSE (which is not a failure). Therefore, we check
100 * that the return code is S_OK instead of using SUCCEEDED() macro. */
101
102 int n = 0;
103
104 while (p_class_enum->Next(1, &p_moniker, &i_fetched) == S_OK)
105 {
106 /* Getting the property page to get the device name */
107 IPropertyBag *p_bag;
108 hr = p_moniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&p_bag);
109 if (SUCCEEDED(hr))
110 {
111 VARIANT var;
112 var.vt = VT_BSTR;
113 hr = p_bag->Read(L"FriendlyName", &var, NULL);
114 p_bag->Release();
115
116 if (SUCCEEDED(hr))
117 {
118 int i_convert = WideCharToMultiByte(CP_ACP, 0, var.bstrVal, SysStringLen(var.bstrVal), NULL, 0, NULL, NULL);
119 char *p_buf = (char *)malloc(i_convert + 1);
120 p_buf[0] = 0;
121
122 WideCharToMultiByte(CP_ACP, 0, var.bstrVal, SysStringLen(var.bstrVal), p_buf, i_convert, NULL, NULL);
123 SysFreeString(var.bstrVal);
124 p_buf[i_convert] = '\0';
125
126 result.push_back(std::string(p_buf));
127
128 free(p_buf);
129 n++;
130 }
131 }
132 p_moniker->Release();
133 }
134
135 p_class_enum->Release();
136
137#elif SCOL_CAMERA_OPENCV // Using openCV utility
138
139 int cameraNumber = 0; // number of cameras detected
140 cv::VideoCapture temp_camera;
141
142 for (int i = 0; i < 128; i++)
143 {
144 cv::VideoCapture temp_camera(i);
145 if (temp_camera.isOpened())
146 {
147 char buffer[6];
148 sprintf(buffer, "cam%d", cameraNumber);
149 result.push_back(buffer);
150 cameraNumber++;
151 }
152 else if (temp_camera.open(i))
153 {
154 char buffer[6];
155 sprintf(buffer, "cam%d", cameraNumber);
156 result.push_back(buffer);
157 cameraNumber++;
158 temp_camera.release();
159 }
160 else
161 break;
162 }
163
164#else // Using android classes
165
166 int cameraNumber = 0;
167 android_app* app = (struct android_app*)SCgetExtra("this_inst");
168 JNIEnv* env = 0;
169 app->activity->vm->AttachCurrentThread(&env, NULL);
170
171 jclass camera_class = env->FindClass("android/hardware/Camera");
172 jmethodID getNumberOfCameras_method = env->GetStaticMethodID(camera_class, "getNumberOfCameras", "()I");
173 cameraNumber = env->CallStaticIntMethod(camera_class, getNumberOfCameras_method);
174
175 if (cameraNumber <= 0)
176 {
177 result.push_back("no camera available on this device");
178 }
179 else
180 {
181 for (int i = 0; i < cameraNumber; i++)
182 {
183 char buffer[6];
184 sprintf(buffer, "cam%d", i);
185 result.push_back(buffer);
186 }
187 }
188
189 env->DeleteLocalRef(camera_class);
190 app->activity->vm->DetachCurrentThread();
191#endif
192
193 return result;
194}
195
197{
198 std::list<ICameraInput*>::iterator it;
199 if (state)
200 {
201 for(it = cameraInputsList.begin(); it != cameraInputsList.end(); it++)
202 {
203 (*it)->Initialize();
204 }
205 }
206 else
207 {
208 for(it = cameraInputsList.begin(); it != cameraInputsList.end(); it++)
209 {
210 (*it)->Close();
211 }
212 }
213}
214
215bool ICameraInput::TakeSnapshot(std::string path) {
216 return false;
217}
218
220{
221 return false;
222}
223
225{
226#ifdef APPLE_IOS
227 turnTorchOn(state);
228#endif
229}
230
void turnTorchOn(bool state)
enable or disable camera flash
bool GetMirrorMode()
void SetMirrorMode(bool mode)
static std::list< ICameraInput * > cameraInputsList
virtual bool TakeSnapshot(std::string path)
ICameraInput(int index)
static void SetCameraInputsState(bool state)
virtual bool SetFocusPoint(int x, int y)
static std::vector< std::string > GetDevicesList()
virtual void SetTorchState(bool state)
virtual ~ICameraInput()
virtual void RenderToScreen()