BitmapToolkit Scol plugin
src
CameraInputWindows.cpp
Go to the documentation of this file.
1
/*
2
-----------------------------------------------------------------------------
3
This source file is part of OpenSpace3D
4
For the latest info, see http://www.openspace3d.com
5
6
Copyright (c) 2012 I-maginer
7
8
This program is free software; you can redistribute it and/or modify it under
9
the terms of the GNU Lesser General Public License as published by the Free Software
10
Foundation; either version 2 of the License, or (at your option) any later
11
version.
12
13
This program is distributed in the hope that it will be useful, but WITHOUT
14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17
You should have received a copy of the GNU Lesser General Public License along with
18
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20
http://www.gnu.org/copyleft/lesser.txt
21
22
-----------------------------------------------------------------------------
23
*/
24
25
#include "
ICameraInput.h
"
26
#include "
CameraInputWindows.h
"
27
#include <dshow.h>
28
29
CameraInputWindows::CameraInputWindows
(
int
index)
30
:
ICameraInput
(index)
31
{
32
mVI =
new
videoInput();
33
}
34
35
bool
CameraInputWindows::Initialize
()
36
{
37
bool
ret =
false
;
38
39
ret = mVI->setupDevice(
mIndex
);
// crash when problem occured
40
if
(!ret)
41
ret = mVI->setupDevice(
mIndex
, VI_COMPOSITE);
// crash when problem occured
42
43
if
(ret)
44
mBuffer.resize(mVI->getSize(
mIndex
));
45
46
return
ret;
47
}
48
49
void
CameraInputWindows::Close
()
50
{
51
mVI->stopDevice(
mIndex
);
52
}
53
54
CameraInputWindows::~CameraInputWindows
()
55
{
56
mVI->stopDevice(
mIndex
);
57
SAFE_DELETE(mVI);
58
}
59
60
bool
CameraInputWindows::IsOpened
()
61
{
62
return
mVI->isDeviceSetup(
mIndex
);
63
}
64
65
cv::Mat
CameraInputWindows::UpdateImage
()
66
{
67
if
(!
IsOpened
())
68
throw
std::logic_error(
"Device not initialised"
);
69
70
// Capture the picture
71
if
(!mVI->isFrameNew(
mIndex
) || !mVI->getPixels(
mIndex
, &mBuffer[0],
false
,
true
))
72
{
73
throw
std::logic_error(
"Device has bad pixel buffer"
);
74
}
75
76
cv::Mat frame(mVI->getHeight(
mIndex
), mVI->getWidth(
mIndex
), CV_8UC3, &mBuffer[0]);
77
78
//if (mMirrorMode) cv::flip(frame,frame,1);
79
return
frame;
80
81
}
82
83
int
CameraInputWindows::GetWidth
()
84
{
85
if
(
IsOpened
())
86
return
mVI->getWidth(
mIndex
);
87
else
88
return
0;
89
}
90
91
int
CameraInputWindows::GetHeight
()
92
{
93
if
(
IsOpened
())
94
return
mVI->getHeight(
mIndex
);
95
else
96
return
0;
97
}
98
99
void
CameraInputWindows::SetSize
(
int
width,
int
height)
100
{
101
if
(
IsOpened
())
102
{
103
mVI->stopDevice(
mIndex
);
104
mVI->setupDevice(
mIndex
, width, height);
105
106
//if the size is not supported we restart with the default size
107
if
(!
IsOpened
())
108
mVI->setupDevice(
mIndex
);
109
110
mBuffer.resize(mVI->getSize(
mIndex
));
111
}
112
}
113
114
bool
CameraInputWindows::TakeSnapshot
(std::string path)
115
{
116
if
(!
IsOpened
())
117
return
false
;
118
119
cv::Mat frame =
UpdateImage
();
120
if
(frame.empty())
121
return
false
;
122
123
if
(!cv::imwrite(path, frame))
124
return
false
;
125
126
std::cout <<
"Frame written to "
<< path <<
"."
<< std::endl;
127
return
true
;
128
}
CameraInputWindows.h
ICameraInput.h
CameraInputWindows::TakeSnapshot
bool TakeSnapshot(std::string path)
Definition
CameraInputWindows.cpp:114
CameraInputWindows::IsOpened
bool IsOpened()
Definition
CameraInputWindows.cpp:60
CameraInputWindows::UpdateImage
cv::Mat UpdateImage()
Definition
CameraInputWindows.cpp:65
CameraInputWindows::GetWidth
int GetWidth()
Definition
CameraInputWindows.cpp:83
CameraInputWindows::Close
void Close()
Definition
CameraInputWindows.cpp:49
CameraInputWindows::Initialize
bool Initialize()
Definition
CameraInputWindows.cpp:35
CameraInputWindows::GetHeight
int GetHeight()
Definition
CameraInputWindows.cpp:91
CameraInputWindows::CameraInputWindows
CameraInputWindows(int index)
Definition
CameraInputWindows.cpp:29
CameraInputWindows::SetSize
void SetSize(int width, int height)
Definition
CameraInputWindows.cpp:99
CameraInputWindows::~CameraInputWindows
~CameraInputWindows()
Definition
CameraInputWindows.cpp:54
ICameraInput
Interface for camera management. Concrete classes are written for Windows, Android and OpenCV native ...
Definition
ICameraInput.h:39
ICameraInput::mIndex
int mIndex
Definition
ICameraInput.h:51
Generated by
1.9.8