29 const std::string & dev_name ,
const std::string & dev_config ,
30 bool one_shot ,
bool lazy_open ,
unsigned int open_timeout ,
32 const std::string & image_input_source_name ) :
33 ImageInputSource(converter,image_input_source_name.empty()?dev_name:image_input_source_name) ,
34 m_dev_name(dev_name) ,
35 m_dev_config(dev_config) ,
36 m_one_shot(one_shot) ,
37 m_lazy_open(lazy_open) ,
38 m_open_timeout(std::max(1U,open_timeout)) ,
43 m_open_timer(*this,&Gv::
Camera::onOpenTimeout,*this) ,
44 m_read_timer(*this,&Gv::
Camera::onReadTimeout,*this) ,
45 m_send_timer(*this,&Gv::
Camera::onSendTimeout,*this)
48 G_DEBUG(
"Gv::Camera::Camera: device [" << dev_name <<
"] sleep=" << m_sleep_ms <<
"ms" ) ;
49 m_capture.reset( create(dev_name,dev_config,m_lazy_open) ) ;
50 if( m_capture.get() == nullptr )
51 m_open_timer.startTimer( m_open_timeout ) ;
58 if( m_capture.get() != nullptr )
67 Gv::Capture * Gv::Camera::create(
const std::string & dev_name ,
const std::string & dev_config ,
bool lazy_open )
73 catch( Gv::Capture::NoDevice & )
75 if( lazy_open )
return nullptr ;
80 void Gv::Camera::onOpenTimeout()
82 m_capture.reset( create(m_dev_name,m_dev_config,
true) ) ;
83 if( m_capture.get() != nullptr )
86 m_open_timer.startTimer( m_open_timeout ) ;
89 void Gv::Camera::onOpen()
91 m_dx =
static_cast<int>(m_capture->dx()) ;
92 m_dy =
static_cast<int>(m_capture->dy()) ;
95 G_LOG(
"Gv::Camera::onOpen: camera device=[" << m_dev_name <<
"] " << m_capture.get()->info() ) ;
99 int Gv::Camera::fd()
const
101 return m_capture.get() ? m_capture->fd() : -1 ;
104 void Gv::Camera::onReadTimeout()
106 G_DEBUG(
"Gv::Camera::readEvent: delayed read" ) ;
111 void Gv::Camera::onException( std::exception & e )
115 G_ERROR(
"Gv::Camera::onException: exception: " << e.what() ) ;
119 void Gv::Camera::readEvent()
124 m_read_timer.startTimer( 0 , m_sleep_ms * 1000UL ) ;
132 void Gv::Camera::doRead()
139 m_open_timer.startTimer( m_open_timeout ) ;
140 m_send_timer.startTimer( 0 ) ;
144 void Gv::Camera::blank()
146 G_ASSERT( m_capture.get() != nullptr ) ;
148 Gr::ImageData image_data( *image_buffer_p , m_image_type.dx() , m_image_type.dy() , m_image_type.channels() ) ;
149 int dy = image_data.
dy() ;
150 int dx = image_data.dx() ;
151 for(
int y = 0 ; y < dy ; y++ )
153 for(
int x = 0 ; x < dx ; x++ )
155 image_data.rgb( x , y , image_data.r(x,y)/8U , image_data.g(x,y)/8U , image_data.b(x,y)/8U ) ;
159 overlay.write( (dx-8)/2 , (dy-8)/2 ,
"X" , m_tz ) ;
167 m_image_data(image_data)
172 cb.
copyTo( m_image_data ) ;
178 bool Gv::Camera::doReadImp()
182 bool read_ok = false ;
184 Gr::ImageData image_data( *image_buffer_p , m_image_type.dx() , m_image_type.dy() , m_image_type.channels() ) ;
185 if( m_capture->simple() )
188 char * p = Gr::imagebuffer::row_ptr( Gr::imagebuffer::row_begin(*image_buffer_p) ) ;
189 size_t n = Gr::imagebuffer::size_of( *image_buffer_p ) ;
190 read_ok = m_capture->read( reinterpret_cast<unsigned char*>(p) , n ) ;
195 Fill copy_to_image( image_data ) ;
196 read_ok = m_capture->read( copy_to_image ) ;
200 if( !m_caption.empty() )
204 overlay.write( 10 , 10 , m_caption , m_tz ) ;
206 m_send_timer.startTimer( 0 ) ;
210 G_DEBUG(
"Gv::Camera::doRead: failed to acquire an image from the capture device" ) ;
214 catch( Gv::Capture::NoDevice & )
216 G_LOG(
"Gv::Camera::doRead: capture device has disappeared" ) ;
223 void Gv::Camera::onSendTimeout()
225 if( !m_image.empty() )
226 sendImageInput( m_image ) ;
229 void Gv::Camera::resend( ImageInputHandler & )
231 m_send_timer.startTimer( 0 ) ;
virtual ~Camera()
Destructor.
A high-level webcam interface that hooks into the GNet event loop and acts as an Gv::ImageInputSource...
A holder for image data, having eight bits per sample and one or three channels.
A representation of a timezone.
A class that can add a text overlay to an image.
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
virtual void dropRead(Descriptor fd)=0
Removes the given event source descriptor from the list of read sources.
bool isOpen() const
Returns true if the webcam device is open.
A class that encapsulates a network file descriptor and hides knowledge of its o/s-spefific error val...
static Capture * create(const std::string &dev_name, const std::string &config, Gv::Timezone=Gv::Timezone())
Factory method.
static ImageType raw(int dx, int dy, int channels)
Factory function for a raw image type.
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
virtual void addRead(Descriptor fd, EventHandler &handler)=0
Adds the given event source descriptor and associated handler to the read list.
A video-capture buffer class to hold image data, with overloaded constructors for the various V4l i/o...
static ImageBuffer * blank(Image &, ImageType raw_type, bool contiguous=false)
Factory function for a not-really-blank raw image that is temporarily writable via the returned image...
A callback interface for the Gv::Capture class.
static std::string splitMatchTail(const std::string &in, const std::string &s, const std::string &ws=Str::ws())
Splits the input string into parts and looks for a part that starts with 's' and returns the trailing...
void copyTo(Gr::ImageData &) const
Copies the image to a correctly-sized image data buffer.
A video capture abstract interface, exposing a file descriptor and a method to handle read events on ...
An image format converter that can convert to and from the raw and jpeg formats (only), with scaling and monochrome options.
int dy() const
Returns the height.
Camera(Gr::ImageConverter &, const std::string &dev_name, const std::string &dev_config, bool one_shot, bool lazy_open, unsigned int lazy_open_timeout, const std::string &caption, const Gv::Timezone &caption_tz=Gv::Timezone(), const std::string &image_input_source_name=std::string())
Constructor.
static EventLoop & instance()
Returns a reference to an instance of the class, if any.