def test_decode(self): arr = [[1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 0, 1], [1, 0, 0, 1, 0, 0], [1, 1, 0, 1, 0, 1], [1, 1, 0, 1, 1, 0], [1, 1, 1, 1, 1, 1]] expect = 683 self.assertTrue(DataMatrixCrypto.decode(arr) == expect) before = 62792 matrix = DataMatrixCrypto.encode(before) after = DataMatrixCrypto.decode(matrix) self.assertTrue(before == after)
def read_matrix(self, rect): """ @param rect is two dimention array with point of a rectangle surrounding matrix area on image @return true if matrix is read succesfully or false if not Function read matrix in selected area. """ wx = (rect[1][0] - rect[0][0]) / self.size wy = (rect[1][1] - rect[0][1]) / self.size vx = (rect[3][0] - rect[0][0]) / self.size vy = (rect[3][1] - rect[0][1]) / self.size first = [rect[0][0] + (wx + vx) / 2, rect[0][1] + (wy + vy) / 2] try: matrix = [[self.th[first[1] + i * wy + j * vy][first[0] + i * wx + j * vx] for i in range(self.size)] for j in range(self.size)] except IndexError: return None if self.check_matrix(matrix): print(True) self.detected = True print(matrix) self.id = DataMatrixCrypto.decode(self.turn_matrix(matrix)) print(matrix) print(self.id) return True else: self.detected = False return False
def test_encode(self): expect = [[1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 0, 1], [1, 1, 0, 1, 0, 0], [1, 0, 1, 1, 1, 1], [1, 0, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1]] arr = DataMatrixCrypto.encode(2678) self.assertTrue(arr == expect)
def create_data_matrix(image_size, mat_id, matrix_size, path='data_matrixes/'): """ @param image_size @param mat_id id of matrix to draw @param matrix_size - size square matrix @param path to save the image Function draw data matrix and save it. """ matrix = DataMatrixCrypto.encode(mat_id) square_size = image_size // matrix_size image = DataMatrixCreator.create_blank(image_size, image_size) cv2.rectangle(image, (0, 0), (image_size - 1, image_size - 1), (0, 0, 0), 1, 1) for i in range(matrix_size): for j in range(matrix_size): if matrix[-i-1][j] == 1: pt1 = (i * square_size, j * square_size) pt2 = ((i+1) * square_size, (j+1) * square_size) cv2.rectangle(image, pt1, pt2, (0, 0, 0), -1, 1) cv2.imwrite(path + str(mat_id) + '.jpg', image)