mchehab / zbar

ZBar is an open source software suite for reading bar codes from various sources, including webcams. As its development stopped in 2012, I took the task of keeping it updated with the V4L2 API. This is the main repository for it. There's a clone at at LinuxTV.org, and another one at gitlab.
https://linuxtv.org/downloads/zbar/
GNU Lesser General Public License v2.1
1k stars 205 forks source link

Python bindings doesn't support binary mode #284

Open epl opened 6 months ago

epl commented 6 months ago

The Python bindings of zbar doesn't support output in binary mode.

Reproduction steps

usage: python3 example.py IMAGE.PNG

pil = Image.open(sys.argv[1]).convert('L') image = zbar.Image(*pil.size, 'Y800', pil.tobytes())

scanner = zbar.ImageScanner() scanner.parse_config('qr.binary') scanner.scan(image)

for symbol in image: print(symbol.data)

- Please run the code with the provided QR code filename as the first parameter.

### Expected behaviour

It'll decode the QR code and then print out:
```console
b'\x80\x91\xa2\xb3\xc4\xd5\xe6\xf7\x88\x99\xaa\xbb\xcc\xdd\xee\xff'

Observed Behavior

It'll throw an exception and print out:

Traceback (most recent call last):
  File "/tmp/example.py", line 18, in <module>
    print(symbol.data)
          ^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

Analysis

I suspect the issue is on this line where it forces the symbol data to be an Unicode string (latest commit as of this issue is a549566): https://github.com/mchehab/zbar/blob/master/python/symbol.c#L105

I'm not sure what's the best suggested fix, but I imagine (at a very high level), it's to provide a means to propagate the binary config into symbol.c and then skip the PyUnicode_FromStringAndSize() call under those circumstances.

Beyond that, I'm not quite sure about the specifics yet. I've never extended Python with C before (though perhaps now is a good time to learn). To be fair, this is open source, so I should contribute a fix. But if you can provide guidance and/or code, then I'd be grateful please. Else, I'll try to learn how to do it and this issue shall be a notice for others.

Issue also related to #64 and #268