Commit 33e79afc authored by Björn Guth's avatar Björn Guth

added qr reader to repository

parent d0af37b3
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys, os, re, subprocess, httplib
from threading import Timer
import pygtk, gtk, gobject
import pygst
pygst.require("0.10")
import gst
import zbar, Image, string, urllib
class GTK_Main:
def __init__(self):
self.clean()
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("Webcam-Viewer")
window.set_default_size(500, 400)
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.button = gtk.Button("Quit")
self.button.connect("clicked", self.exit)
hbox.pack_start(self.button, False)
self.button_restart = gtk.Button('Restart')
self.button_restart.connect('clicked',self.restart)
hbox.pack_start(self.button_restart,True)
hbox.add(gtk.Label())
window.show_all()
# Set up the gstreamer pipepile
self.player = gst.parse_launch ("""v4l2src !
tee name=t ! queue ! autovideosink t.
! queue ! videorate ! video/x-raw-yuv, width=800, height=600, framerate=1/1
! jpegenc ! multifilesink location=snapshot-%03d.jpg""")
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)
self.player.set_state(gst.STATE_PLAYING)
self.scan()
def exit(self, widget, data=None):
self.t.cancel()
self.clean()
gtk.main_quit()
def restart(self, widget, data=None):
self.clean()
self.scan()
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)
def scan(self):
files = os.listdir(".")
files = filter(lambda x: re.search('^snapshot-[0-9]*.jpg$', x) != None, files)
for f in files:
print("processing image: "+str(f))
self.process_qr(str(f))
os.remove(f)
self.t = Timer(3.0, self.scan)
self.t.start()
def process_qr(self, picture):
scanner = zbar.ImageScanner()
scanner.parse_config('enable')
pil = Image.open(picture).convert('L')
width,height = pil.size
raw = pil.tostring()
image = zbar.Image(width,height,'Y800',raw)
scanner.scan(image)
no_qr = True
result = ''
for symbol in image:
url = symbol.data
print(url)
if url.find('http://fsmpi.rwth-aachen.de/lip/view/') >= 0:
os.system('firefox -new-tab '+url)
else:
self.invalid_code()
no_qr = False
del(image)
if no_qr:
print('\033[1;31mno QR code found\033[1;m')
else:
exit()
def stop(self):
exit()
def clean(self):
files = os.listdir(".")
files = filter(lambda x: re.search('^snapshot-[0-9]*.jpg$', x) != None, files)
for f in files:
os.remove(f)
def invalid_code(self):
print('\033[1;41m _ _ _ _ _ ___ _ _ ___ ___ ___ ___ ___ ___ ___ \033[1;m')
print('\033[1;41m | || \| || | |/ - \| | | || \ / \| - | / _|/ \| \| __| \033[1;m')
print('\033[1;41m | || \ || | || | || |_ | || | | | | || \ | (_ | | || | || __| \033[1;m')
print('\033[1;41m |_||_\__| \_/ |_|_||___||_||___/ \__\_|_|\_\ \___|\___/|___/|___| \033[1;m')
print('\033[1;41m \033[1;m')
g = GTK_Main()
gtk.gdk.threads_init()
gtk.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment