# -*- coding: utf-8 -*-
import os
import numpy as np
import cv2
import re
from PIL import Image, ImageDraw, ImageFont
WIDTHS = {
"0": 212222, "1": 222122, "2": 222221,
"3": 121223, "4": 121322, "5": 131222,
"6": 122213, "7": 122312, "8": 132212,
"9": 221213, "10": 221312, "11": 113222, "12": 112232,
"13": 122132, "14": 122231, "15": 113222,
"16": 123122, "17": 123221, "18": 223211,
"19": 221132, "20": 221231, "21": 213212,
"22": 223112, "23": 312131, "24": 311222,
"25": 321122, "26": 321221, "27": 312212,
"28": 322112, "29": 322211, "30": 212123,
"31": 212321, "32": 232121, "33": 111323,
"34": 131123, "35": 131321, "36": 112313,
"37": 132113, "38": 132311, "39": 211313,
"40": 231113, "41": 231311, "42": 112133,
"43": 112331, "44": 132131, "45": 113123,
"46": 113321, "47": 133121, "48": 313121,
"49": 211331, "50": 231131, "51": 213113,
"52": 213311, "53": 213131, "54": 311123,
"55": 311321, "56": 331121, "57": 312113,
"58": 312311, "59": 332111, "60": 314111,
"61": 221411, "62": 431111, "63": 111224,
"64": 111422, "65": 121124, "66": 121421,
"67": 141122, "68": 141221, "69": 112214,
"70": 112412, "71": 122114, "72": 122411,
"73": 142112, "74": 142211, "75": 241211,
"76": 221114, "77": 413111, "78": 241112,
"79": 134111, "80": 111242, "81": 121142,
"82": 121241, "83": 114212, "84": 124112,
"85": 124211, "86": 411212, "87": 421112,
"88": 421211, "89": 212141, "90": 214121,
"91": 412121, "92": 111143, "93": 111341,
"94": 131141, "95": 114113, "96": 114311,
"97": 411113, "98": 411311, "99": 113141,
"100": 114131, "101": 311141, "102": 411131,
"103": 211412, "104": 211214, "105": 211232,
"106": 2331112,
}
START_CODE = "105"
STOP_CODE = "106"
CODE_B = "100"
CODE_A = "101"
FNC1 = "102"
def write_barcode_to_image(barcode, height=100, thickness=3, quiet_zone=True, add_text=""):
barcode_widths = []
for weight in barcode:
barcode_widths.append(int(weight) * thickness)
width = sum(barcode_widths)
x = 0
padding_y = 10
if quiet_zone:
width +=20 * thickness
x = 10 * thickness
# Monochrome Image
img = np.zeros((height, width, 3)).astype(np.uint8)
img[:, :, :] = 255
# img =Image.new('RGB', (width, height), 'white')
# draw = ImageDraw.Draw(img)
draw_bar = True
for bar_width in barcode_widths:
if draw_bar:
print(x, padding_y,x + bar_width - 1, height - padding_y)
cv2.rectangle(img, (x, padding_y), (x + bar_width - 1, height - padding_y), (0, 0, 0), thickness=-1)
point_size = 1
point_color = (0, 0, 255) # BGR
thickness = 0 # It can be for 0 、4、8
cv2.circle(img, (x, padding_y), point_size, point_color, thickness)
# img_o.rectangle(((x, padding_y), (x + bar_width - 1, height - padding_y)), fill=0)
draw_bar = not draw_bar
x += bar_width
return img
def generate_raw_bar_widths(build_barcode):
"""
Generates a barcode from raw codes, allows for FNC1 use.
:param build_barcode: array of codes excluding start, check-digit and stop.
:return:
"""
build_barcode.insert(0, START_CODE)
build_barcode.append(build_check_digit(build_barcode))
build_barcode.append(STOP_CODE)
return "".join(str(WIDTHS[str(i)]) for i in build_barcode)
def build_check_digit(build_barcode):
checksum = 0
for i, item in enumerate(build_barcode):
checksum += max(i, 1) * int(item)
return str(checksum % 103)
def generate_codes_from_str(raw_string):
build_barcode = [START_CODE] # START
# CONTENT
for pair in re.findall(r'..', raw_string):
build_barcode.append(str(int(pair)))
build_barcode.append(build_check_digit(build_barcode)) # CHECKSUM
build_barcode.append(STOP_CODE) # STOP
return build_barcode
def generate_bar_widths(raw_string):
build_barcode = generate_codes_from_str(raw_string)
return "".join(str(WIDTHS[i]) for i in build_barcode)
if __name__ == '__main__':
from pyzbar import pyzbar
img_o = np.zeros((402, 482, 3)).astype(np.uint8)
img_o[:, :, :] = 255
height=100
a = np.random.randint(100000, 999999)
b = np.random.randint(100000, 999999)
c = np.random.randint(100000, 999999)
bar_str = str(a) + str(b) + str(c)
print(bar_str)
bar_widths_str = generate_bar_widths(bar_str)
print(bar_widths_str)
image = write_barcode_to_image(bar_widths_str,height=height,add_text=None)
img_o[30:(30 +height), 10:(image.shape[1] + 10)] = image
img_o = cv2.putText(img_o, bar_str, (100, 170), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 2)
cv2.imshow("img",img_o)
result= pyzbar.decode(img_o)
print(result)
cv2.waitKey()
cv2.imwrite("asdfasdf.jpg",image)