Permalink
Browse files

Now, instead of a dict, `quirc.decode` returns object with the same a…

…ttributes as dict keys
  • Loading branch information...
1 parent 5fa3f81 commit 4c8c62b015e40c481e4826885fbc28b82017f7ef @svartalf committed May 25, 2012
Showing with 33 additions and 15 deletions.
  1. +2 −2 docs/source/usage/high-level.rst
  2. +3 −0 docs/source/usage/low-level.rst
  3. +23 −8 quirc/base.py
  4. +5 −5 tests/test_quirc.py
@@ -7,10 +7,10 @@ It is very simple in use::
from PIL import Image
for code in quirc.decode(Image.open('images/qr-code.png')):
- print code['text']
+ print code
`quirc.decode` function returns generator, which contains results of QR codes recognition.
-Each item is `dict` object with those keys:
+Each item is an object with those attributes:
corners
Tuple with the four corners of the QR code, from top left, clockwise
@@ -3,6 +3,8 @@ Low-level API
Low-level API fully copies the C API and contains all in the `quirc.api` module.
+**Warning**: you will need to use *ctypes* here manually.
+
.. automodule:: quirc.api
@@ -47,3 +49,4 @@ Usage
print ctypes.string_at(data.payload, data.payload_len)
api.destroy(obj)
+
View
@@ -12,6 +12,21 @@
except ImportError:
from PIL import Image
+class Code(object):
+ """Structure for storing extracted QR code data"""
+
+ __slots__ = ('corners', 'size', 'version', 'ecc_level', 'data_type', 'text')
+
+ def __init__(self, corners, size, version, ecc_level, data_type, text):
+ self.corners = corners
+ self.size = size
+ self.version = version
+ self.ecc_level = ecc_level
+ self.data_type = data_type
+ self.text = text
+
+ def __repr__(self):
+ return self.text
def decode(image):
"""Recognize image and return generator with all the available QR codes
@@ -48,14 +63,14 @@ def decode(image):
api.extract(obj, i, code)
api.decode(code, data)
- yield {
- 'corners': tuple([(corner.x, corner.y) for corner in code.corners]),
- 'size': code.size,
- 'version': data.version,
- 'ecc_level': data.ecc_level,
- 'data_type': data.data_type,
- 'text': ctypes.string_at(data.payload, data.payload_len),
- }
+ yield Code(
+ tuple([(corner.x, corner.y) for corner in code.corners]),
+ code.size,
+ data.version,
+ data.ecc_level,
+ data.data_type,
+ ctypes.string_at(data.payload, data.payload_len),
+ )
api.destroy(obj)
View
@@ -40,9 +40,9 @@ def test_pil(self):
code = result[0]
- self.assertEqual(code['data_type'], 4)
- self.assertEqual(code['ecc_level'], 0)
- self.assertEqual(code['size'], 29)
+ self.assertEqual(code.data_type, 4)
+ self.assertEqual(code.ecc_level, 0)
+ self.assertEqual(code.size, 29)
- self.assertTupleEqual(code['corners'], ((16, 16), (132, 16), (132, 132), (16, 132)))
- self.assertEqual(code['text'], 'https://github.com/svartalf/python-quirc')
+ self.assertTupleEqual(code.corners, ((16, 16), (132, 16), (132, 132), (16, 132)))
+ self.assertEqual(code.text, str(code), 'https://github.com/svartalf/python-quirc')

0 comments on commit 4c8c62b

Please sign in to comment.