Webcam application

This is an early example of how to integrate HornetsEye application in a Qt4 graphical user interface under GNU/Linux.  The image is converted to a Qt::Pixmap object and then displayed using Qt::Label.

To use this application you need to install Qt4Ruby.  You also need to compile the user interface description file first:

rbuic4 webcamwindow.ui > ui_webcamwindow.rb

You need to install qt4-qtruby-1.4.8 or later because earlier versions have a memory-leak in Qt::ByteArray.

See also

#!/usr/bin/env ruby
# Qt V4L webcam (requires Qt4-Ruby)
# http://developer.kde.org/language-bindings/ruby/tutorial/tutorial.html
require 'Qt4'
require 'hornetseye'
require 'ui_webcamwindow'
app=Qt::Application.new(ARGV)
class WebcamWindow < Qt::Dialog
  slots 'open_camera()'
  slots 'set_value(int)'
  def initialize( parent = nil )
    super parent
    @ui = Ui::WebcamWindow.new
    @ui.setupUi self
    connect @ui.reconnectButton, SIGNAL('clicked()'),
            self, SLOT('open_camera()')
    connect @ui.brightnessSlider, SIGNAL('valueChanged(int)'),
            self, SLOT('set_value(int)')
    connect @ui.hueSlider, SIGNAL('valueChanged(int)'),
            self, SLOT('set_value(int)')
    connect @ui.colourSlider, SIGNAL('valueChanged(int)'),
            self, SLOT('set_value(int)')
    connect @ui.contrastSlider, SIGNAL('valueChanged(int)'),
            self, SLOT('set_value(int)')
    @timer = 0
    open_camera
  end
  def open_camera
    @ui.errorLabel.text = ''
    begin
      if @input
        @input.close
        @input = nil
      end
      @input = Hornetseye::V4LInput.new @ui.deviceEdit.text
      @timer = startTimer 0 if @timer == 0
    rescue RuntimeError => e
      @ui.errorLabel.text = e.to_s
      @input = nil
    end
    @input
  end
  def set_value( value )
    @input.set_sensivity @ui.brightnessSlider.value,
                         @ui.hueSlider.value,
                         @ui.colourSlider.value,
                         @ui.contrastSlider.value if @input
  end
  def timerEvent( e )
    begin
      raise 'No input available' unless @input
      str = @input.read.to_magick.to_blob {
        self.format = 'PPM'
        self.depth = 8
      }
      pix = Qt::Pixmap.new
      pix.loadFromData Qt::ByteArray.fromRawData( str, str.size )
      @ui.displayLabel.setPixmap pix
      @ui.displayLabel.update
    rescue RuntimeError => e
      killTimer @timer
      @timer = 0
    end
  end
end
win = WebcamWindow.new
win.show
app.exec
HornetsEye allows you to capture images using a V4L-capable (video for linux) device (for example a USB webcam).
Close