-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarcode.py
executable file
·142 lines (110 loc) · 3.95 KB
/
barcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
# Copyright 2018 Simon Robin Lehn. All rights reserved.
# Use of this source code is governed by a MIT-style
# license that can be found in the LICENSE file.
################################################################################
class barcode:
def __init__(self, str="", barcode_type="", image_type=""):
self.str = str
self.barcode_type = barcode_type
self.image_type = image_type
self.barcode_linear = ''
self.sixel = ''
self.barcode_linear_width = 50
self.barcode_thick_line_width = 6
self.barcode_thin_line_width = 2
self.convert_string()
self.enlarge_barcode()
self.convert_barcode()
#print(self.barcode_linear)
print("\n " + self.sixel)
##############################################################################
def convert_string(self):
if self.barcode_type == "code39":
self.convert_string_to_code39()
return
##############################################################################
def convert_string_to_code39(self):
map = {
'1' : '110100101011',
'2' : '101100101011',
'3' : '110110010101',
'4' : '101001101011',
'5' : '110100110101',
'6' : '101100110101',
'7' : '101001011011',
'8' : '110100101101',
'9' : '101100101101',
'0' : '101001101101',
'A' : '110101001011',
'B' : '101101001011',
'C' : '110110100101',
'D' : '101011001011',
'E' : '110101100101',
'F' : '101101100101',
'G' : '101010011011',
'H' : '110101001101',
'I' : '101101001101',
'J' : '101011001101',
'K' : '110101010011',
'L' : '101101010011',
'M' : '110110101001',
'N' : '101011010011',
'O' : '110101101001',
'P' : '101101101001',
'Q' : '101010110011',
'R' : '110101011001',
'S' : '101101011001',
'T' : '101011011001',
'U' : '110010101011',
'V' : '100110101011',
'W' : '110011010101',
'X' : '100101101011',
'Y' : '110010110101',
'Z' : '100110110101',
'-' : '100101011011',
'.' : '110010101101',
' ' : '100110101101',
'*' : '100101101101',
}
str = '*' + self.str.upper().replace('*', '_') + '*'
barcode_temp = [map[char] + '0' if char in map else map['-'] + '0' for char in str]
self.barcode_linear = ''.join(barcode_temp)[:-1]
##############################################################################
def convert_barcode(self):
if self.image_type == "sixel":
self.convert_barcode_to_sixel()
return
##############################################################################
def enlarge_barcode(self):
self.barcode_linear = self.barcode_linear\
.replace('00', '2')\
.replace('11', '3')\
.replace('0', '0' * self.barcode_thin_line_width)\
.replace('1', '1' * self.barcode_thin_line_width)\
.replace('2', '0' * self.barcode_thick_line_width)\
.replace('3', '1' * self.barcode_thick_line_width)
##############################################################################
def convert_barcode_to_sixel(self):
if self.barcode_linear:
self.convert_barcode_to_sixel_linear()
return
##############################################################################
def convert_barcode_to_sixel_linear(self):
self.sixel = "\033Pq"
filler = 6 - len(self.barcode_linear) % 6
if filler == 6:
filler = 0
self.barcode_linear += '0' * filler
bc = self.barcode_linear
while len(bc) != 0:
sixel_char = chr(63 + int(bc[:6][::-1], 2))
bc = bc[6:]
self.sixel += '!' + str(self.barcode_linear_width) + sixel_char + '-'
self.sixel = self.sixel[:-1] + "\033\\"
################################################################################
def main():
barcode(str="tes((t", barcode_type="code39", image_type="sixel")
################################################################################
if __name__ == "__main__":
main()