upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
vcap.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <iostream>
28 
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <sys/mman.h>
33 #include <jpeglib.h>
34 #include <linux/videodev2.h>
35 
36 #define VCAP_DEFAULT_VIDEODEV "/dev/video0"
37 #define VCAP_DEFAULT_OUTPUTFILE "vcap.jpg"
38 #define VCAP_DEFAULT_WIDTH 640
39 #define VCAP_DEFAULT_HEIGHT 480
40 #define VCAP_DEFAULT_JPEG_QUALITY 99
41 
42 namespace upm {
72  class VCAP {
73  public:
74 
80  VCAP(std::string videoDev=VCAP_DEFAULT_VIDEODEV);
81 
85  ~VCAP();
86 
98  bool setResolution(int width, int height);
99 
105  bool captureImage();
106 
115  bool saveImage(std::string filename=VCAP_DEFAULT_OUTPUTFILE);
116 
124  int getWidth() const
125  {
126  return m_width;
127  };
128 
136  int getHeight() const
137  {
138  return m_height;
139  };
140 
148  void setJPGQuality(unsigned int quality);
149 
155  int getJPGQuality() const
156  {
157  return m_jpgQuality;
158  };
159 
165  void setDebug(bool enable)
166  {
167  m_debugging = enable;
168  };
169 
170  protected:
171  // open the device and check that it meats minimum requirements
172  bool initVideoDevice();
173 
174  // make sure device is streamable, supports mmap and capture
175  bool checkCapabilities();
176 
177  // read the mmapped buffer in YUYV format and create a jpeg image
178  bool YUYV2JPEG(FILE *file);
179 
180  // buffer management
181  bool allocBuffer();
182  void releaseBuffer();
183 
184  // does the actual capture
185  bool doCaptureImage();
186 
187  private:
188  // internal ioctl
189  int xioctl(int fd, int request, void* argp);
190 
191  std::string m_videoDevice;
192 
193  // our file descriptor to the video device
194  int m_fd;
195 
196  // v4l info
197  struct v4l2_capability m_caps;
198  struct v4l2_format m_format;
199 
200  // our mmaped buffer
201  unsigned char *m_buffer;
202  size_t m_bufferLen;
203 
204  // the resolution and quality
205  int m_width;
206  int m_height;
207  int m_jpgQuality;
208 
209  // at least one image captured with current settings?
210  bool m_imageCaptured;
211 
212  // are we debugging?
213  bool m_debugging;
214  };
215 }
void setDebug(bool enable)
Definition: vcap.hpp:165
API for the Video Capture driver.
Definition: vcap.hpp:72
int getWidth() const
Definition: vcap.hpp:124
~VCAP()
Definition: vcap.cxx:61
void setJPGQuality(unsigned int quality)
Definition: vcap.cxx:521
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
int getHeight() const
Definition: vcap.hpp:136
VCAP(std::string videoDev=VCAP_DEFAULT_VIDEODEV)
Definition: vcap.cxx:40
bool captureImage()
Definition: vcap.cxx:417
bool saveImage(std::string filename=VCAP_DEFAULT_OUTPUTFILE)
Definition: vcap.cxx:383
bool setResolution(int width, int height)
Definition: vcap.cxx:141
int getJPGQuality() const
Definition: vcap.hpp:155