appkit  1.5.1
RoadNarrows Robotics Application Kit
Camera.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Library: librnr_cam
6 //
7 // File: Camera.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2013-07-13 14:12:15 -0600 (Sat, 13 Jul 2013) $
12  * $Rev: 3126 $
13  *
14  * \brief Video and still image camera base class implementation.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  * \author Daniel Packard (daniel@roadnarrows.com)
18  *
19  * \par Copyright
20  * \h_copy 2012-2017. RoadNarrows LLC.\n
21  * http://www.roadnarrows.com\n
22  * All Rights Reserved
23  */
24 /*
25  * @EulaBegin@
26  *
27  * Permission is hereby granted, without written agreement and without
28  * license or royalty fees, to use, copy, modify, and distribute this
29  * software and its documentation for any purpose, provided that
30  * (1) The above copyright notice and the following two paragraphs
31  * appear in all copies of the source code and (2) redistributions
32  * including binaries reproduces these notices in the supporting
33  * documentation. Substantial modifications to this software may be
34  * copyrighted by their authors and need not follow the licensing terms
35  * described here, provided that the new terms are clearly indicated in
36  * all files where they apply.
37  *
38  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
39  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
40  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
41  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
42  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
43  * THE POSSIBILITY OF SUCH DAMAGE.
44  *
45  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
46  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
47  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
48  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
49  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
50  *
51  * @EulaEnd@
52  */
53 ////////////////////////////////////////////////////////////////////////////////
54 
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include <unistd.h>
58 #include <stdio.h>
59 
60 #include "rnr/rnrconfig.h"
61 #include "rnr/log.h"
62 
63 #include "rnr/appkit/Camera.h"
64 
65 #include "opencv/cv.h"
66 
67 using namespace std;
68 using namespace rnr;
69 using namespace cv;
70 
71 
72 //-----------------------------------------------------------------------------
73 // Camera Base Class
74 //-----------------------------------------------------------------------------
75 
76 Camera::Camera(const std::string &strVideoDevName,
77  const CamRes &resVideo,
78  const CamRes &resImage)
79 {
80  m_strVideoDevName = strVideoDevName;
81  m_nVideoIndex = getVideoIndex(m_strVideoDevName);
82  m_resCurrent = CamResUndef;
83  m_resVideo = resVideo;
84  m_resImage = resImage;
85  m_bCameraRunning = false;
86  m_bTakingImage = false;
87  m_bFatal = false;
88 
89  if( (m_resVideo.width <= 0) || (m_resVideo.height <= 0) )
90  {
91  m_resVideo = CamResQVGA;
92  }
93 
94  if( (m_resImage.width <= 0) || (m_resImage.height <= 0) )
95  {
96  m_resImage = CamResVGA;
97  }
98 
99  if( m_nVideoIndex < 0 )
100  {
101  m_bFatal = true;
102  }
103 }
104 
105 /*!
106  * \brief Default destructor.
107  */
108 Camera::~Camera()
109 {
110  // reelase video resources
111  if( isCameraRunning() )
112  {
113  stopVideo();
114  }
115 }
116 
117 int Camera::getVideoIndex(const string &strVideoDevName)
118 {
119  struct stat statVid;
120  uint_t uMajor;
121  uint_t uMinor;
122 
123  if( strVideoDevName.empty() )
124  {
125  LOGERROR("No video device name.");
126  return RC_ERROR;
127  }
128 
129  else if( access(strVideoDevName.c_str(), F_OK|R_OK|W_OK) != 0 )
130  {
131  LOGSYSERROR("%s.", strVideoDevName.c_str());
132  return RC_ERROR;
133  }
134 
135  else if( stat(strVideoDevName.c_str(), &statVid) != 0 )
136  {
137  LOGSYSERROR("%s.", strVideoDevName.c_str());
138  return RC_ERROR;
139  }
140 
141  uMajor = major(statVid.st_rdev);
142  uMinor = minor(statVid.st_rdev);
143 
144  if( uMajor != VideoDevMajor )
145  {
146  LOGERROR("%s: Not a video device",
147  "Device major number %u != expected number %d.",
148  strVideoDevName.c_str(), uMajor, VideoDevMajor);
149  return RC_ERROR;
150  }
151 
152  return (int)uMinor;
153 }
const CamRes CamResUndef
Common 4:3 aspect ratio camera resolutions.
Definition: Camera.h:88
Video and still image camera base class.
const CamRes CamResQVGA
Quarter VGA 320 x 240 res.
Definition: Camera.h:90
Camera resolution structure.
Definition: Camera.h:79
const int VideoDevMajor
major device number
Definition: Camera.h:115
RoadNarrows Robotics.
Definition: Camera.h:74
const CamRes CamResVGA
VGA 640 x 480 resolution.
Definition: Camera.h:91