Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Barcode Reader by Sim0000
table = {
' __ _' : ('0', 'L'), ' _ ___' : ('0', 'G'), '___ _ ' : ('0', 'R'),
' __ _' : ('1', 'L'), ' __ __' : ('1', 'G'), '__ __ ' : ('1', 'R'),
' _ __' : ('2', 'L'), ' __ __' : ('2', 'G'), '__ __ ' : ('2', 'R'),
' ____ _' : ('3', 'L'), ' _ _' : ('3', 'G'), '_ _ ' : ('3', 'R'),
' _ __' : ('4', 'L'), ' ___ _' : ('4', 'G'), '_ ___ ' : ('4', 'R'),
' __ _' : ('5', 'L'), ' ___ _' : ('5', 'G'), '_ ___ ' : ('5', 'R'),
' _ ____' : ('6', 'L'), ' _ _' : ('6', 'G'), '_ _ ' : ('6', 'R'),
' ___ __' : ('7', 'L'), ' _ _' : ('7', 'G'), '_ _ ' : ('7', 'R'),
' __ ___' : ('8', 'L'), ' _ _' : ('8', 'G'), '_ _ ' : ('8', 'R'),
' _ __' : ('9', 'L'), ' _ ___' : ('9', 'G'), '___ _ ' : ('9', 'R'),
}
first_digit = {
'LLLLLL' : '0',
'LLGLGG' : '1',
'LLGGLG' : '2',
'LLGGGL' : '3',
'LGLLGG' : '4',
'LGGLLG' : '5',
'LGGGLL' : '6',
'LGLGLG' : '7',
'LGLGGL' : '8',
'LGGLGL' : '9',
}
def barcode_reader(barcode):
def decode(barcode):
result = parity = ''
# left part
for i in range(3, 45, 7):
bars = barcode[i:i+7]
if bars not in table: return
d, p = table[bars]
if p not in 'LG': return
result += d
parity += p
# right part
for i in range(50, 92, 7):
bars = barcode[i:i+7]
if bars not in table: return
d, p = table[bars]
if p != 'R': return
result += d
# get first digit
if parity not in first_digit: return
return first_digit[parity] + result
# check guard bar
if barcode[:3] != '_ _': return # left
if barcode[92:] != '_ _': return # right
if barcode[45:50] != ' _ _ ': return # center
# decode digit character
result = decode(barcode)
if not result:
result = decode(barcode[::-1]) # try reverse order
if not result: return
# verify check digit
checksum = (10 - sum(int(result[i]) * (2 * (i % 2) + 1) for i in range(12)) % 10) % 10
if str(checksum) != result[12]: return
return result
"""
pos len : explanation
0-2 (3) : left guard bar (= 101)
3-44 (42) : 6 characters. each character use 7 bars
45-49 (5) : center bar (= 01010)
50-84 (35) : 5 characters. each character use 7 bars
85-91 (7) : 1 character. check digit
92-94 (3) : right guard bar (= 101)
"""
if __name__ == '__main__':
assert barcode_reader(
'_ _ _ __ _ ___ __ __ _ __ ____ _ ___ _ _ _ __ __ __ __ _ _ _ ___ _ ___ _ _ _ _'
) == '5901234123457', '5901234123457'
assert barcode_reader(
'_ _ _ __ _ ___ _ __ _ ____ _ _ _ _ _ _ _ _ __ __ _ _ _ _ _ _ _ ___ _ _'
) == '4299687613665', '4299687613665'
assert barcode_reader(
'_ _ ___ __ __ _ _ __ ____ _ _ __ __ _ _ _ _ _ _ _ _ _ ___ _ __ __ __ __ _ _'
) is None, '0712345678912 : wrong check digit (right: 1)'
assert barcode_reader(
'___ _ __ _ ___ _ __ _ ____ _ _ _ _ _ _ _ _ __ __ _ _ _ _ _ _ _ ___ _ _'
) is None, 'wrong left guard bar'
assert barcode_reader(
'_ _ _ __ _ ___ _ __ _ ____ _ _ _ _ _ ___ _ __ __ _ _ _ _ _ _ _ ___ _ _'
) is None, 'wrong center bar'
assert barcode_reader(
'_ _ _ __ _ ___ _ __ _ ____ _ _ _ _ _ _ _ _ __ __ _ _ _ _ _ _ _ ___ ___'
) == None, 'wrong right guard bar'
print("Check done.")
Nov. 25, 2017