Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
*Barcode Scanning solution in Clear category for Barcode Reader by JimmyCarlos
def barcode_reader(barcode):
# Check markers.
if barcode[:3] != "_ _": return None
if barcode[-3:] != "_ _": return None
if barcode[45:50] != " _ _ ": return None
# Try the code forwards and backwards.
if find_barcode(barcode): return find_barcode(barcode)
barcode = barcode[::-1]
if find_barcode(barcode): return find_barcode(barcode)
return None
def find_barcode(barcode):
# Split barcode into individual parts, turning to binary.
left_strs = ["".join(["1" if c == "_" else "0" for c in barcode[i:i+7]]) for i in range(3,45,7)]
right_strs = ["".join(["1" if c == "_" else "0" for c in barcode[i:i+7]]) for i in range(50,92,7)]
# Define how EAN-13 encodes letters
L_encodings = ["0001101","0011001","0010011","0111101","0100011","0110001","0101111","0111011","0110111","0001011"]
R_encodings = ["".join(str(int(not(bool(int(c))))) for c in x) for x in L_encodings]
G_encodings = [x[::-1] for x in R_encodings]
# Decode each string found in the barcode, and build together the structure of the left digits.
code,leftStructure = "",""
for barcode_seg in left_strs:
if barcode_seg in L_encodings: code += str(L_encodings.index(barcode_seg)); leftStructure += "L"
elif barcode_seg in G_encodings: code += str(G_encodings.index(barcode_seg)); leftStructure += "G"
else: return None
for barcode_seg in right_strs:
if barcode_seg in R_encodings: code += str(R_encodings.index(barcode_seg))
else: return None
# Using the structure reconstructed in the last part, find and add on the first digit
leftStructures = ["LLLLLL","LLGLGG","LLGGLG","LLGGGL","LGLLGG","LGGLLG","LGGGLL","LGLGLG","LGLGGL","LGGLGL"]
code = str(leftStructures.index(leftStructure)) + code
# Check the checksum
if sum([3*int(c) if i%2 else int(c) for i,c in enumerate(str(code))]) % 10: return None
return code
Nov. 3, 2019