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 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 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)