概述
# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author : v_jiaohaicheng@baidu.com
@des :
"""
import pyzbar.pyzbar
from PIL import Image, ImageEnhance
import cv2
import matplotlib.pyplot as plt
import numpy as np
from threading import Thread
from datetime import datetime
import qrcode
import hashlib
from queue import Queue
from PIL import ImageFont, ImageDraw, Image
MAP = {}
class ScanCode():
def __init__(self):
self.shop_list = []
# self.set_log = Queue()
self.scan()
def make_md5(self,item: str, salt: bytes = b""):
"""
计算输入文本的 md5
:param item:输入的文本
:return: 输入的文本对应的md5
"""
# item = item.rstrip("n")
md5_machine = hashlib.md5(salt)
md5_machine.update(item.encode('utf-8'))
return md5_machine.hexdigest()
def decode(self,input):
barcodes = pyzbar.decode(input)
if barcodes == []:
pass
else: # 识别出二维码
for barcode in barcodes:
barcodeData = barcode.data.decode("utf-8")
# print(barcodeData)
name,sell,weight,date1,date2 = barcodeData.split(" ")
sell = float(sell.replace("元/公斤",""))
weight = float(weight.replace("公斤",""))
date_shelves = "{} {}".format(date1,date2)
price = float(sell*weight)
info = {
"name":name,
"sell":sell,
"weight":weight,
"date_shelves":date_shelves,
"date_sold":str(datetime.now()).split(".")[0],
"price":price
}
secret = "{}{}{}{}{}".format(name,sell,weight,date_shelves,price)
md5_info = self.make_md5(secret)
if MAP.get(md5_info) is None :
print("商品信息: ",name, sell, weight, "{} {}".format(date1, date2))
self.shop_list.append(info)
MAP[md5_info] = info
def calculate_result(self):
all_price = 0
for i in self.shop_list:
all_price += i["price"]
return round(all_price,2)
def scan(self):
camera = cv2.VideoCapture(0)
while camera.isOpened():
# 读取当前帧
ret, frame = camera.read()
cv2.imshow("video", frame)
# 转为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
Thread(target=self.decode,args=(gray,)).start()
key = cv2.waitKey(1) # 每一帧图像显示时间,相当于刷新率
if key == 27:
all_price = self.calculate_result()
print("本次共计消费:{}元".format(all_price))
self.shop_list.clear()
MAP.clear()
# break
camera.release()
cv2.destroyAllWindows()
class MakeCode():
"""
生成商品信息二维码
"""
def __init__(self,name,sell,weight):
self.name = name
self.sell = sell
self.weight = weight
self.make_code()
def make_code(self):
contain = "{} {}元/公斤 {}公斤 {}".format(self.name,self.sell,self.weight,str(datetime.now()).split(".")[0])
qr = qrcode.QRCode(version=2,
error_correction=qrcode.constants.ERROR_CORRECT_H,
)
qr.add_data(contain)
qr.make(fit=True)
img = qr.make_image()
img.save(f'{name}-{sell}(二维码).png')
img = Image.open(f'{name}-{sell}(二维码).png')
draw = ImageDraw.Draw(img)
ttfront = ImageFont.truetype(R"D:ProjectsSupportRequirementsProjectimagemarkSourceHanSerifSC-Bold.otf", 14)
content = "{}t{}元/公斤t{}公斤".format(self.name,self.sell,self.weight)
draw.text((140, 460), content,font=ttfront)
img.save(f'{name}-{sell}(二维码).png')
if __name__ == '__main__':
ScanCode()
# name = "新鲜豆角2"
# sell = 8.88
# weight = 1.99
# MakeCode(name,sell,weight)
最后
以上就是文静刺猬为你收集整理的python 商品信息二维码生成,扫码计算单价的全部内容,希望文章能够帮你解决python 商品信息二维码生成,扫码计算单价所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复