BitmapToolkit Scol plugin
CameraInputWindows.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#include "CameraInputWindows.h"
27#include <dshow.h>
28
30 : ICameraInput(index)
31{
32 mVI = new videoInput();
33}
34
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
50{
51 mVI->stopDevice(mIndex);
52}
53
55{
56 mVI->stopDevice(mIndex);
57 SAFE_DELETE(mVI);
58}
59
61{
62 return mVI->isDeviceSetup(mIndex);
63}
64
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
84{
85 if (IsOpened())
86 return mVI->getWidth(mIndex);
87 else
88 return 0;
89}
90
92{
93 if (IsOpened())
94 return mVI->getHeight(mIndex);
95 else
96 return 0;
97}
98
99void 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
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}
bool TakeSnapshot(std::string path)
void SetSize(int width, int height)
Interface for camera management. Concrete classes are written for Windows, Android and OpenCV native ...