Logo Search packages:      
Sourcecode: quickly-widgets version File versions  Download package

camera_button.py

### BEGIN LICENSE
# Copyright (C) 2010 Rick Spencer rick.spencer@canonical.com
#This program is free software: you can redistribute it and/or modify it 
#under the terms of the GNU General Public License version 3, as published 
#by the Free Software Foundation.
#
#This program is distributed in the hope that it will be useful, but 
#WITHOUT ANY WARRANTY; without even the implied warranties of 
#MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
#PURPOSE.  See the GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License along 
#with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

import gtk
import gobject
import pygame.camera
import pygame.surface

class CameraButton(gtk.VBox):

    def __init__(self, capture=False):
        gtk.VBox.__init__(self, False, 0)
        pygame.camera.init()
        self.__cameras = pygame.camera.list_cameras()
        self.__camera = None
        self.__capture = False

        if len(self.__cameras) == 1:
            self.set_camera(self.__cameras[0])
    
        if len(self.__cameras) > 1:
            model = gtk.ListStore(gobject.TYPE_STRING,gobject.TYPE_STRING)
            for i, c in enumerate(self.__cameras):
                model.append(["Camera " + str(i), c])
            cell = gtk.CellRendererText()
            combo = gtk.ComboBox(model)
            combo.show()
            combo.pack_start(cell, True)
            combo.add_attribute(cell, 'text', 0)
            combo.connect("changed",self.change_camera)
            combo.set_active(0)
            self.pack_start(combo, True, True)
        
        self.__image = gtk.Image()
        self.__image.set_size_request(180,180)

        self.__image.set_from_icon_name(gtk.STOCK_MISSING_IMAGE, gtk.ICON_SIZE_DIALOG)
        self.__image.show()
        self.pack_start(self.__image, False, False)

        b_box = gtk.HBox(False,1)
        bl = gtk.Label("Bright: ")
        bl.show()
        b_box.pack_start(bl, False, False)
        mn = gtk.Label("-")
        mn.show()
        b_box.pack_start(mn,False, False)        
        b_scale = gtk.HScale()
        b_scale.set_range(0,64)
        b_scale.set_value(32)
        b_scale.set_increments(1,5)
        b_scale.set_property("digits",0)
        b_scale.set_draw_value(False)
        b_scale.show()
        b_scale.connect("value-changed",self.set_brightness)
        b_box.pack_start(b_scale,True,True)
        pl = gtk.Label("+")
        pl.show()
        b_box.pack_start(pl,False, False)
        b_box.show()
        self.pack_start(b_box,False,False)        

        self.set_capture(capture)

        pic_but = gtk.Button("Take Picture")
        pic_but.show()
        pic_but.connect("clicked",self.take_picture)
        self.pack_start(pic_but, True, True)

    def __set_thumbnail_image(self):
        surf = self.__camera.get_image()
        img_string = pygame.image.tostring(surf,"RGB",False)
        width, height = self.__camera.get_size()
        pixbuf1 = gtk.gdk.pixbuf_new_from_data(img_string,
                            gtk.gdk.COLORSPACE_RGB,False,8,width,height,
                            3 * width)
        if width > height:
            sm_width = 160
            sm_height = int((float(height)/float(width)) * 160)
        else:
            sm_height = 160
            sm_width = int((float(width)/float(height)) * 160)

        pixbuf2 = pixbuf1.scale_simple(sm_width,sm_height,gtk.gdk.INTERP_BILINEAR)
        self.__image.set_from_pixbuf(pixbuf2)

        if not self.__capture:
            self.__camera.stop()
        return self.__capture

    def change_camera(self, widget, data=None):
        row = widget.get_active_iter()
        model = widget.get_model()
        camera = model.get_value(row,1)      
        self.set_camera(camera)

    def set_capture(self, capture):
        if capture:
            self.__camera.start()
            self.__thumb_tick = gobject.timeout_add(100,self.__set_thumbnail_image)
        self.__capture = capture

    def set_camera(self, camera):
        if self.__camera is not None:
            del(self.__camera)
        self.__camera = pygame.camera.Camera(camera)
        if self.__capture:
            self.__camera.start()

    def set_brightness(self, widget):
        self.__camera.set_controls(0,0,int(widget.get_value()) )

    def take_picture(self, widget, data=None):
        if not self.__capture:
            return
        surf = self.__camera.get_image()
        img_string = pygame.image.tostring(surf,"RGB",False)
        width, height = self.__camera.get_size()
        pixbuf = gtk.gdk.pixbuf_new_from_data(img_string,
                            gtk.gdk.COLORSPACE_RGB,False,8,width,height,
                            3 * width)
        self.emit('picture_taken',pixbuf)

    __gsignals__ = {'picture_taken' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
            (gobject.TYPE_PYOBJECT,)),
            'camera_availabe' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
            (gobject.TYPE_PYOBJECT,))
            }


00143 class TestWindow(gtk.Window):
    """For testing and demonstrating CameraButton.

    """

    def __init__(self):
        #create a window a VBox to hold the controls
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        self.set_title("Camera Test Window")
        windowbox = gtk.VBox(False, 2)
        windowbox.show()
        self.add(windowbox)

        cb = CameraButton(True)
        cb.show()
        windowbox.pack_start(cb)
  

        self.show()
  
        #finish wiring up the window
        self.connect("destroy", self.destroy)

        #start up gtk.main in a threaded environment  
        gtk.gdk.threads_init()
        gtk.gdk.threads_enter()
        gtk.main()
        gtk.gdk.threads_leave()

        #called when the window is destroyed
    def destroy(self, widget, data = None):
        gtk.main_quit()

if __name__== "__main__":
    test = TestWindow()


Generated by  Doxygen 1.6.0   Back to index