# Linux Cairo, webcam displaying images

**URL:** <https://www.ruby-forum.com/t/linux-cairo-webcam-displaying-images/75567>\
**Category:** Ruby\
**Created:** [November 11, 2006, 1:01am UTC](https://www.ruby-forum.com/t/linux-cairo-webcam-displaying-images/75567 "2006-11-11T01:01:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [November 11, 2006, 1:01am UTC](https://www.ruby-forum.com/t/linux-cairo-webcam-displaying-images/75567/1 "2006-11-11T01:01:56Z")

</div>

G’day  
Can anyone please advise me on how to constantly display the output from  
a webcam using Ruby & Cairo?  
I can operate the webcam, write images to disk etc and I can display a  
Gdk::Pixbuf okay. It’s just the updating of an image on the screen that  
I’m missing.

The data returned from V4l is a ruby ‘string’ but how do I display it?  
At first I thought to display a blank Gdk::Pixbuf of correct size  
(640x480) and constantly modify it’s buffer but I think this might not  
be possible from within Ruby???

# ==== From the Ruby-Gnome2 site it says: Gdk::Pixbuf.new(colorspace, has\_alpha, bits\_per\_sample, width, height) Creates a new Gdk::Pixbuf and allocates a buffer for it. The buffer has an optimal rowstride. Note that the buffer is not cleared; you will have to fill it completely yourself. Raises an…

note “you will have to fill it completely yourself” how is this achieved  
in Ruby?

Any help would be most welcome.

Cheers, Mike

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [November 11, 2006, 5:00am UTC](https://www.ruby-forum.com/t/linux-cairo-webcam-displaying-images/75567/2 "2006-11-11T05:00:27Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Linux Cairo, webcam displaying images” on Sat, 11 Nov 2006 09:00:08  
+0900,  
Mike D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone please advise me on how to constantly display the output from  
> a webcam using Ruby & Cairo?

Data from webcam (String) → Gdk::Pixbuf →   
Cairo::ImageSurface.new(data, format, width, height, stride) →   
Cairo::Context.new(Cairo::XXXSurface.new) →   
context.set\_source(image\_surface) → context.paint

> The data returned from V4l is a ruby ‘string’ but how do I display it?  
> At first I thought to display a blank Gdk::Pixbuf of correct size  
> (640x480) and constantly modify it’s buffer but I think this might not  
> be possible from within Ruby???

Use Gdk::Pixbuf.new(data, colorspace, has\_alpha, bit\_per\_sample,  
width, height, rowstride):

Creates a new Gdk::Pixbuf out of in-memory image  
data. Currently only RGB images with 8 bits per sample  
are supported. Raises an Gdk::PixbufError if there was  
not enough memory.

```
* data: Image data in 8-bit/sample packed format(String).
* colorspace: Color space(GdkColorspace) for image data.
* has_alpha: true if the data has an opacity channel or false.
* bits_per_sample: Number of bits per color sample.
* width: Width of image in pixels.
* height: Height of image in pixels.
* rowstride: Distance in bytes between row starts.
* Returns: A newly-created Gdk::Pixbuf.

```

Thanks,

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [November 11, 2006, 5:14am UTC](https://www.ruby-forum.com/t/linux-cairo-webcam-displaying-images/75567/3 "2006-11-11T05:14:28Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: Linux Cairo, webcam displaying images” on Sat, 11 Nov 2006  
12:58:41 +0900,  
Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Can anyone please advise me on how to constantly display the output from  
> > a webcam using Ruby & Cairo?
> 
> Data from webcam (String) → Gdk::Pixbuf →   
> Cairo::ImageSurface.new(data, format, width, height, stride) →   
> Cairo::Context.new(Cairo::XXXSurface.new) →   
> context.set\_source(image\_surface) → context.paint

I mistook. Gdk::Pixbuf isn’t needed.

Data from webcam (String) →   
Cairo::ImageSurface.new(data, format, width, height, stride) →   
Cairo::Context.new(Cairo::XXXSurface.new) →   
context.set\_source(image\_surface) → context.paint

Thanks,

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [November 11, 2006, 7:56am UTC](https://www.ruby-forum.com/t/linux-cairo-webcam-displaying-images/75567/4 "2006-11-11T07:56:44Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: Linux Cairo, webcam displaying images” on Sat, 11 Nov 2006  
15:20:04 +0900,  
Mike D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just a couple of questions to clarify things if you wouldn’t mind.
> 
> > Cairo::ImageSurface.new(data, format, width, height, stride) →
> 
> is ‘data’ a Ruby string returned from the V4l?

Yes, it’s a String. But I don’t know V4l’s data format. So,  
I can’t promise you that you can use V4l’s data directory.

> what is ‘format’?

One of the cairo\_format\_t:  
[http://www.cairographics.org/manual/cairo-Image-Surfaces.html#id2598794](http://www.cairographics.org/manual/cairo-Image-Surfaces.html#id2598794)

In rcairo, those formats are mapped to Cairo::FORMAT\_XXX.

Thanks,

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [November 11, 2006, 7:20am UTC](https://www.ruby-forum.com/t/linux-cairo-webcam-displaying-images/75567/5 "2006-11-11T07:20:39Z")

</div>

Kouhei S. wrote:

> > Cairo::Context.new(Cairo::XXXSurface.new) -\>  
> > –  
> > kou

Thanks for the info kou,  
Just a couple of questions to clarify things if you wouldn’t mind.

> Cairo::ImageSurface.new(data, format, width, height, stride) -\>

is ‘data’ a Ruby string returned from the V4l?

what is ‘format’?

Thanks for your help kou  
Mike

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [November 11, 2006, 8:50am UTC](https://www.ruby-forum.com/t/linux-cairo-webcam-displaying-images/75567/6 "2006-11-11T08:50:46Z")

</div>

Kouhei S. wrote:

> > is ‘data’ a Ruby string returned from the V4l?
> 
> ## Thanks,
> 
> kou

Thanks a lot kou, you’re a star.  
Cheers, Mike
