NaturalHistoryMuseum / pyzbar

Read one-dimensional barcodes and QR codes from Python 2 and 3.
MIT License
718 stars 175 forks source link

-1 on uint? "4294967295 is not a valid ZBarOrientation" #139

Open purplesword opened 2 years ago

purplesword commented 2 years ago

Hi, on a few rare occasions I got ValueError: 4294967295 is not a valid ZBarOrientation on some images. I assume it was due to -1 assigned to uint and got stored as UINT_MAX?

Enviroment:

Traceback:

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_957744/3082549785.py in <module>
      1 from pyzbar import pyzbar
----> 2 decoded = pyzbar.decode(gray_image)
      3 decoded

~/.virtualenvs/py397/lib/python3.9/site-packages/pyzbar/pyzbar.py in decode(image, symbols)
    232                 raise PyZbarError('Unsupported image format')
    233             else:
--> 234                 results.extend(_decode_symbols(_symbols_for_image(img)))
    235 
    236     return results

~/.virtualenvs/py397/lib/python3.9/site-packages/pyzbar/pyzbar.py in _decode_symbols(symbols)
    125 
    126         if zbar_symbol_get_orientation:
--> 127             orientation = ZBarOrientation(zbar_symbol_get_orientation(symbol)).name
    128         else:
    129             orientation = None

/usr/local/lib/python3.9/enum.py in __call__(cls, value, names, module, qualname, type, start)
    382         """
    383         if names is None:  # simple value lookup
--> 384             return cls.__new__(cls, value)
    385         # otherwise, functional API: we're creating a new Enum type
    386         return cls._create_(

/usr/local/lib/python3.9/enum.py in __new__(cls, value)
    700                 ve_exc = ValueError("%r is not a valid %s" % (value, cls.__qualname__))
    701                 if result is None and exc is None:
--> 702                     raise ve_exc
    703                 elif exc is None:
    704                     exc = TypeError(

ValueError: 4294967295 is not a valid ZBarOrientation

Unfortunately I cannot share the original bug doc due to privacy. I will try if I can reproduce the error on some shareable anonymized image. But I guess it's some -1 treated as 4294967295?

simon-staal commented 1 year ago

This issue affects me as well, I've done a little more digging and I think I've found the problematic lines. The ZBarOrientation enum class specifies an UNKNOWN type as -1 here. However, when setting zbar_symbol_get_orientation, the c_uint type is passed here. Presumably if this was changed to c_int this would solve this issue? Not sure if this library is maintained at all anymore, but providing this info just in case.

infacc commented 1 year ago

Same issue ...

I concur with the analysis that it is caused by the ZBarOrientation UNKNOWN (-1) being interpreted as unsigned integer.

It seems the exception is thrown after the QR code has been decoded. Therefore, one can set a breakpoint at the line causing the exception: https://github.com/NaturalHistoryMuseum/pyzbar/blob/ff2892666fa7cd305f0a95f9808159809c6fc222/pyzbar/pyzbar.py#L127 and look at the data variable in a debugger. It should show the QR code data.

I also found a QR code online that causes this problem. The one on the bottom right here:

qr-code-error