-
Notifications
You must be signed in to change notification settings - Fork 3
/
qr_reader.py
57 lines (50 loc) · 1.48 KB
/
qr_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from xml.dom.minidom import parse, parseString
from subprocess import Popen, PIPE
def take_image():
p = Popen(['fswebcam', '--no-banner', '--greyscale', '-r', '640x480', 'assets/scan.jpg', '-S', '1'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate()
if p.returncode == 0:
return True
else:
return False
def read_image():
p = Popen(['zbarimg', '-q', '--xml', 'assets/scan.jpg'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate()
if p.returncode == 0:
dom = parseString(output)
nodes = dom.getElementsByTagName('data')
if nodes.length == 0:
return False
return nodes[0].firstChild.data
else:
return False
def parse_url(url):
#separate address & params
result = { 'success': True, 'params': {} }
split = url.split('?')
#extract address
address_half = split[0]
address_split = address_half.split('bitcoin:')
if len(address_split) == 2:
result['address'] = address_split[1]
else:
return False
#extract params
if len(split) > 1:
params_half = split[1]
params = params_half.split('&')
for param in params:
print param
pair = param.split('=')
result['params'][pair[0]] = pair[1]
return result
def read_qr():
pic = take_image()
if pic:
result = read_image()
if (result):
return parse_url(result)
else:
return { 'success': False, 'message': "Failed to find QR code" }
else:
return { 'success': False, 'message': "Failed to take image" }