Introducing the rb_webcam gem

The rb_webcam has been around for a while however it seems to also be an incomplete research project. This entry documents how I created a webcam jpeg image using this gem.

Installation

You will need to install imagemagick, as well as Open CV, and the rb_webcam gem.

sudo apt-get install imagemagick libmagickcore-dev libmagickwand-dev libcv2.1 libhighgui2.1
sudo gem1.9.1 install rb_webcam
sudo gem1.9.1 install rmagick

Ta

king a snapshot

I executed the following code from an IRB session:

require 'rb_webcam'
require 'RMagick'

capture = Webcam.new 
image = capture.grab 
width, height = image.size[:width], image.size[:height]
rows = image.data.unpack("C*").each_slice(3).to_a.each_slice(width).to_a
img = Magick::Image.new width, height
rows.each_with_index do |r, i|
  q = r.map {|b, g, r| Magick::Pixel.new r * 256, g * 256, b * 256, 0}
  img.store_pixels(0, i, width, 1, q)
end
img.format = 'jpg'
img.write 'webcam.jpg'

Tags:
Source:
2012hrs.txt
Published:
14-12-2011 20:12