View Single Post
daperl's Avatar
Posts: 2,427 | Thanked: 2,986 times | Joined on Dec 2007
#10
The script you pointed to doesn't really do anything. The following really, really bad hack will actually take a picture and create a .png file on an n800. As is, it won't work on an n900, but the changes should be obvious.

Also, the file creation is really, really slow; about 25 seconds. It's strictly just a hard-coded, proof-of-concept that a picture can be taken on a tablet with just using PyGTK and GStreamer. Obviously, plenty of room for improvement.

Code:
#! /usr/bin/env python

import string
import gtk
import gst

class ShowMe:
	def __init__(self):
		window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		window.set_title("Webcam-Viewer")
		window.connect("destroy", gtk.main_quit, "WM destroy")
		vbox = gtk.VBox()
		window.add(vbox)
		self.movie_window = gtk.DrawingArea()
		vbox.add(self.movie_window)
		hbox = gtk.HBox()
		vbox.pack_start(hbox, False)
		hbox.set_border_width(10)
		hbox.pack_start(gtk.Label())
                self.takePicture = 0
		self.button0 = gtk.Button("Snap")
		self.button0.connect("clicked", self.onTakePicture)
		hbox.pack_start(self.button0, False)
		self.button = gtk.Button("Start")
		self.button.connect("clicked", self.start_stop)
		hbox.pack_start(self.button, False)
		self.button2 = gtk.Button("Quit")
		self.button2.connect("clicked", self.exit)
		hbox.pack_start(self.button2, False)
		hbox.add(gtk.Label())
		window.show_all()

                if 1 == 1:
                    self.player = gst.Pipeline('ThePipe')
                    src = gst.element_factory_make("gconfv4l2src", "src")
                    self.player.add(src)
                    for p in src.pads():
                        #print p.get_caps().to_string()
                        print p.get_name()
                    caps = gst.element_factory_make("capsfilter", "caps")
                    caps.set_property('caps', gst.caps_from_string(
                        'video/x-raw-rgb,width=352,height=288,bpp=16,depth=16,\
                        framerate=15/1'))
                    #caps.set_property('caps', gst.caps_from_string(
                        #'video/x-raw-rgb,width=352,height=288,\
                        #framerate=15/1'))
                        #red_mask=224,green_mask=28,blue_mask=3,framerate=15/1'))
                    self.player.add(caps)
                    filt = gst.element_factory_make("ffmpegcolorspace", "filt")
                    self.player.add(filt)
                    caps2 = gst.element_factory_make("capsfilter", "caps2")
                    caps2.set_property('caps', gst.caps_from_string(
                        'video/x-raw-rgb,width=352,height=288,bpp=16,depth=16,\
                        framerate=15/1'))
                    self.player.add(caps2)
                    sink = gst.element_factory_make("xvimagesink", "sink")
                    self.player.add(sink)
                    pad = src.get_pad('src')
                    pad.add_buffer_probe(self.doBuffer)
                    src.link(caps)
                    caps.link(filt)
                    filt.link(caps2)
                    caps2.link(sink)

		# Set up the gstreamer pipeline
		#self.player = gst.parse_launch ('gconfv4l2src ! video/x-raw-yuv,width=352,height=288,framerate=(fraction)15/1 ! autovideosink')
		#self.player = gst.parse_launch ('gconfv4l2src ! video/x-raw-yuv,width=352,height=288,framerate=(fraction)15/1 ! tee name=qole qole. ! ffmpegcolorspace ! queue ! filesink location=qole.raw qole. ! queue ! autovideosink')
		#self.player = gst.parse_launch ('gconfv4l2src ! video/x-raw-rgb,width=352,height=288,framerate=(fraction)15/1 ! tee name=qole qole. ! ffmpegcolorspace ! jpegenc ! filesink location=qole.raw qole. ! queue ! autovideosink')
		#self.player = gst.parse_launch ('v4l2src ! autovideosink')

		bus = self.player.get_bus()
		bus.add_signal_watch()
		bus.enable_sync_message_emission()
		bus.connect("message", self.on_message)
		bus.connect("sync-message::element", self.on_sync_message)

	def onTakePicture(self, w):
            self.takePicture = 1

	def doBuffer(self, pad, buffer):
            if self.takePicture:
                self.takePicture = 0
                #print buffer.get_caps()
                # 63488 2016 31
                # 0xf8  0x07,0xe0  0x1f
                p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,352,288)
                pa = p.get_pixels()
                pal = list(pa)
                for i in range(0,len(buffer)/2):
                    pal[i*3] = "%c" % (0xf8 & ord(buffer[i*2+1]))
                    pal[i*3+1] = "%c" % (((0x07 & ord(buffer[i*2+1])) << 5) |\
                        ((0xe0 & ord(buffer[i*2])) >> 5))
                    pal[i*3+2] = "%c" % ((0x1f & ord(buffer[i*2])) << 3)
                js = string.join(pal,'')
                pb = gtk.gdk.pixbuf_new_from_data(js,gtk.gdk.COLORSPACE_RGB,                        False,8,352,288,1056)
                pb.save('/home/user/MyDocs/.images/daperl00.png','png')
                print pb.get_width(),pb.get_height()
            return True

	def start_stop(self, w):
		if self.button.get_label() == "Start":
			self.button.set_label("Stop")
			self.player.set_state(gst.STATE_PLAYING)
		else:
			self.player.set_state(gst.STATE_NULL)
			self.button.set_label("Start")

	def exit(self, widget, data=None):
		gtk.main_quit()

	def on_message(self, bus, message):
		t = message.type
		if t == gst.MESSAGE_EOS:
			self.player.set_state(gst.STATE_NULL)
			self.button.set_label("Start")
		elif t == gst.MESSAGE_ERROR:
			err, debug = message.parse_error()
			print "Error: %s" % err, debug
			self.player.set_state(gst.STATE_NULL)
			self.button.set_label("Start")

	def on_sync_message(self, bus, message):
		if message.structure is None:
			return
		message_name = message.structure.get_name()
		if message_name == "prepare-xwindow-id":
			# Assign the viewport
			imagesink = message.src
			imagesink.set_property("force-aspect-ratio", True)
			imagesink.set_xwindow_id(self.movie_window.window.xid)

if __name__ == "__main__":
    gtk.gdk.threads_init()
    ShowMe()
    gtk.main()
__________________
N9: Go white or go home
 

The Following 3 Users Say Thank You to daperl For This Useful Post: