11from __future__ import annotations
22from functools import cached_property
33from pathlib import Path
4+ from typing import Union , Optional , Tuple , List , Dict
5+ import numpy as np
46
57from .matrix import AztecMatrix
68from .detection import BullseyeDetector
1416
1517
1618class AztecDecoder :
19+ """High-level façade that decodes an Aztec symbol from an image in **one line**.
20+
21+ This class orchestrates all lower-level components:
22+ :class:`AztecMatrix`, :class:`BullseyeDetector`,
23+ :class:`OrientationManager`, :class:`ModeReader` and
24+ :class:`CodewordReader`.
25+
26+ Parameters
27+ ----------
28+ image_path : Union[str, Path]
29+ Path to the image file **already cropped** to the Aztec symbol.
30+ auto_orient : Optional[bool], default ``True``
31+ If *True*, the matrix is rotated automatically so that orientation
32+ patterns match the canonical position (black-white corner pattern).
33+ auto_correct : Optional[bool], default ``True``
34+ Apply Reed-Solomon correction on the *data* code-words before
35+ high-level decoding. Disable it for debugging corrupted symbols.
36+ mode_auto_correct : Optional[bool], default ``True``
37+ Apply Reed-Solomon correction on the *mode message* (layers, data
38+ words, ecc bits).
39+
40+ Attributes
41+ ----------
42+ matrix : numpy.ndarray
43+ Final, possibly rotated, binary matrix (0/1) of the symbol.
44+ aztec_type : AztecType
45+ ``COMPACT`` or ``FULL`` deduced from the bull's-eye.
46+ bullseye_bounds : Tuple[int, int, int, int]
47+ Coordinates of the bull's-eye corners.
48+ mode_info : Dict[str, Union[int, List[int]]]
49+ Parsed mode fields - keys ``layers``, ``data_words``, ``ecc_bits``.
50+ bitmap : numpy.ndarray
51+ Raw bit-stream extracted from the data spiral (before ECC).
52+ corrected_bits : List[int]
53+ Bit-stream after Reed-Solomon correction and bit-stuff removal.
54+ message : str
55+ Decoded user message (lazy property, evaluated once).
56+
57+ Raises
58+ ------
59+ InvalidParameterError
60+ *image_path* does not point to an existing file.
61+ BullseyeDetectionError, OrientationError, BitReadError, etc.
62+ Any lower-level exception is propagated so the caller can catch
63+ precisely the failing phase.
64+
65+ Examples
66+ --------
67+ >>> from aztec_tool import AztecDecoder
68+ >>> dec = AztecDecoder("ticket.png")
69+ >>> dec.message # same as dec.decode()
70+ 'EVENT: Concert
71+ ROW 12 SEAT 34'
72+
73+ A one-liner helper is also available:
74+
75+ >>> from aztec_tool import decode
76+ >>> decode("hello.png")
77+ 'Hello, world!'
78+ """
79+
1780 def __init__ (
1881 self ,
19- image_path : str | Path ,
82+ image_path : Union [ str , Path ] ,
2083 * ,
21- auto_orient : bool = True ,
22- auto_correct : bool = True ,
23- mode_auto_correct : bool = True ,
84+ auto_orient : Optional [ bool ] = True ,
85+ auto_correct : Optional [ bool ] = True ,
86+ mode_auto_correct : Optional [ bool ] = True ,
2487 ) -> None :
2588 self .image_path = Path (image_path )
2689 if not self .image_path .exists ():
@@ -32,42 +95,42 @@ def __init__(
3295 self ._mode_auto_correct = mode_auto_correct
3396
3497 @cached_property
35- def _raw_matrix (self ):
98+ def _raw_matrix (self ) -> np . ndarray :
3699 return AztecMatrix (str (self .image_path )).matrix
37100
38101 @cached_property
39102 def _bullseye (self ) -> BullseyeDetector :
40103 return BullseyeDetector (self ._raw_matrix )
41104
42105 @cached_property
43- def bullseye_bounds (self ):
106+ def bullseye_bounds (self ) -> Tuple [ int , int , int , int ] :
44107 return self ._bullseye .bounds
45108
46109 @cached_property
47110 def aztec_type (self ) -> AztecType :
48111 return self ._bullseye .aztec_type
49112
50113 @cached_property
51- def matrix (self ):
114+ def matrix (self ) -> np . ndarray :
52115 if not self ._auto_orient :
53116 return self ._raw_matrix
54117 return OrientationManager (
55118 self ._raw_matrix , self .bullseye_bounds
56119 ).rotate_if_needed ()
57120
58121 @cached_property
59- def _mode (self ):
122+ def _mode (self ) -> ModeReader :
60123 bullseye = BullseyeDetector (self .matrix )
61124 return ModeReader (
62125 self .matrix , bullseye .bounds , bullseye .aztec_type , self ._mode_auto_correct
63126 )
64127
65128 @cached_property
66- def mode_info (self ):
129+ def mode_info (self ) -> Dict [ str , Union [ int , List [ int ]]] :
67130 return self ._mode .mode_fields
68131
69132 @cached_property
70- def _codewords (self ):
133+ def _codewords (self ) -> CodewordReader :
71134 return CodewordReader (
72135 self .matrix ,
73136 self .mode_info ["layers" ],
@@ -77,16 +140,17 @@ def _codewords(self):
77140 )
78141
79142 @cached_property
80- def bitmap (self ):
143+ def bitmap (self ) -> np . ndarray :
81144 return self ._codewords .bitmap
82145
83146 @cached_property
84- def corrected_bits (self ):
147+ def corrected_bits (self ) -> List [ int ] :
85148 return self ._codewords .corrected_bits
86149
87150 @cached_property
88151 def message (self ) -> str :
89152 return self ._codewords .decoded_string
90153
91154 def decode (self ) -> str :
155+ """Return the decoded user message (alias of :pyattr:`message`)."""
92156 return self .message
0 commit comments