source: eviacam/trunk/fuentes/creavision/crvcamera_wdm.cpp @ 567

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

updated sources to version 2.0.3

  • Property svn:executable set to *
File size: 3.7 KB
Line 
1/////////////////////////////////////////////////////////////////////////////
2// Name:        crvcamera_wdm.cpp
3// Purpose: 
4// Author:      Cesar Mauri Loba (cesar at crea-si dot com)
5// Modified by:
6// Created:     30/01/2009
7// Copyright:   (C) 2009 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_wdm.h"
23
24#include <sys/timeb.h>
25#include <sys/types.h>
26#include <cassert>
27#include <cv.h>
28#include "crvimage.h"
29#include "videoInput.h"
30#include "simplelog.h"
31
32CCameraWDM::CCameraWDM(int cameraId, unsigned int width, unsigned int height,
33                float fr) throw(camera_exception)
34{
35        if (cameraId>= GetNumDevices()) throw camera_exception("wrong camera id");
36        m_Id= cameraId;
37        m_Width= width;
38        m_Height= height;
39        m_FrameRate= fr;
40
41        m_VI= NULL;     
42
43        if (slog_get_priority()> SLOG_PRIO_CRIT)
44                videoInput::setVerbose(true);
45        else
46                videoInput::setVerbose(false);
47}
48
49CCameraWDM::~CCameraWDM(void)
50{
51        Close();
52}
53
54bool CCameraWDM::DoOpen ()
55{
56        // Already open? Do nothing
57        if (m_VI) return true;
58
59        // Create a videoInput object
60        m_VI= new videoInput;
61        assert (m_VI);
62
63        // BGR channel sequence assumed
64        //m_pImage= new CIplImage (m_Width, m_Height, IPL_DEPTH_8U, "BGR");
65        //assert (m_pImage);   
66
67        m_VI->setIdealFramerate(m_Id, (int) m_FrameRate);
68        if (!m_VI->setupDevice (m_Id, m_Width, m_Height)) {
69                Close();
70                return false;
71        }
72
73        return true;   
74}
75
76void CCameraWDM::DoClose ()
77{
78        if (m_VI) {
79                m_VI->stopDevice(m_Id);
80                delete m_VI;
81                m_VI= NULL;
82                //delete m_pImage;
83        }
84}
85
86IplImage *CCameraWDM::DoQueryFrame()
87{
88        if (!CCameraWDM::DoQueryFrame(m_Image)) return NULL;
89
90        return m_Image.ptr();
91}
92
93bool CCameraWDM::DoQueryFrame(CIplImage& image)
94{
95        if (!m_VI) return false;
96
97        if (!image.Initialized() || m_VI->getWidth(m_Id)!= image.Width() || m_VI->getHeight(m_Id)!= image.Height()) {
98                // Allocate image in case is not properly set up
99                if (!image.Create (m_Width, m_Height, IPL_DEPTH_8U, "BGR", 1)) {
100                        assert (!"CCameraWDM::DoQueryFrame: cannot create image");
101                        return false;
102                }
103        }               
104        assert (image.Initialized());
105        assert (image.ptr()->imageSize== m_VI->getSize(m_Id));
106       
107        m_VI->getPixels(m_Id, reinterpret_cast<unsigned char *>(image.ptr()->imageData), false, false);
108        // Set appropriate origin
109        image.ptr()->origin= 1;
110
111        return true;
112}
113
114void CCameraWDM::ShowSettingsDialog ()
115{
116        if (m_VI) m_VI->showSettingsWindow (m_Id);
117}
118
119int CCameraWDM::g_numDevices= -1;
120char CCameraWDM::g_deviceNames[MAX_CAM_DEVICES][CAM_DEVICE_NAME_LENGHT];
121
122int CCameraWDM::GetNumDevices() 
123{ 
124        // Win32: uses videoInput
125        if (g_numDevices== -1) {               
126                // First initialization
127                g_numDevices= videoInput::listDevices (true);
128                if (g_numDevices> MAX_CAM_DEVICES) g_numDevices= MAX_CAM_DEVICES;               
129                for (int i= 0; i< g_numDevices; ++i) {
130                        // Prepend device number and append device name
131                        _snprintf (g_deviceNames[i], CAM_DEVICE_NAME_LENGHT, " (Id:%d) %s\0", i, videoInput::getDeviceName(i));
132                }               
133        }
134
135        return g_numDevices;   
136}
137
138const char* CCameraWDM::GetDeviceName (unsigned int id)
139{ 
140        if ((int) id>= GetNumDevices()) return NULL;
141        return g_deviceNames[id];
142}
Note: See TracBrowser for help on using the repository browser.