-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathroundtrip_qr.py
More file actions
executable file
·128 lines (102 loc) · 3.35 KB
/
Copy pathroundtrip_qr.py
File metadata and controls
executable file
·128 lines (102 loc) · 3.35 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
# This file is part of Checkbox.
#
# Copyright 2020 Canonical Ltd.
# Written by:
# Jonathan Cave <jonathan.cave@canonical.com>
#
# Checkbox 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.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY 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 Checkbox. If not, see <http://www.gnu.org/licenses/>.
import os
import random
import string
import time
import subprocess as sp
import sys
import pyqrcode
import zbar
from PIL import Image
def capture_rpi(name):
import picamera
file = os.path.join(
os.path.expandvars("$PLAINBOX_SESSION_SHARE"),
"{}_qrcapture.png".format(name),
)
with picamera.PiCamera() as camera:
time.sleep(2)
camera.capture(file)
return file
def capture_webcam(name):
file = os.path.join(
os.path.expandvars("$PLAINBOX_SESSION_SHARE"),
"{}_qrcapture.jpg".format(name),
)
cmd = (
"gst-launch-1.0 v4l2src device=/dev/{} num-buffers=1 ! jpegenc !"
"filesink location={}"
).format(name, file)
try:
sp.check_call(cmd, shell=True)
except (sp.CalledProcessError, OSError) as e:
print(e)
raise SystemExit("ERROR: failed to capture image")
return file
def generate_data():
return "".join(random.choice(string.ascii_letters) for i in range(10))
def generate_qr_code(data):
return pyqrcode.create(data)
def display_code(qr):
with open("/dev/tty0", "wb+", buffering=0) as term:
# clear the tty so the qr is always printed at the top of the sceen
term.write(str.encode("\033c"))
# print the qr code
term.write(qr.terminal(quiet_zone=1).encode())
def decode_image(filename):
scanner = zbar.ImageScanner()
scanner.parse_config("enable")
pil = Image.open(filename).convert("L")
width, height = pil.size
raw = pil.tobytes()
image = zbar.Image(width, height, "Y800", raw)
scanner.scan(image)
result = None
for code in image:
result = code.data
del image
if result is None:
raise SystemExit("ERROR: no qrcodes decoded")
return result
def main():
if len(sys.argv) != 2:
raise SystemExit("ERROR: expected a device name")
name = sys.argv[1]
print("Testing device name: {}\n".format(name))
test_str = generate_data()
print("Input string: {}".format(test_str), flush=True)
print("Generating QR code...", flush=True)
qr = generate_qr_code(test_str)
print("Displaying on screen", flush=True)
display_code(qr)
print("Capture image of screen", flush=True)
if name == "vchiq":
file = capture_rpi(name)
else:
file = capture_webcam(name)
print("Image {} captured".format(file))
print("Decoding image file", flush=True)
result = decode_image(file)
print("Decoded data: {}".format(result))
if result != test_str:
raise SystemExit("FAIL: decoded data does not match input")
print("PASS: decoded data and input match")
if __name__ == "__main__":
main()