-
Notifications
You must be signed in to change notification settings - Fork 7
/
QRCodePage.py
72 lines (49 loc) · 1.66 KB
/
QRCodePage.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- mode: python; indent-tabs-mode: nil; -*- coding: utf-8 -*-
"""
QRCodePage.py
Copyright 2014 by Marcello Perathoner
Distributable under the GNU General Public License Version 3 or newer.
A page to generate QR-codes.
"""
from __future__ import unicode_literals
import six
from six.moves import urllib
import cherrypy
import qrcode
class QRCodePage (object):
""" Serve a QR-code as PNG image. """
def index (self, **kwargs):
""" Output QR-Code.
Parameters are:
data: the data to encode (url quoted)
ec_level: error correction level. One of: L M Q H
version: QR code version
box_size: size of one QR code box in pixel
border: width of border in boxes (should be at least 4)
"""
qr = qrcode.QRCode (
error_correction = self._get_ecl (kwargs),
version = kwargs.get ('version', None),
box_size = kwargs.get ('box_size', 10),
border = kwargs.get ('border', 4),
)
qr.add_data (urllib.parse.unquote (kwargs['data']))
qr.make (fit = True)
img = qr.make_image ()
cherrypy.response.headers['Content-Type'] = 'image/png'
buf = six.BytesIO ()
img._img.save (buf, 'PNG')
return buf.getvalue ()
@staticmethod
def _get_ecl (kwargs):
""" Get and decode error correction paramter. """
ecl = {
'L': 1,
'M': 0,
'Q': 3,
'H': 2,
}
if 'ec_level' in kwargs and kwargs['ec_level'] in ecl:
return ecl[kwargs['ec_level']]
return ecl['M']