1414
1515
1616class ZBarDecoder :
17+ @classmethod
18+ def is_usable (cls ):
19+ return False
20+
1721 def validate_code_types (self , code_types ):
1822 available_code_types = self .get_available_code_types ()
1923
@@ -91,22 +95,36 @@ def decode(self, image, code_types):
9195 ]
9296
9397
94- available_implementations = {
95- 'pyzbar' : PyZBarDecoder ,
96- 'zbarlight' : ZBarLightDecoder ,
97- }
98+ class XZbarDecoder (ZBarDecoder ):
99+ """Proxy-like that deals with all the implementations."""
100+ available_implementations = {
101+ 'pyzbar' : PyZBarDecoder ,
102+ 'zbarlight' : ZBarLightDecoder ,
103+ }
104+ zbar_decoder = None
105+
106+ def __init__ (self ):
107+ # making it a singleton so it gets initialized once
108+ XZbarDecoder .zbar_decoder = (
109+ self .zbar_decoder or self ._get_implementation ())
110+
111+ def _get_implementation (self ):
112+ for name , implementation in self .available_implementations .items ():
113+ if implementation .is_usable ():
114+ zbar_decoder = implementation ()
115+ Logger .info ('ZBarCam: Using implementation %s' , name )
116+ return zbar_decoder
117+ else :
118+ raise ImportError (
119+ 'No zbar implementation available '
120+ f'(tried { ", " .join (self .available_implementations .keys ())} )'
121+ )
98122
123+ def get_available_code_types (self ):
124+ return self .zbar_decoder .get_available_code_types ()
99125
100- for name , implementation in available_implementations .items ():
101- if implementation .is_usable ():
102- zbar_decoder = implementation ()
103- Logger .info ('ZBarCam: Using implementation %s' , name )
104- break
105- else :
106- raise ImportError (
107- 'No zbar implementation available '
108- f'(tried { ", " .join (available_implementations .keys ())} )'
109- )
126+ def decode (self , image , code_types ):
127+ return self .zbar_decoder .decode (image , code_types )
110128
111129
112130class ZBarCam (AnchorLayout ):
@@ -119,7 +137,7 @@ class ZBarCam(AnchorLayout):
119137 symbols = ListProperty ([])
120138 Symbol = namedtuple ('Symbol' , ['type' , 'data' ])
121139 # checking all possible types by default
122- code_types = ListProperty (zbar_decoder .get_available_code_types ())
140+ code_types = ListProperty (XZbarDecoder () .get_available_code_types ())
123141
124142 def __init__ (self , ** kwargs ):
125143 # lazy loading the kv file rather than loading at module level,
@@ -170,7 +188,7 @@ def _detect_qrcode_frame(cls, texture, code_types):
170188 pil_image = PIL .Image .frombytes (mode = 'RGBA' , size = size ,
171189 data = image_data )
172190 pil_image = fix_android_image (pil_image )
173- return zbar_decoder .decode (pil_image , code_types )
191+ return XZbarDecoder () .decode (pil_image , code_types )
174192
175193 @property
176194 def xcamera (self ):
0 commit comments