086f6ae84eb22b2da702d7e27e5610a6f498fb6e
[oweals/gnunet.git] / src / util / gnunet-qr.py.in
1 #!@PYTHON@
2 import sys
3 import getopt
4 from sys import argv
5 try:
6     import zbar
7 except ImportError as e:
8     print 'Cannot run gnunet-qr, please install zbar-python'
9     sys.exit (1)
10
11 def help ():
12  print 'gnunet-qr\n\
13 Scan a QR code using a video device and import\n\
14 Arguments mandatory for long options are also mandatory for short options.\n\
15   -d, --device=DEVICE        use device DEVICE\n\
16   -h, --help                 print this help\n\
17 Report bugs to gnunet-developers@gnu.org.\n\
18 GNUnet home page: http://www.gnu.org/software/gnunet/\n\
19 General help using GNU software: http://www.gnu.org/gethelp/'
20
21
22 if __name__ == '__main__':
23     # Parse arguments
24     try:
25         opts, args = getopt.gnu_getopt(sys.argv[1:], "hd:", ["help", "device"])
26     except getopt.GetoptError as e:
27         help ()
28         print str (e)
29         exit (1)
30     
31     device = '/dev/video0'
32     for o,a in opts:
33         if o in ("-h", "--help"):
34             help ()            
35             sys.exit (0)
36         elif o in ("-d", "--device"):
37             device = a
38     # create a Processor
39     proc = zbar.Processor()
40
41     # configure the Processor
42     proc.parse_config('enable')
43
44     # initialize the Processor
45     try:
46         proc.init(device)
47     except Exception as e:
48         print 'Failed to open device ' + device
49         exit (1)
50
51     # enable the preview window
52     proc.visible = True
53
54     # read at least one barcode (or until window closed)
55     try:
56         proc.process_one()
57     except Exception as e:
58         # Window was closed without finding code
59         exit (1)
60
61     # hide the preview window
62     proc.visible = False
63
64     # extract results
65     for symbol in proc.results:
66         # do something useful with results
67         print 'Found ', symbol.type, ' symbol ', '"%s"' % symbol.data