~thomas-deruyter-3/qreator/qreator

« back to all changes in this revision

Viewing changes to qreator/QRCode.py

  • Committer: David Planella
  • Date: 2012-02-10 19:18:24 UTC
  • Revision ID: david.planella@ubuntu.com-20120210191824-2l8s07g9ysrdg93y
Added some documentation, cleaned up the QRCode's object methods a bit

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
    MEDIUM = 230
17
17
    LARGE = 350
18
18
 
 
19
class QRCodeOutput(object):
 
20
    PIL_IMAGE = 0
 
21
    CAIRO_SURFACE = 1
19
22
 
20
23
class QRCode(object):
21
24
 
26
29
        self.image = None
27
30
        self.cr_surface = None
28
31
 
29
 
    def encode(self, text):
30
 
        return self._encode_to_pil(text)
31
 
 
32
 
    def encode_to_cairo(self, text):
 
32
    def encode(self, text, output_type=QRCodeOutput.PIL_IMAGE):
 
33
        '''Encodes the given text and returns a QR code in the given format'''
 
34
        
 
35
        if output_type == QRCodeOutput.PIL_IMAGE:
 
36
            qr_code = self._encode_to_pil(text)
 
37
        elif output_type == QRCodeOutput.CAIRO_SURFACE:
 
38
            qr_code = self._encode_to_cairo(text)
 
39
 
 
40
        return qr_code
 
41
 
 
42
    def _encode_to_cairo(self, text):
 
43
        '''Encodes the given text and returns a Cairo surface'''
33
44
        self._encode_to_pil(text)
34
45
 
35
46
        # Convert the Python PIL image to a Cairo surface.
38
49
        # http://mail.gnome.org/archives/gtkmm-list/2007-May/msg00111.html
39
50
        bytearr = array.array('B', self.image.tostring("raw", "BGRA", 0, 1))
40
51
        height, width = self.image.size
41
 
        print self.image.size
 
52
        #print self.image.size
42
53
 
43
54
        self.cr_surface = cairo.ImageSurface.create_for_data(bytearr,
44
55
                                                     cairo.FORMAT_ARGB32,
48
59
        return self.cr_surface
49
60
 
50
61
    def _encode_to_pil(self, text):
 
62
        '''Encodes the given text and returns a Python PIL Image'''
51
63
        version, self.qrcode_size, self.image = qrencode.encode(text)
52
64
        self.image = self.image.convert('RGBA')
53
65
        self._resize()
83
95
        self.pixel_size = pixel_size
84
96
 
85
97
    def _add_border(self):
 
98
        '''Adds a border to the QR code'''
86
99
        # Add a border
87
100
        border_size = (self.output_size - self.image.size[0]) / 2
88
101
        self.image = ImageOps.expand(self.image, border=border_size,