upm  1.5.0
Sensor/Actuator repository for libmraa (v1.8.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 {
69  class VCAP {
70  public:
71 
77  VCAP(std::string videoDev=VCAP_DEFAULT_VIDEODEV);
78 
82  ~VCAP();
83 
95  bool setResolution(int width, int height);
96 
102  bool captureImage();
103 
112  bool saveImage(std::string filename=VCAP_DEFAULT_OUTPUTFILE);
113 
121  int getWidth() const
122  {
123  return m_width;
124  };
125 
133  int getHeight() const
134  {
135  return m_height;
136  };
137 
145  void setJPGQuality(unsigned int quality);
146 
152  int getJPGQuality() const
153  {
154  return m_jpgQuality;
155  };
156 
162  void setDebug(bool enable)
163  {
164  m_debugging = enable;
165  };
166 
167  protected:
168  // open the device and check that it meats minimum requirements
169  bool initVideoDevice();
170 
171  // make sure device is streamable, supports mmap and capture
172  bool checkCapabilities();
173 
174  // read the mmapped buffer in YUYV format and create a jpeg image
175  bool YUYV2JPEG(FILE *file);
176 
177  // buffer management
178  bool allocBuffer();
179  void releaseBuffer();
180 
181  // does the actual capture
182  bool doCaptureImage();
183 
184  private:
185  // internal ioctl
186  int xioctl(int fd, int request, void* argp);
187 
188  std::string m_videoDevice;
189 
190  // our file descriptor to the video device
191  int m_fd;
192 
193  // v4l info
194  struct v4l2_capability m_caps;
195  struct v4l2_format m_format;
196 
197  // our mmaped buffer
198  unsigned char *m_buffer;
199  size_t m_bufferLen;
200 
201  // the resolution and quality
202  int m_width;
203  int m_height;
204  int m_jpgQuality;
205 
206  // at least one image captured with current settings?
207  bool m_imageCaptured;
208 
209  // are we debugging?
210  bool m_debugging;
211  };
212 }
void setDebug(bool enable)
Definition: vcap.hpp:162
API for the Video Capture driver.
Definition: vcap.hpp:69
int getWidth() const
Definition: vcap.hpp:121
~VCAP()
Definition: vcap.cxx:61
void setJPGQuality(unsigned int quality)
Definition: vcap.cxx:521
Definition: a110x.hpp:29
int getHeight() const
Definition: vcap.hpp:133
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:152