Skip to content

Commit 54f74aa

Browse files
committed
limit the result
1 parent 736b95f commit 54f74aa

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

dls_barcode/datamatrix/datamatrix.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from dls_util.image.image import Image
33
from .locate import Locator
44
from pylibdmtx.pylibdmtx import decode
5-
import cv2
6-
5+
import string
76

87
# We predict the location of the center of each square (pixel/bit) in the datamatrix based on the
98
# size and location of the finder pattern, but this can sometimes be slightly off. If the initial
@@ -25,6 +24,10 @@ class DataMatrix:
2524
"""
2625
DEFAULT_SIZE = 14
2726
DEFAULT_SIDE_SIZES = [12, 14]
27+
# allow only capitol letters, digits and dash in the decoded string
28+
ALLOWED_CHARS = set(string.ascii_uppercase + string.digits + '-')
29+
# allow up to 10 chars in the decoded string
30+
ALLOWED_NUMBER_OF_CHARS = 11
2831

2932
def __init__(self, finder_pattern):
3033
""" Initialize the DataMatrix object with its finder pattern location in an image. To actually
@@ -107,16 +110,17 @@ def _read(self, gray_image):
107110
given by the datamatrix finder pattern.
108111
"""
109112
try:
110-
111-
112113
result = decode(gray_image, max_count = 1)
113114
if len(result) > 0:
114115
d = result[0].data
115116
decoded = d.decode('UTF-8')
116-
new_line_removed = decoded.replace("\n","")
117-
self._data = new_line_removed
118-
self._read_ok = True
119-
self._error_message = ""
117+
if self._contains_allowed_chars_only(decoded) and len(decoded) < self.ALLOWED_NUMBER_OF_CHARS:
118+
new_line_removed = decoded.replace("\n","")
119+
self._data = new_line_removed
120+
self._read_ok = True
121+
self._error_message = ""
122+
else:
123+
self._read_ok = False
120124
else:
121125
self._read_ok = False
122126
#cv2.imshow("Erode", gray_image)
@@ -133,6 +137,9 @@ def draw(self, img, color):
133137
img.draw_line(fp.c1, fp.c2, color)
134138
img.draw_line(fp.c1, fp.c3, color)
135139

140+
def _contains_allowed_chars_only(self, text):
141+
return (set(text)).issubset(self.ALLOWED_CHARS)
142+
136143
@staticmethod
137144
def locate_all_barcodes_in_image(grayscale_img, matrix_sizes=[DEFAULT_SIZE]):
138145
""" Searches the image for all datamatrix finder patterns

0 commit comments

Comments
 (0)