Skip to content

Commit 54f7307

Browse files
committed
Init the project
0 parents  commit 54f7307

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+662
-0
lines changed

.idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/QRDecoder.iml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dictionaries/Kavinda.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/other.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Decoder/DecodeVersion01.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from Decoder.DecoderVersion import DecoderVersion
2+
3+
__author__ = 'N.A. Supun Kavinda'
4+
5+
class DecodeVersion01(DecoderVersion):
6+
7+
def __init__(self, unmask_matrix):
8+
super().__init__(unmask_matrix)
9+
10+
def is_skip_point(self, i, j):
11+
if (9 > i and 9 > j) or (9 > i and j > 12) or (i > 12 and j < 9):
12+
return True
13+
elif i == 6 or j == 6:
14+
return True
15+
else:
16+
return False

Decoder/DecodeVersion02.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from Decoder.DecoderVersion import DecoderVersion
2+
3+
__author__ = 'N.A. Supun Kavinda'
4+
5+
6+
class DecodeVersion02(DecoderVersion):
7+
8+
def __init__(self, unmask_matrix):
9+
self.__unmask_matrix = unmask_matrix
10+
super().__init__(unmask_matrix)
11+
12+
def is_skip_point(self, i, j):
13+
if (9 > i and 9 > j) or (9 > i and j > len(self.__unmask_matrix) - 9) or (
14+
i > (len(self.__unmask_matrix) - 9) and j < 9) or (
15+
(len(self.__unmask_matrix) - 4) > i > 15 and (len(self.__unmask_matrix[0]) - 4) > j > 15):
16+
return True
17+
elif i == 6 or j == 6:
18+
return True
19+
else:
20+
return False

Decoder/Decoder.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Decoder:
2+
def __init__(self):
3+
pass
4+
5+
@staticmethod
6+
def decode_bytes_array(binary_array):
7+
"""
8+
This method give the included word in QRCode.
9+
:rtype: str
10+
"""
11+
length = 8
12+
__binary_length_of_word = ''
13+
for i in range(4, 4 + length):
14+
__binary_length_of_word = __binary_length_of_word + binary_array[i]
15+
16+
i = 1
17+
word = ''
18+
length_of_word = int(__binary_length_of_word, 2)
19+
print("length of word ", length_of_word)
20+
while i <= length_of_word:
21+
character = ''
22+
for j in range(4 + length * i, 4 + length * (i + 1)):
23+
character = character + binary_array[j]
24+
char_int_value = int(character, 2)
25+
word += str(char_int_value) if 0 <= char_int_value < 10 else chr(char_int_value)
26+
i = i + 1
27+
return word

Decoder/DecoderFactory.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from Decoder.DecodeVersion02 import DecodeVersion02
2+
from Decoder.DecodeVersion01 import DecodeVersion01
3+
from ProcessMatrix.QRCodeDetails import QRCodeDetails
4+
5+
__author__ = 'N.A. Supun Kavinda'
6+
7+
8+
class DecoderFactory:
9+
def __init__(self, unmask_matrix):
10+
self.__unmask_matrix = unmask_matrix
11+
12+
def create(self):
13+
__version = QRCodeDetails(self.__unmask_matrix).get_version()
14+
print("version ", __version)
15+
16+
if __version == 1:
17+
return DecodeVersion01(self.__unmask_matrix)
18+
elif __version == 2:
19+
return DecodeVersion02(self.__unmask_matrix)
20+
# elif __version == 3:
21+
# return DecodeVersion03(self.__unmask_matrix)
22+
# elif __version == 4:
23+
# return DecodeVersion04(self.__unmask_matrix)
24+
# elif __version == 5:
25+
# return DecodeVersion05(self.__unmask_matrix)
26+
# elif __version == 6:
27+
# return DecodeVersion06(self.__unmask_matrix)
28+
# elif __version == 7:
29+
# return DecodeVersion07(self.__unmask_matrix)
30+
# elif __version == 8:
31+
# return DecodeVersion08(self.__unmask_matrix)
32+
else:
33+
return NotImplementedError('This Pattern is not implement yet.')

Decoder/DecoderVersion.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from abc import ABC, abstractmethod
2+
3+
from Decoder.Decoder import Decoder
4+
5+
__author__ = 'N.A. Supun Kavinda'
6+
7+
8+
class DecoderVersion(ABC):
9+
def __init__(self, unmask_matrix):
10+
self.__unmask_matrix = unmask_matrix
11+
12+
def decoder(self):
13+
__encode_mode = self.__get_encode_mode()
14+
__binary_array = self.__binary_data_array()
15+
if __encode_mode == '0100':
16+
return Decoder.decode_bytes_array(__binary_array)
17+
else:
18+
return NotImplementedError(__encode_mode, ' This encode mode is not implemented')
19+
20+
def __get_encode_mode(self):
21+
rows = len(self.__unmask_matrix)
22+
cols = len(self.__unmask_matrix[0])
23+
return str(self.__unmask_matrix[rows - 1][cols - 1]) + str(self.__unmask_matrix[rows - 1][cols - 2]) + str(
24+
self.__unmask_matrix[rows - 2][cols - 1]) + str(self.__unmask_matrix[rows - 2][cols - 2])
25+
26+
def __binary_data_array(self):
27+
"""
28+
This method return total binary values included in word.
29+
:return: array
30+
"""
31+
self.data_array = []
32+
__is_down_to_up = True
33+
j = len(self.__unmask_matrix[0]) - 1 # cols
34+
while j > 0:
35+
if __is_down_to_up:
36+
for i in range(len(self.__unmask_matrix) - 1, -1, -1): # rows
37+
self.__fill_array(i, j)
38+
__is_down_to_up = False
39+
else:
40+
for i in range(len(self.__unmask_matrix)):
41+
self.__fill_array(i, j)
42+
__is_down_to_up = True
43+
if j is 8:
44+
j = j - 3
45+
else:
46+
j = j - 2
47+
return self.data_array
48+
49+
def __fill_array(self, i, j):
50+
"""
51+
Add binary value to the array. If binary value is part of QRCode included word.
52+
:param i:
53+
:param j:
54+
"""
55+
if not self.is_skip_point(i, j):
56+
self.data_array.append(str(self.__unmask_matrix[i][j]))
57+
if not self.is_skip_point(i, j - 1):
58+
self.data_array.append(str(self.__unmask_matrix[i][j - 1]))
59+
60+
@abstractmethod
61+
def is_skip_point(self, i, j):
62+
"""
63+
This method return the true when that specific point should skip. otherwise return false
64+
:type i: int
65+
:type j: int
66+
"""
67+
pass

