source: eviacam/trunk/fuentes/creavision/crvcamera_cv.cpp @ 29

Last change on this file since 29 was 29, checked in by mabarracus, 4 years ago

added eviacam content

  • Property svn:executable set to *
File size: 5.9 KB
Line 
1/////////////////////////////////////////////////////////////////////////////
2// Name:        crvcamera_cv.cpp
3// Purpose: 
4// Author:      Cesar Mauri Loba (cesar at crea-si dot com)
5// Modified by:
6// Created:     30/05/2008
7// Copyright:   (C) 2008 Cesar Mauri Loba - CREA Software Systems
8//
9//  This program is free software: you can redistribute it and/or modify
10//  it under the terms of the GNU General Public License as published by
11//  the Free Software Foundation, either version 3 of the License, or
12//  (at your option) any later version.
13//
14//  This program is distributed in the hope that it will be useful,
15//  but WITHOUT ANY WARRANTY; without even the implied warranty of
16//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17//  GNU General Public License for more details.
18//
19//  You should have received a copy of the GNU General Public License
20//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21/////////////////////////////////////////////////////////////////////////////
22#include "crvcamera_cv.h"
23#include <highgui.h>
24#include <sys/timeb.h>
25#include <sys/types.h>
26#include <stdio.h>
27#include <cassert>
28
29bool CCameraCV::g_cvInitialized= false;
30int CCameraCV::g_numDevices= 0;
31char CCameraCV::g_deviceNames[MAX_CV_DEVICES][50];
32
33#if defined(WIN32)
34#include <windows.h>
35
36#define MAX_KEY_LENGTH 255
37#define MAX_VALUE_NAME 16383
38
39// Workround to enable capture at 30fps for some camera models
40// Should be called with administrative rights
41// Return 0 if ok, -1 if permission denied
42int VfwCamFpsWorkaround ()
43{
44        HKEY hKey;   
45        TCHAR    achKey[MAX_KEY_LENGTH];   // buffer for subkey name
46    DWORD    cbName;                   // size of name string
47    TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name
48    DWORD    cchClassName = MAX_PATH;  // size of class string
49    DWORD    cSubKeys=0;               // number of subkeys
50    DWORD    cbMaxSubKey;              // longest subkey size
51    DWORD    cchMaxClass;              // longest class string
52    DWORD    cValues;              // number of values for key
53    DWORD    cchMaxValue;          // longest value name
54    DWORD    cbMaxValueData;       // longest value data
55    DWORD    cbSecurityDescriptor; // size of security descriptor
56    FILETIME ftLastWriteTime;      // last write time
57        DWORD i, retCode;   
58   // DWORD cchValue = MAX_VALUE_NAME;
59
60        // Open key
61        retCode= RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\VfWWDM Mapper"), 
62                                                        0, KEY_ALL_ACCESS, &hKey);
63        if (retCode!= ERROR_SUCCESS) return -1;
64
65        // Get the class name and the value count.
66    retCode = RegQueryInfoKey(
67        hKey,                    // key handle
68        achClass,                // buffer for class name
69        &cchClassName,           // size of class string
70        NULL,                    // reserved
71        &cSubKeys,               // number of subkeys
72        &cbMaxSubKey,            // longest subkey size
73        &cchMaxClass,            // longest class string
74        &cValues,                // number of values for this key
75        &cchMaxValue,            // longest value name
76        &cbMaxValueData,         // longest value data
77        &cbSecurityDescriptor,   // security descriptor
78        &ftLastWriteTime);       // last write time
79 
80        // Enumerate the subkeys
81        if (cSubKeys) {
82                for (i=0; i<cSubKeys; i++) { 
83                        cbName = MAX_KEY_LENGTH;
84                        retCode = RegEnumKeyEx(hKey, i, achKey, &cbName, NULL, 
85                                                                        NULL, NULL, &ftLastWriteTime); 
86
87                        if (retCode == ERROR_SUCCESS) {
88                                HKEY hKeyValue;
89
90                                retCode= RegOpenKeyEx(hKey, achKey, 0, KEY_WRITE, &hKeyValue);
91                                if (retCode== ERROR_SUCCESS) {
92                                        DWORD value= 333330;
93                                        retCode= RegSetValueEx(hKeyValue, TEXT("AvgTimePerFrame"), 0, REG_DWORD, 
94                                                                                        (const BYTE *) &value, sizeof(DWORD));
95                                }
96                                RegCloseKey(hKeyValue);
97                        }
98                }
99        } 
100
101        // Close key
102        RegCloseKey(hKey);
103
104        return 0;
105}
106
107#endif
108
109CCameraCV::CCameraCV(int cameraId, unsigned int width, int unsigned height, 
110                float fr) throw (camera_exception)
111{
112        if (cameraId>= GetNumDevices()) throw camera_exception("wrong camera id");
113        m_Id= cameraId;
114        m_Width= width;
115        m_Height= height;
116        m_FrameRate= fr;
117        m_pCvCapture= NULL;
118
119#if defined(WIN32)
120        VfwCamFpsWorkaround ();
121#endif
122}
123
124CCameraCV::~CCameraCV(void)
125{
126        Close ();       
127}
128
129bool CCameraCV::DoOpen ()
130{
131        if (m_pCvCapture!= NULL) return true;   // Already opened
132        m_pCvCapture= cvCaptureFromCAM (m_Id);
133        if (m_pCvCapture== NULL) return false;
134       
135        // Try to set capture parameters although not always works
136        cvSetCaptureProperty( m_pCvCapture, CV_CAP_PROP_FRAME_WIDTH, (double) m_Width );
137        cvSetCaptureProperty( m_pCvCapture, CV_CAP_PROP_FRAME_HEIGHT, (double) m_Height );
138        // The following line does nothing under MS Windows
139        cvSetCaptureProperty( m_pCvCapture, CV_CAP_PROP_FPS, (double) m_FrameRate );
140
141        return true;
142}
143
144void CCameraCV::DoClose ()
145{
146        if (m_pCvCapture== NULL) return;        // Already closed
147        cvReleaseCapture (&m_pCvCapture);
148        m_pCvCapture= NULL;
149}
150
151IplImage *CCameraCV::DoQueryFrame()
152{
153        assert (m_pCvCapture);
154        if (m_pCvCapture== NULL) return NULL;
155
156        IplImage *pImage= cvQueryFrame( m_pCvCapture );
157        assert (pImage);
158        if (pImage== NULL) return NULL;
159       
160#if defined(linux)
161        // It seems that under Linux no proper channelSeq is reported
162        // Tested with Logitech Quickcam pro 4000
163        pImage->channelSeq[0]= 'B';
164        pImage->channelSeq[2]= 'R';
165#endif
166
167        return pImage;
168}
169
170int CCameraCV::GetNumDevices()
171{
172        if (!g_cvInitialized) {         
173                cvInitSystem (0, NULL); 
174                g_cvInitialized= true; 
175
176                int i;
177                CvCapture* tmpCapture;
178
179                // Detect number of connected devices
180                for (i= 0; i< MAX_CV_DEVICES; ++i) {
181                        tmpCapture= cvCreateCameraCapture (i);
182                        if (tmpCapture== NULL) break;
183
184                        cvReleaseCapture (&tmpCapture);
185
186                        // Generate device name
187                        sprintf (g_deviceNames[i], "Camera (Id:%d)", i);
188                }               
189                g_numDevices= i;
190        }
191        return g_numDevices;
192}
193
194char* CCameraCV::GetDeviceName (unsigned int id)
195{
196        if ((int) id>= GetNumDevices()) return NULL;   
197        return g_deviceNames[id];
198}
Note: See TracBrowser for help on using the repository browser.