Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
44-liner: step by step solution in Clear category for Barcode Reader by przemyslaw.daniel
EAN13 = {
"LLLLLLRRRRRR": "0", "LLGLGGRRRRRR": "1",
"LLGGLGRRRRRR": "2", "LLGGGLRRRRRR": "3",
"LGLLGGRRRRRR": "4", "LGGLLGRRRRRR": "5",
"LGGGLLRRRRRR": "6", "LGLGLGRRRRRR": "7",
"LGLGGLRRRRRR": "8", "LGGLGLRRRRRR": "9"
}
DECODE_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")
}
def one_way_barcode_reader(barcode):
utmost_bar, center_bar = '_ _', ' _ _ '
if len(barcode) != 95 or barcode[45:50] != center_bar:
return
if barcode[:3] != utmost_bar or barcode[-3:] != utmost_bar:
return
barcode = barcode[3:45] + barcode[50:]
barcode = [barcode[x:x + 7] for x in range(0, 84, 7)]
data = [DECODE_TABLE.get(x, 'SYMBOL ERROR') for x in barcode]
if 'SYMBOL ERROR' in data:
return
digits, scheme = [''.join(x) for x in list(zip(*data))]
data = enumerate((EAN13[scheme]+digits)[:-1])
checksum = -sum([[1, 3][i % 2]*int(x) for i, x in data]) % 10
if checksum != int(digits[-1]):
return
return EAN13[scheme]+digits
def barcode_reader(barcode):
result = one_way_barcode_reader(barcode)
if result is not None:
return result
return one_way_barcode_reader(barcode[::-1])
Dec. 5, 2017