Decoder/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from Decoder import DecoderFactory
2+
from Decoder import DecoderVersion

Mask/AddMask.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from abc import ABC, abstractmethod
2+
3+
__author__ = 'N.A. Supun Kavinda'
4+
5+
6+
class AddMask(ABC):
7+
8+
def __init__(self, matrix):
9+
self.__matrix = matrix
10+
11+
def add_mask(self):
12+
for i in range(len(self.__matrix)):
13+
for j in range(len(self.__matrix[0])):
14+
if not ((i < 9 and (j < 9 or j > len(self.__matrix[0]) - 9)) or (i > len(self.__matrix) - 9 and j < 9)):
15+
self.change_pixel(i, j, self.__matrix)
16+
return self.__matrix
17+
18+
@abstractmethod
19+
def change_pixel(self, i, j, matrix):
20+
pass

Mask/Pattern01.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from Mask.AddMask import AddMask
2+
3+
__author__ = 'N.A. Supun Kavinda'
4+
5+
6+
class Pattern01(AddMask):
7+
8+
def __init__(self, matrix):
9+
super().__init__(matrix)
10+
11+
def change_pixel(self, i, j, matrix):
12+
if j % 3 == 0:
13+
if matrix[i][j] == 1:
14+
matrix[i][j] = 0
15+
else:
16+
matrix[i][j] = 1

Mask/Pattern02.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from Mask.AddMask import AddMask
2+
3+
__author__ = 'N.A. Supun Kavinda'
4+
5+
6+
class Pattern02(AddMask):
7+
8+
def __init__(self, matrix):
9+
super().__init__(matrix)
10+
11+
def change_pixel(self, i, j, matrix):
12+
if (i + j) % 3 == 0:
13+
if matrix[i][j] == 1:
14+
matrix[i][j] = 0
15+
else:
16+
matrix[i][j] = 1

Mask/Pattern03.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from Mask.AddMask import AddMask
2+
3+
__author__ = 'N.A. Supun Kavinda'
4+
5+
6+
class Pattern03(AddMask):
7+
8+
def __init__(self, matrix):
9+
super().__init__(matrix)
10+
11+
def change_pixel(self, i, j, matrix):
12+
if (i + j) % 2 == 0:
13+
if matrix[i][j] == 1:
14+
matrix[i][j] = 0
15+
else:
16+
matrix[i][j] = 1

Mask/Pattern04.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from Mask.AddMask import AddMask
2+
3+
__author__ = 'N.A. Supun Kavinda'
4+
5+
6+
class Pattern04(AddMask):
7+
8+
def __init__(self, matrix):
9+
super().__init__(matrix)
10+
11+
def change_pixel(self, i, j, matrix):
12+
if i % 2 == 0:
13+
if matrix[i][j] == 1:
14+
matrix[i][j] = 0
15+
else:
16+
matrix[i][j] = 1

Mask/Pattern05.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from Mask.AddMask import AddMask
2+
3+
__author__ = 'N.A. Supun Kavinda'
4+
5+
6+
class Pattern05(AddMask):
7+
8+
def __init__(self, matrix):
9+
super().__init__(matrix)
10+
11+
def change_pixel(self, i, j, matrix):
12+
if ((i * j) % 3 + (i * j)) % 2 == 0:
13+
if matrix[i][j] == 1:
14+
matrix[i][j] = 0
15+
else:
16+
matrix[i][j] = 1

Mask/Pattern06.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from Mask.AddMask import AddMask
2+
3+
__author__ = 'N.A. Supun Kavinda'
4+
5+
6+
class Pattern06(AddMask):
7+
8+
def __init__(self, matrix):
9+
super().__init__(matrix)
10+
11+
def change_pixel(self, i, j, matrix):
12+
if ((i * j) % 3 + i + j) % 2 == 0:
13+
if matrix[i][j] == 1:
14+
matrix[i][j] = 0
15+
else:
16+
matrix[i][j] = 1

Mask/Pattern07.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from Mask.AddMask import AddMask
2+
3+
__author__ = 'N.A. Supun Kavinda'
4+
5+
6+
class Pattern07(AddMask):
7+
8+
def __init__(self, matrix):
9+
super().__init__(matrix)
10+
11+
def change_pixel(self, i, j, matrix):
12+
if (j / 2 + i / 3) % 2 == 0:
13+
if matrix[i][j] == 1:
14+
matrix[i][j] = 0
15+
else:
16+
matrix[i][j] = 1

0 commit comments

Comments
 (0)