diff --git a/dls_barcode/datamatrix/datamatrix.py b/dls_barcode/datamatrix/datamatrix.py index 5c24764..9dd5e60 100644 --- a/dls_barcode/datamatrix/datamatrix.py +++ b/dls_barcode/datamatrix/datamatrix.py @@ -1,9 +1,12 @@ import logging -from dls_util.image.image import Image -from .locate import Locator +import string +from string import ascii_lowercase + from pylibdmtx.pylibdmtx import decode -import cv2 +from dls_util.image.image import Image + +from .locate import Locator # We predict the location of the center of each square (pixel/bit) in the datamatrix based on the # size and location of the finder pattern, but this can sometimes be slightly off. If the initial @@ -25,6 +28,8 @@ class DataMatrix: """ DEFAULT_SIZE = 14 DEFAULT_SIDE_SIZES = [12, 14] + # allow only capitol letters, digits and dash in the decoded string + ALLOWED_CHARS = set(string.ascii_uppercase + string.ascii_lowercase + string.digits + '-' + '_') def __init__(self, finder_pattern): """ Initialize the DataMatrix object with its finder pattern location in an image. To actually @@ -107,16 +112,17 @@ def _read(self, gray_image): given by the datamatrix finder pattern. """ try: - - result = decode(gray_image, max_count = 1) if len(result) > 0: d = result[0].data decoded = d.decode('UTF-8') - new_line_removed = decoded.replace("\n","") - self._data = new_line_removed - self._read_ok = True - self._error_message = "" + if self._contains_allowed_chars_only(decoded): + new_line_removed = decoded.replace("\n","") + self._data = new_line_removed + self._read_ok = True + self._error_message = "" + else: + self._read_ok = False else: self._read_ok = False #cv2.imshow("Erode", gray_image) @@ -133,6 +139,9 @@ def draw(self, img, color): img.draw_line(fp.c1, fp.c2, color) img.draw_line(fp.c1, fp.c3, color) + def _contains_allowed_chars_only(self, text): + return (set(text)).issubset(self.ALLOWED_CHARS) + @staticmethod def locate_all_barcodes_in_image(grayscale_img, matrix_sizes=[DEFAULT_SIZE]): """ Searches the image for all datamatrix finder patterns diff --git a/dls_barcode/version.py b/dls_barcode/version.py index 3d3e3ea..ad6fb3a 100644 --- a/dls_barcode/version.py +++ b/dls_barcode/version.py @@ -1 +1 @@ -VERSION = "v1.8.0" \ No newline at end of file +VERSION = "v1.9.0" \ No newline at end of file diff --git a/docs/release-notes/release-notes-dev.md b/docs/release-notes/release-notes-dev.md index 35cad06..af0594e 100644 --- a/docs/release-notes/release-notes-dev.md +++ b/docs/release-notes/release-notes-dev.md @@ -5,8 +5,8 @@ Changes merged into master -------------------------- | Jira Task | GitHub Issue | Type | Description | |-----------|--------------|------|------------------------------------| -| - | - | |Use python 3.7 | -|I04_1-1036 | - |Minor |Use pylibdtmx to read barcodes | +| - | - | | | +| - | - | | | Change Types: * Major - Backward incompatible change diff --git a/docs/release-notes/release-notes-v1_9_0.md b/docs/release-notes/release-notes-v1_9_0.md new file mode 100644 index 0000000..88742d5 --- /dev/null +++ b/docs/release-notes/release-notes-v1_9_0.md @@ -0,0 +1,16 @@ +Release Notes (Development) +=========================== + +Changes merged into master +-------------------------- +| Jira Task | GitHub Issue | Type | Description | +|-----------|--------------|------|---------------------------------------| +|I04_1-1062 | - |Minor |Only letters, numbers, _ and - allowed| + +Change Types: +* Major - Backward incompatible change +* Minor - Backward compatible change in API/functionality +* Patch - Bug fix, no change in functionality + + + diff --git a/tests/unit_tests/test_dls_barcode/test_datamatrix/test_datamatrix.py b/tests/unit_tests/test_dls_barcode/test_datamatrix/test_datamatrix.py new file mode 100644 index 0000000..6296bb9 --- /dev/null +++ b/tests/unit_tests/test_dls_barcode/test_datamatrix/test_datamatrix.py @@ -0,0 +1,19 @@ +import unittest + +from mock import Mock, patch + +from dls_barcode.datamatrix.datamatrix import DataMatrix + +class TestDatamatrix(unittest.TestCase): + + def test_contains_allowed_characers(self): + datamatrix = DataMatrix(Mock) + test_string_good = "AbcD123_-" + self.assertTrue(datamatrix._contains_allowed_chars_only(test_string_good)) + + test_string_bad = "AbcD123_-$<" + self.assertFalse(datamatrix._contains_allowed_chars_only(test_string_bad)) + + + +