root / branches / test / PS3Capture / PS3Capture / PS3.cpp @ 6

View | Annotate | Download (5.4 KB)

1
//! PS3.cpp
2
/*!        Update: 2011-05-14 by Yishi Guo
3
            Add the capture and show functions */
4
// -------------------------------------------------
5
6
#include "PS3.h"
7
8
PS3::PS3(){
9
        _bShow = true;
10
        _bRunning = false;
11
        _bInitialized = false;
12
13
        _pCapBuffer = NULL;
14
};
15
16
PS3::~PS3() {
17
        _cam = NULL;
18
        _pCapBuffer = NULL;
19
20
};
21
22
int PS3::GetCameraCount() {
23
        return CLEyeGetCameraCount();
24
}
25
26
// Get GUID by camera Id
27
GUID PS3::GetGUID( int camId ) {
28
        return CLEyeGetCameraUUID( camId );
29
}
30
31
//! Convert the GUID to string
32
std::string PS3::GUID2String( GUID guid, char delimiter, bool uppercase ) {
33
        std::stringstream ss;
34
        if ( uppercase ) {
35
                ss << std::uppercase;
36
        } else {
37
                ss << std::nouppercase;
38
        }
39
40
        //! Get the hex format GUID string
41
        /*! Like: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXX 
42
        */
43
        ss.fill( '0' );        //! Fill the space with "0"
44
        ss << std::hex;        //! Output as Hexadecimal
45
46
        //! 8 Bytes        |        Data1
47
        ss.width( 8 );        //! Set the width to 8
48
        ss << guid.Data1 << delimiter;
49
50
        //! 4 Bytes        |        Data2
51
        ss.width( 4 );        ss << guid.Data2 << delimiter;
52
53
        //! 4 Bytes        |        Data3
54
        ss.width( 4 );        ss << guid.Data3 << delimiter;
55
56
        //! 4 Bytes        |        Initial two bytes from Data4
57
        ss.width( 2 );        ss << (int)guid.Data4[0];
58
        ss.width( 2 );        ss << (int)guid.Data4[1] << delimiter;
59
60
        //! 12 Bytes|        Remaining six bytes from Data4
61
        ss.width( 2 );        ss << (int)guid.Data4[2];
62
        ss.width( 2 );        ss << (int)guid.Data4[3];
63
        ss.width( 2 );        ss << (int)guid.Data4[4];
64
        ss.width( 2 );        ss << (int)guid.Data4[5];
65
        ss.width( 2 );        ss << (int)guid.Data4[6];
66
        ss.width( 2 );        ss << (int)guid.Data4[7];
67
68
        return ss.str();
69
}
70
71
//! The thread for capture
72
/*! Copy from CLEyeMulticamTest.cpp */
73
DWORD WINAPI PS3::CaptureThread( LPVOID instance ) {
74
        PS3 *pThis = (PS3*)instance;
75
        pThis->Run();
76
77
        return 0;
78
}
79
80
//! Run
81
void PS3::Run() {
82
        if ( !_bInitialized ) {
83
                return;
84
        }
85
        int width, height;
86
        IplImage *pCapImage;
87
88
        _cam = CLEyeCreateCamera( _camGUID, _camColorMode, _camResolution, _frameRate );
89
        
90
        if ( _cam == NULL ) {
91
                return;        //! Could not create camera
92
        }
93
94
        CLEyeCameraGetFrameDimensions( _cam, width, height );
95
96
        if ( _camColorMode == CLEYE_COLOR_PROCESSED || _camColorMode == CLEYE_COLOR_RAW ) {
97
                pCapImage = cvCreateImage( cvSize(width, height), IPL_DEPTH_8U, 4 );
98
        } else {
99
                pCapImage = cvCreateImage( cvSize(width, height), IPL_DEPTH_8U, 1 );
100
        }
101
102
        //! Set some camera parameters
103
        CLEyeSetCameraParameter( _cam, CLEYE_GAIN, 0 );
104
        CLEyeSetCameraParameter( _cam, CLEYE_EXPOSURE, 511 );
105
106
        //! Start capturing
107
        CLEyeCameraStart( _cam );
108
109
        //! Get the image from captured buffer
110
        cvGetImageRawData( pCapImage, &_pCapBuffer );
111
112
        //! Image capturing loop
113
        while ( _bRunning ) {
114
                if ( _bCapture ) {
115
                        CLEyeCameraGetFrame( _cam, _pCapBuffer );
116
                        if ( _bShow ) {
117
                                cvShowImage( _windowTitle.c_str(), pCapImage );
118
                        }
119
                }
120
        }
121
122
        //! Stop camera capture
123
        CLEyeCameraStop( _cam );
124
125
        //! Destroy camera object
126
        CLEyeDestroyCamera( _cam );
127
128
        //! Release OpenCV resources
129
        cvReleaseImage( &pCapImage );
130
131
        _cam = NULL;
132
}
133
134
//! Set PS3 camera
135
void PS3::SetCamera( GUID camGUID, CLEyeCameraColorMode colorMode, CLEyeCameraResolution camRes, float frameRate ) {
136
        _camGUID = camGUID;
137
        _camColorMode = colorMode;
138
        _camResolution = camRes;
139
        _frameRate = frameRate;
140
141
        _bInitialized = true;
142
}
143
144
//! Get the GUID of camera
145
GUID PS3::GetGUID() const{
146
        return _camGUID;
147
}
148
149
//! Set the GUID of camera
150
void PS3::SetGUID( GUID guid ) {
151
        _camGUID = guid;
152
}
153
154
bool PS3::StartCamera() {
155
        //! The camera must be initialized
156
        if ( !_bInitialized ) {
157
                return false;
158
        }
159
160
        _bRunning = true;
161
162
        //! Show window or not
163
        if ( _bShow ) {
164
                if ( _windowTitle.empty() ) {
165
                        _windowTitle = "GUID: " + GUID2String( _camGUID );
166
                }
167
                cvNamedWindow( _windowTitle.c_str(), CV_WINDOW_AUTOSIZE );
168
        }
169
170
        // Start the capture thread
171
        _hThread = CreateThread( NULL, 0, &PS3::CaptureThread, this, 0, 0 );
172
173
        if ( _hThread == NULL ) {
174
                printf( "Could not create capture thread\n" );
175
                //MessageBoxA( NULL, "Could not create capture thread", "PS3Capture", MB_ICONEXCLAMATION );
176
                return false;
177
        }
178
179
        return true;
180
}
181
182
void PS3::StopCamera() {
183
        if ( !_bRunning ) {
184
                return;
185
        }
186
187
        //! Stop the capturing loop
188
        _bRunning = false;
189
190
        WaitForSingleObject( _hThread, 1000 );
191
192
        //! Destroy window
193
        if ( _bShow ) {
194
                cvDestroyWindow( _windowTitle.c_str() );
195
        }
196
}
197
198
bool PS3::StartCapture() {
199
        if ( !_bRunning || _bCapture ) {
200
                return false;
201
        }
202
203
        if ( !CLEyeCameraStart( _cam ) ) {
204
                return false;
205
        }
206
        _bCapture = true;
207
208
        return true;
209
}
210
211
bool PS3::StopCapture() {
212
        if ( !_bRunning || !_bCapture ) {
213
                return false;
214
        }
215
216
        if ( !CLEyeCameraStop( _cam ) ) {
217
                return false;
218
        }
219
220
        _bCapture = false;
221
222
        return true;
223
}
224
225
226
bool PS3::ShowWindow( bool bShow ) {
227
        //! The camera must be capturing now!
228
        if ( !_bRunning || !_bCapture ) {
229
                return false;
230
        }
231
232
        if ( bShow && !_bShow ) {
233
                _bShow = true;
234
                // WaitForSingleObject(_hThread, 1000);
235
                cvNamedWindow( _windowTitle.c_str(), CV_WINDOW_AUTOSIZE );
236
237
                return true;
238
        } else if ( !bShow && _bShow ) {
239
                _bShow = false;
240
                // WaitForSingleObject(_hThread, 1000);
241
                cvDestroyWindow( _windowTitle.c_str() );
242
243
                return true;
244
        }
245
246
        return true;
247
}
248
249
void PS3::SetShow( bool bShow ) {
250
        _bShow = bShow;
251
}
252
253
void PS3::SetWindowTitle( string title ) {
254
        _windowTitle = title;
255
}
256
257
unsigned char* PS3::GetPixels() {
258
        return _pCapBuffer;
259
}