~thomas-deruyter-3/qreator/qreator

« back to all changes in this revision

Viewing changes to qreator/QRCode.py

  • Committer: David Planella
  • Date: 2012-01-22 09:32:24 UTC
  • Revision ID: david.planella@ubuntu.com-20120122093224-k7martvnj1mdhjjn
Moved the QR generation code to a separate module, independent of the UI

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import qrencode
 
4
import Image
 
5
import ImageOps
 
6
import cairo
 
7
import array
 
8
import Geoclue
 
9
#import osmgpsmap
 
10
 
 
11
POS_PROVIDER = 'Ubuntu GeoIP'
 
12
 
 
13
#    self.osm = osmgpsmap.GpsMap()
 
14
 
 
15
#    self.osm.layer_add(
 
16
#                    osmgpsmap.GpsMapOsd(
 
17
#                        show_dpad=True,
 
18
#                        show_zoom=True))
 
19
#        self.osm.layer_add(
 
20
#                    DummyLayer())
 
21
 
 
22
 
 
23
class QRCodeSize(object):
 
24
    SMALL = 120
 
25
    MEDIUM = 230
 
26
    LARGE = 350
 
27
 
 
28
 
 
29
class QRCode(object):
 
30
 
 
31
    def __init__(self, output_size = QRCodeSize.MEDIUM):
 
32
        self.output_size = output_size
 
33
        self.qrcode_size = 0
 
34
        self.pixel_size = 1
 
35
        self.image = None
 
36
        self.cr_surface = None
 
37
 
 
38
    def encode(self, text):
 
39
        return self._encode_to_pil(text)
 
40
 
 
41
    def encode_to_cairo(self, text):
 
42
        self._encode_to_pil(text)
 
43
 
 
44
        # Convert the Python PIL image to a Cairo surface.
 
45
        # Notice the conversion to BGRA that Cairo expects
 
46
        # See http://cairographics.org/pythoncairopil/ and
 
47
        # http://mail.gnome.org/archives/gtkmm-list/2007-May/msg00111.html
 
48
        bytearr = array.array('B', self.image.tostring("raw", "BGRA", 0, 1))
 
49
        height, width = self.image.size
 
50
        print self.image.size
 
51
 
 
52
        self.cr_surface = cairo.ImageSurface.create_for_data(bytearr,
 
53
                                                     cairo.FORMAT_ARGB32,
 
54
                                                     width, height,
 
55
                                                     width * 4)
 
56
 
 
57
        return self.cr_surface
 
58
 
 
59
    def _encode_to_pil(self, text):
 
60
        version, self.qrcode_size, self.image = qrencode.encode(text)
 
61
        self.image = self.image.convert('RGBA')
 
62
        self._resize()
 
63
        self._add_border()
 
64
 
 
65
        return self.image
 
66
 
 
67
    def _resize(self):
 
68
        self._set_pixel_size(self.qrcode_size)
 
69
        print self.pixel_size, self.qrcode_size
 
70
        self.image = self.image.resize((self.pixel_size * self.qrcode_size,
 
71
                                        self.pixel_size * self.qrcode_size),
 
72
                                       Image.NEAREST)
 
73
 
 
74
    def _set_pixel_size(self, barcode_orig_size):
 
75
        pixel_size = 1
 
76
 
 
77
        print pixel_size * barcode_orig_size, self.output_size
 
78
        while (pixel_size * barcode_orig_size < self.output_size):
 
79
            pixel_size += 1
 
80
 
 
81
        while ((pixel_size * barcode_orig_size > self.output_size) or \
 
82
                ((pixel_size * barcode_orig_size) % 2 != 0) or \
 
83
                (pixel_size * barcode_orig_size + 2 * pixel_size > \
 
84
                    self.output_size)):
 
85
            pixel_size -= 1
 
86
 
 
87
            # FIXME: it needs to automatically go up to the next size,
 
88
            # rather than hardcoding it as now
 
89
            if pixel_size == 0:
 
90
                self.output_size = QRCodeSize.MEDIUM
 
91
 
 
92
        self.pixel_size = pixel_size
 
93
 
 
94
    def _add_border(self):
 
95
        # Add a border
 
96
        border_size = (self.output_size - self.image.size[0]) / 2
 
97
        self.image = ImageOps.expand(self.image, border=border_size,
 
98
                                     fill='white')
 
99
 
 
100
 
 
101
def get_current_location():
 
102
    location = Geoclue.DiscoverLocation()
 
103
    location.init()
 
104
    location.set_position_provider(POS_PROVIDER)
 
105
    position = location.get_location_info()
 
106
    return position['latitude'], position['longitude']