Skip to content

Commit

Permalink
Added optional argument to decode method to allow binary data to be r…
Browse files Browse the repository at this point in the history
…eturned.
  • Loading branch information
kurtjensen committed Apr 4, 2019
1 parent a425ec3 commit 8700a05
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pylibdmtx/pylibdmtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ def _decoded_matrix_region(decoder, region, corrections):
dmtxMessageDestroy(byref(message))


def _decode_region(decoder, region, corrections, shrink):
def _decode_region(decoder, region, corrections, shrink, binary=False):
"""Decodes and returns the value in a region.
Args:
region (DmtxRegion):
binary (boolean): flag to set interepretation of decoded message as binary bytes rather
than as a zero terminated cstring.
Yields:
Decoded or None: The decoded value.
Expand All @@ -169,8 +171,13 @@ def _decode_region(decoder, region, corrections, shrink):
y0 = int((shrink * p00.Y) + 0.5)
x1 = int((shrink * p11.X) + 0.5)
y1 = int((shrink * p11.Y) + 0.5)
if binary:
decodedbuf = cast(msg.contents.output, ctypes.POINTER(ctypes.c_ubyte * msg.contents.outputIdx))[0]
payload = bytes(decodedbuf)
else:
payload = string_at(msg.contents.output)
return Decoded(
string_at(msg.contents.output),
payload,
Rect(x0, y0, x1 - x0, y1 - y0)
)
else:
Expand Down Expand Up @@ -225,7 +232,7 @@ def _pixel_data(image):

def decode(image, timeout=None, gap_size=None, shrink=1, shape=None,
deviation=None, threshold=None, min_edge=None, max_edge=None,
corrections=None, max_count=None):
corrections=None, max_count=None, binary=False):
"""Decodes datamatrix barcodes in `image`.
Args:
Expand All @@ -241,7 +248,8 @@ def decode(image, timeout=None, gap_size=None, shrink=1, shape=None,
corrections (int):
max_count (int): stop after reading this many barcodes. `None` to read
as many as possible.
binary (boolean): tells decoder to interpret decoded message as binary bytes rather
than as a zero terminated cstring
Returns:
:obj:`list` of :obj:`Decoded`: The values decoded from barcodes.
"""
Expand Down Expand Up @@ -285,7 +293,7 @@ def decode(image, timeout=None, gap_size=None, shrink=1, shape=None,
else:
# Decoded
res = _decode_region(
decoder, region, corrections, shrink
decoder, region, corrections, shrink, binary=binary
)
if res:
results.append(res)
Expand Down

0 comments on commit 8700a05

Please sign in to comment.