Skip to content

Commit ead24ca

Browse files
Merge pull request #92 from DiamondLightSource/odd_chars
Odd chars
2 parents 736b95f + 69cb7f7 commit ead24ca

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

dls_barcode/datamatrix/datamatrix.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import logging
2-
from dls_util.image.image import Image
3-
from .locate import Locator
2+
import string
3+
from string import ascii_lowercase
4+
45
from pylibdmtx.pylibdmtx import decode
5-
import cv2
66

7+
from dls_util.image.image import Image
8+
9+
from .locate import Locator
710

811
# We predict the location of the center of each square (pixel/bit) in the datamatrix based on the
912
# size and location of the finder pattern, but this can sometimes be slightly off. If the initial
@@ -25,6 +28,8 @@ class DataMatrix:
2528
"""
2629
DEFAULT_SIZE = 14
2730
DEFAULT_SIDE_SIZES = [12, 14]
31+
# allow only capitol letters, digits and dash in the decoded string
32+
ALLOWED_CHARS = set(string.ascii_uppercase + string.ascii_lowercase + string.digits + '-' + '_')
2833

2934
def __init__(self, finder_pattern):
3035
""" Initialize the DataMatrix object with its finder pattern location in an image. To actually
@@ -107,16 +112,17 @@ def _read(self, gray_image):
107112
given by the datamatrix finder pattern.
108113
"""
109114
try:
110-
111-
112115
result = decode(gray_image, max_count = 1)
113116
if len(result) > 0:
114117
d = result[0].data
115118
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 = ""
119+
if self._contains_allowed_chars_only(decoded):
120+
new_line_removed = decoded.replace("\n","")
121+
self._data = new_line_removed
122+
self._read_ok = True
123+
self._error_message = ""
124+
else:
125+
self._read_ok = False
120126
else:
121127
self._read_ok = False
122128
#cv2.imshow("Erode", gray_image)
@@ -133,6 +139,9 @@ def draw(self, img, color):
133139
img.draw_line(fp.c1, fp.c2, color)
134140
img.draw_line(fp.c1, fp.c3, color)
135141

142+
def _contains_allowed_chars_only(self, text):
143+
return (set(text)).issubset(self.ALLOWED_CHARS)
144+
136145
@staticmethod
137146
def locate_all_barcodes_in_image(grayscale_img, matrix_sizes=[DEFAULT_SIZE]):
138147
""" Searches the image for all datamatrix finder patterns

dls_barcode/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "v1.8.0"
1+
VERSION = "v1.9.0"

docs/release-notes/release-notes-dev.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Changes merged into master
55
--------------------------
66
| Jira Task | GitHub Issue | Type | Description |
77
|-----------|--------------|------|------------------------------------|
8-
| - | - | |Use python 3.7 |
9-
|I04_1-1036 | - |Minor |Use pylibdtmx to read barcodes |
8+
| - | - | | |
9+
| - | - | | |
1010

1111
Change Types:
1212
* Major - Backward incompatible change
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Release Notes (Development)
2+
===========================
3+
4+
Changes merged into master
5+
--------------------------
6+
| Jira Task | GitHub Issue | Type | Description |
7+
|-----------|--------------|------|---------------------------------------|
8+
|I04_1-1062 | - |Minor |Only letters, numbers, _ and - allowed|
9+
10+
Change Types:
11+
* Major - Backward incompatible change
12+
* Minor - Backward compatible change in API/functionality
13+
* Patch - Bug fix, no change in functionality
14+
15+
16+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import unittest
2+
3+
from mock import Mock, patch
4+
5+
from dls_barcode.datamatrix.datamatrix import DataMatrix
6+
7+
class TestDatamatrix(unittest.TestCase):
8+
9+
def test_contains_allowed_characers(self):
10+
datamatrix = DataMatrix(Mock)
11+
test_string_good = "AbcD123_-"
12+
self.assertTrue(datamatrix._contains_allowed_chars_only(test_string_good))
13+
14+
test_string_bad = "AbcD123_-$<"
15+
self.assertFalse(datamatrix._contains_allowed_chars_only(test_string_bad))
16+
17+
18+
19+

0 commit comments

Comments
 (0)