appkit  1.5.1
RoadNarrows Robotics Application Kit
Camera.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_cam
8 //
9 // File: Camera.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-07-13 13:54:59 -0600 (Sat, 13 Jul 2013) $
14  * $Rev: 3122 $
15  *
16  * \brief Video and still image camera base class.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2012-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Permission is hereby granted, without written agreement and without
30  * license or royalty fees, to use, copy, modify, and distribute this
31  * software and its documentation for any purpose, provided that
32  * (1) The above copyright notice and the following two paragraphs
33  * appear in all copies of the source code and (2) redistributions
34  * including binaries reproduces these notices in the supporting
35  * documentation. Substantial modifications to this software may be
36  * copyrighted by their authors and need not follow the licensing terms
37  * described here, provided that the new terms are clearly indicated in
38  * all files where they apply.
39  *
40  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45  * THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  *
53  * @EulaEnd@
54  */
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 #ifndef _RNR_CAMERA_H
58 #define _RNR_CAMERA_H
59 
60 #include <string>
61 
62 #include "rnr/rnrconfig.h"
63 #include "rnr/log.h"
64 
65 #include "opencv/cv.h"
66 
67 #ifdef CAM_USE_ECAM_IMPL
68 #include "econ/econ32.h"
69 #endif // CAM_USE_ECAM_IMPL
70 
71 /*!
72  * \brief RoadNarrows Robotics
73  */
74 namespace rnr
75 {
76  /*!
77  * \brief Camera resolution structure.
78  */
79  struct CamRes
80  {
81  int width; ///< width in pixels
82  int height; ///< height in pixels
83  };
84 
85  /*!
86  * \brief Common 4:3 aspect ratio camera resolutions.
87  */
88  const CamRes CamResUndef = { -1, -1}; ///< undefined resolution
89  const CamRes CamResDft = { 0, 0}; ///< default resolution
90  const CamRes CamResQVGA = { 320, 240}; ///< Quarter VGA 320 x 240 res
91  const CamRes CamResVGA = { 640, 480}; ///< VGA 640 x 480 resolution
92  const CamRes CamRes1024x768 = {1024, 768}; ///< 1024 x 768 resolution
93  const CamRes CamRes1440x1080 = {1440, 1080}; ///< 1440 x 1080 resolution
94  const CamRes CamRes1600x1200 = {1600, 1200}; ///< 1600 x 1200 resolution
95  const CamRes CamRes2048x1536 = {2048, 1536}; ///< 2048 x 1536 resolution
96  const CamRes CamRes2592x1944 = {2592, 1944}; ///< 2592 x 1944 resolution
97 
98  /*!
99  * \brief Common 16:10 aspect ratio camera resolutions.
100  */
101  const CamRes CamRes1280x800 = {1280, 800}; ///< 1280 x 800 resolution
102  const CamRes CamRes1440x900 = {1440, 900}; ///< 1440 x 900 resolution
103 
104  /*!
105  * \brief Common ~16:9 aspect ratio camera resolutions.
106  */
107  const CamRes CamRes1280x720 = {1280, 720}; ///< 1280 x 720 resolution
108  const CamRes CamRes1920x1080 = {1920, 1080}; ///< 1920 x 1080 resolution
109 
110  /*!
111  * \brief Default video device.
112  */
113  const char* const VideoDevDft = "/dev/video0"; ///< default video device
114  const int VideoIndexDft = 0; ///< default video index
115  const int VideoDevMajor = 81; ///< major device number
116 
117 
118  //---------------------------------------------------------------------------
119  // Camera Base Class
120  //---------------------------------------------------------------------------
121 
122  /*!
123  * \brief Camera base class.
124  */
125  class Camera
126  {
127  public:
128  /*
129  * \brief Default initialization constructor.
130  *
131  * \param strVideoDevName Video camera device name.
132  * \param resVideo Video resolution.
133  * \param resImage Image resolution.
134  */
135  Camera(const std::string &strVideoDevName=VideoDevDft,
136  const CamRes &resVideo=CamResQVGA,
137  const CamRes &resImage=CamResVGA);
138 
139  /*!
140  * \brief Destructor.
141  */
142  virtual ~Camera();
143 
144  /*!
145  * \brief Start the camera streaming video.
146  *
147  * \param resVideo Video resolution. If equal to CamResDft, then
148  * the initial/last video resolution setting is used.
149  *
150  * \copydoc doc_return_std
151  */
152  virtual int startVideo(const CamRes &resVideo=CamResDft)
153  {
154  CamRes res = resVideo;
155 
156  // default is the current video resolution
157  if( isEqResolution(res, CamResDft) )
158  {
159  res = m_resVideo;
160  }
161 
162  // video resolution differs from camera's current resolution
163  if( !isEqResolution(res, m_resCurrent) )
164  {
165  setCameraResolution(res);
166  }
167 
168  m_resVideo = m_resCurrent;
169  m_bCameraRunning = true;
170 
171  return OK;
172  }
173 
174  /*!
175  * \brief Stop the camera from streaming video.
176  *
177  * \copydoc doc_return_std
178  */
179  virtual int stopVideo()
180  {
181  m_bCameraRunning = false;
182 
183  return OK;
184  }
185 
186  /*!
187  * \brief Grab a image frame from the video stream.
188  *
189  * \param [in,out] The image frame matrix. May be resized.
190  *
191  * \copydoc doc_return_std
192  */
193  virtual int grabFrame(cv::Mat &frame)
194  {
195  return RC_ERROR;
196  }
197 
198  /*!
199  * \brief Take a still image.
200  *
201  * \param [in,out] img Snap shot image taken.
202  * \param resImage Still image resolution. If equal to CamResDft, then
203  * the initial/last image resolution setting is used.
204  *
205  * \copydoc doc_return_std
206  */
207  virtual int clickImage(cv::Mat &img, const CamRes &resImage=CamResDft)
208  {
209  return RC_ERROR;
210  }
211 
212  /*!
213  * \brief Auto-focus camera.
214  */
215  virtual void autoFocus()
216  {
217  }
218 
219  /*!
220  * \brief Test if the camera is on and running.
221  *
222  * \return Returns true if camera is on, else false.
223  */
224  bool isCameraRunning() const
225  {
226  return m_bCameraRunning;
227  }
228 
229  /*!
230  * \brief Test if a still image is currently being taken.
231  *
232  * \return Returns true if an image is currently being taken, else false.
233  */
234  bool isTakingAnImage() const
235  {
236  return m_bTakingImage;
237  }
238 
239  /*!
240  * \brief Test if camera object is in a fatal condition.
241  *
242  * \return Returns true or false.
243  */
244  bool isFatal() const
245  {
246  return m_bFatal;
247  }
248 
249  /*!
250  * \brief Check is two camera resolutions are equal.
251  *
252  * \param res1 Resolution 1.
253  * \param res2 Resolution 2.
254  *
255  * \return Returns true or false.
256  */
257  static bool isEqResolution(const CamRes &res1, const CamRes &res2)
258  {
259  return (res1.width == res2.width) &&
260  (res1.height == res2.height)? true: false;
261  }
262 
263  /*!
264  * \brief Get the video index associated with the device.
265  *
266  * \param strVideoDevName Video camera device name.
267  *
268  * \return Returns video index on success, \ref RC_ERROR(-1) on failure.
269  */
270  static int getVideoIndex(const std::string &strVideoDevName);
271 
272  /*!
273  * \brief Get the current video resolution.
274  *
275  * \return Returns video resolution size.
276  */
278  {
279  return m_resVideo;
280  }
281 
282  /*!
283  * \brief Get the current still image resolution.
284  *
285  * \return Returns still image resolution size.
286  */
288  {
289  return m_resImage;
290  }
291 
292  protected:
293  std::string m_strVideoDevName; ///< video device name
294  int m_nVideoIndex; ///< video index
295  CamRes m_resCurrent; ///< current camera resolution
296  CamRes m_resVideo; ///< current video resolution
297  CamRes m_resImage; ///< current still image resolution
298  bool m_bCameraRunning; ///< camera is [not] on and running video
299  bool m_bTakingImage; ///< taking an image is [not] finished
300  bool m_bFatal; ///< camera instance is in a fatal state
301 
302  /*!
303  * \brief Set the camera resolution in either video or still image mode.
304  *
305  * \param res Target camera resolution.
306  *
307  * \return Actual resolution set.
308  */
309  virtual CamRes setCameraResolution(const CamRes &res)
310  {
311  m_resCurrent = res;
312 
313  return m_resCurrent;
314  }
315  };
316 
317 } // namespace rnr
318 
319 
320 #endif // _RNR_CAMERA_H
const CamRes CamRes1440x1080
1440 x 1080 resolution
Definition: Camera.h:93
const CamRes CamRes2592x1944
2592 x 1944 resolution
Definition: Camera.h:96
const CamRes CamRes1600x1200
1600 x 1200 resolution
Definition: Camera.h:94
static bool isEqResolution(const CamRes &res1, const CamRes &res2)
Check is two camera resolutions are equal.
Definition: Camera.h:257
bool isFatal() const
Test if camera object is in a fatal condition.
Definition: Camera.h:244
const char *const VideoDevDft
Default video device.
Definition: Camera.h:113
const CamRes CamResUndef
Common 4:3 aspect ratio camera resolutions.
Definition: Camera.h:88
CamRes m_resCurrent
current camera resolution
Definition: Camera.h:295
const CamRes CamResDft
default resolution
Definition: Camera.h:89
virtual void autoFocus()
Auto-focus camera.
Definition: Camera.h:215
const CamRes CamRes1280x720
Common ~16:9 aspect ratio camera resolutions.
Definition: Camera.h:107
const CamRes CamRes1024x768
1024 x 768 resolution
Definition: Camera.h:92
virtual CamRes setCameraResolution(const CamRes &res)
Set the camera resolution in either video or still image mode.
Definition: Camera.h:309
bool m_bTakingImage
taking an image is [not] finished
Definition: Camera.h:299
CamRes m_resVideo
current video resolution
Definition: Camera.h:296
bool m_bCameraRunning
camera is [not] on and running video
Definition: Camera.h:298
const CamRes CamRes1280x800
Common 16:10 aspect ratio camera resolutions.
Definition: Camera.h:101
Camera base class.
Definition: Camera.h:125
bool isTakingAnImage() const
Test if a still image is currently being taken.
Definition: Camera.h:234
const CamRes CamRes1440x900
1440 x 900 resolution
Definition: Camera.h:102
const CamRes CamResQVGA
Quarter VGA 320 x 240 res.
Definition: Camera.h:90
Camera resolution structure.
Definition: Camera.h:79
virtual int stopVideo()
Stop the camera from streaming video.
Definition: Camera.h:179
virtual int clickImage(cv::Mat &img, const CamRes &resImage=CamResDft)
Take a still image.
Definition: Camera.h:207
const int VideoDevMajor
major device number
Definition: Camera.h:115
virtual int grabFrame(cv::Mat &frame)
Grab a image frame from the video stream.
Definition: Camera.h:193
int width
width in pixels
Definition: Camera.h:81
int height
height in pixels
Definition: Camera.h:82
int m_nVideoIndex
video index
Definition: Camera.h:294
CamRes m_resImage
current still image resolution
Definition: Camera.h:297
std::string m_strVideoDevName
video device name
Definition: Camera.h:293
virtual int startVideo(const CamRes &resVideo=CamResDft)
Start the camera streaming video.
Definition: Camera.h:152
CamRes getImageResolution() const
Get the current still image resolution.
Definition: Camera.h:287
const CamRes CamRes2048x1536
2048 x 1536 resolution
Definition: Camera.h:95
const int VideoIndexDft
default video index
Definition: Camera.h:114
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224
CamRes getVideoResolution() const
Get the current video resolution.
Definition: Camera.h:277
RoadNarrows Robotics.
Definition: Camera.h:74
const CamRes CamRes1920x1080
1920 x 1080 resolution
Definition: Camera.h:108
const CamRes CamResVGA
VGA 640 x 480 resolution.
Definition: Camera.h:91
bool m_bFatal
camera instance is in a fatal state
Definition: Camera.h:300