Advertisement
lepe

opencv-vala-gtk-example

Jan 18th, 2012
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 1.78 KB | None | 0 0
  1. /*
  2.  * Simple example on how to load an image using OpenCV and display it in Gtk
  3.  * Compile with: valac --pkg opencv --pkg gdk-pixbuf-2.0 --pkg gtk+-2.0 opencv.vala
  4.  * Author: A.Lepe (dev@alepe.com)
  5.  */
  6.  
  7. using OpenCV;
  8. using Gdk;
  9. using Gtk;
  10.  
  11. class Utils {
  12.     public static void PixbufDestroyNotify (uint8* pixels) {
  13.             GLib.stdout.printf("delete buffer\n");
  14.             delete pixels;
  15.     }
  16.     public static Gtk.Widget convertOpenCvToGtk(IPL.Image gtkMask) {
  17.         Gtk.Widget gtkImg;
  18.         Gdk.Pixbuf pix = new Gdk.Pixbuf.from_data (
  19.                 gtkMask.image_data,//Raw image data
  20.                 Gdk.Colorspace.RGB,                     //Colorspace
  21.                 false,                                  //Has alpha channel or not
  22.                 gtkMask.depth,                          //Bits per pixel
  23.                 gtkMask.width,                          //Width of image
  24.                 gtkMask.height,                         //Height of image
  25.                 gtkMask.width_step,                     //Row stride
  26.                 Utils.PixbufDestroyNotify               //destroy function (optional)
  27.         );
  28.         gtkImg = new Gtk.Image.from_pixbuf (pix);
  29.         return gtkImg;
  30.     }
  31. }
  32.  
  33. void main (string[] args) {
  34.     IPL.Image img = new IPL.Image.load("/var/www/test/image.jpg");
  35.     stdout.printf("Width: %d\n", img.width);
  36.     stdout.printf("Height: %d\n", img.height);
  37.     Gtk.init (ref args);
  38.  
  39.     var win = new Gtk.Window ();
  40.     win.set_size_request (img.width,img.height);
  41.     win.border_width = 5;
  42.     win.title = "Widget test";
  43.     win.destroy.connect (Gtk.main_quit);
  44.  
  45.     var frame = new Frame ("Example Vala Widget");
  46.     win.add (frame);
  47.     frame.add (Utils.convertOpenCvToGtk(img));
  48.  
  49.     win.show_all ();
  50.     Gtk.main ();
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement