appkit  1.5.1
RoadNarrows Robotics Application Kit
CameraEcon32.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: CameraEcon32.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2013-07-15 11:45:50 -0600 (Mon, 15 Jul 2013) $
12  * $Rev: 3131 $
13  *
14  * \brief Econ 3.2 megapixel video and still image camera 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 #if defined(ARCH_overo)
56 
57 #include <stdio.h>
58 #include <stdlib.h>
59 
60 #include "rnr/rnrconfig.h"
61 #include "rnr/log.h"
62 
63 #include "rnr/appkit/Camera.h"
64 #include "rnr/appkit/CameraCv.h"
66 
67 #include "opencv/cv.h"
68 
69 #include "econ/econ32.h"
70 
71 
72 using namespace std;
73 using namespace rnr;
74 using namespace cv;
75 
76 
77 //-----------------------------------------------------------------------------
78 // Econ 3.2MP Derived Camera Class (TBD)
79 //-----------------------------------------------------------------------------
80 
81 CameraEcon32::CameraEcon32(const std::string &strVideoDevName,
82  const CamRes &resVideo,
83  const CamRes &resImage) :
84  CameraCv(strVideoDevName, resVideo, resImage)
85 {
86  makeTmpFile();
87 }
88 
89 CameraEcon32::~CameraEcon32()
90 {
91  stopVideo();
92 
93  if( m_bufTmpName[0] != 0 )
94  {
95  remove(m_bufTmpName);
96  }
97 }
98 
99 int CameraEcon32::clickImage(Mat &img, const CamRes &resImage)
100 {
101  CamRes resVideoPrev = m_resVideo;
102  bool bCamWasRunning = isCameraRunning();
103  CamRes res = resImage;
104 
105  if( m_eCam == NULL )
106  {
107  LOGERROR("No ecam object.");
108  return RC_ERROR;
109  }
110 
111  m_bTakingImage = true;
112 
113  // default is the current image resolution
114  if( isEqResolution(res, CamResDft) )
115  {
116  res = m_resImage;
117  }
118 
119  stopVideo();
120 
122 
123 #if 0 // TODO RDK
124  take_snap(m_eCam);
125 
126  save_snap(m_eCam, m_bufTmpName);
127 #endif // 0 TODO RDK
128 
129  img = imread(m_bufTmpName, CV_LOAD_IMAGE_COLOR);
130 
131  flip(img, img, 0);
132 
133  LOGDIAG3("Took a %dx%d still image.", img.cols, img.rows);
134 
135  // video was running
136  if( bCamWasRunning )
137  {
139  }
140 
141  m_bTakingImage = false;
142 
143  return OK;
144 }
145 
146 void CameraEcon32::autoFocus()
147 {
148 }
149 
150 CamRes CameraEcon32::setCameraResolution(const CamRes &res)
151 {
152  int oldWidth = m_eCam->fmt.fmt.pix.width;
153  int oldHeight = m_eCam->fmt.fmt.pix.height;
154 
155  m_eCam->fmt.fmt.pix.width = res.width;
156  m_eCam->fmt.fmt.pix.height = res.height;
157 
158  m_eCam->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
159 
160  m_eCam->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;
161  m_eCam->fmt.fmt.pix.bytesperline = m_eCam->fmt.fmt.pix.width * 2; // BHW
162  m_eCam->fmt.fmt.pix.priv = 0;
163  m_eCam->fmt.fmt.pix.sizeimage = m_eCam->fmt.fmt.pix.width * \
164  m_eCam->fmt.fmt.pix.height * 2;
165 
166  if (ioctl(m_eCam->fd_v4l2, VIDIOC_S_FMT, &m_eCam->fmt) < 0)
167  {
168  LOGERROR("Unable to set camera resolution %dx%d", res.width, res.height);
169  m_eCam->fmt.fmt.pix.width = oldWidth;
170  m_eCam->fmt.fmt.pix.height = oldHeight;
171  }
172 
173  m_resCurrent.width = m_eCam->fmt.fmt.pix.width;
174  m_resCurrent.height = m_eCam->fmt.fmt.pix.height;
175 
176  return m_resCurrent;
177 }
178 
179 void CameraEcon32::makeTmpFile()
180 {
181  string strX("XXXXXX"); // pattern to be replaced
182  string strSuffix(".bmp"); // suffix
183  int fd; // file descriptor
184 
185  sprintf(m_bufTmpName, "/tmp/camecon32-%s%s", strX.c_str(), strSuffix.c_str());
186 
187 #ifdef HAVE_MKSTEMPS
188  if( (fd = mkstemps(m_bufTmpName, strSuffix.length())) < 0 )
189  {
190  LOGERROR("mkstemps(%s, %zu) failed.\n", m_bufTmpName, strSuffix.length());
191  m_bufTmpName[0] = 0;
192  m_bFatal = true;
193  }
194 
195 #else
196  char tmp[PATH_MAX];
197 
198  sprintf(tmp, "/tmp/camecon32-%s", strX.c_str());
199 
200  if( (fd = mkstemp(tmp)) < 0 )
201  {
202  LOGERROR("mkstemp(%s) failed.\n", tmp);
203  m_bufTmpName[0] = 0;
204  m_bFatal = true;
205  }
206 
207  else
208  {
209  rename(tmp, m_bufTmpName);
210  }
211 #endif
212 
213  if( fd >= 0 )
214  {
215  close(fd);
216  }
217 }
218 
219 #endif // defined(ARCH_overo)
OpenCv implementation of the camera class. The video is streamed via OpenCv calls.
Definition: CameraCv.h:83
static bool isEqResolution(const CamRes &res1, const CamRes &res2)
Check is two camera resolutions are equal.
Definition: Camera.h:257
CamRes m_resCurrent
current camera resolution
Definition: Camera.h:295
virtual int startVideo(const CamRes &resVideo=CamResDft)
Start the camera streaming video.
Definition: CameraCv.cxx:89
Video and still image camera base class.
const CamRes CamResDft
default resolution
Definition: Camera.h:89
bool m_bTakingImage
taking an image is [not] finished
Definition: Camera.h:299
CamRes m_resVideo
current video resolution
Definition: Camera.h:296
OpenCv video and still image camera class.
Econ 3.2 megapixel video and still image camera class.
virtual CamRes setCameraResolution(const CamRes &res)
Set the camera resolution in either video or still image mode.
Definition: CameraCv.cxx:265
virtual int stopVideo()
Stop the camera from streaming video.
Definition: CameraCv.cxx:144
Camera resolution structure.
Definition: Camera.h:79
int width
width in pixels
Definition: Camera.h:81
int height
height in pixels
Definition: Camera.h:82
CamRes m_resImage
current still image resolution
Definition: Camera.h:297
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224
RoadNarrows Robotics.
Definition: Camera.h:74
bool m_bFatal
camera instance is in a fatal state
Definition: Camera.h:300