python: generate and decode QrCode

发布时间 2023-07-08 22:44:45作者: ®Geovin Du Dream Park™

 

# encoding: utf-8
#-*- coding: UTF-8 -*-
# 版权所有 2023 ©涂聚文有限公司
# 许可信息查看:
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 311
# Datetime  : 2023/7/5 11:08
# User      : geovindu
# Product   : UI
# Project   : pythonTkinterDemo
# File      : main.py
# explain   : 学习

import  pyzbar.pyzbar as p
import pyqrcode
import qrcode


def generateQrCode(qrcodestr:str,qrcodefile:str):
    """
    生成二维码
    :param qrcodestr:  二要生成的二给码字符
    :param qrcodefile: 生成的文件名
    :return:
    """
    img = qrcode.make(qrcodestr)
    type(img)
    img.save(f'{qrcodefile}.png')

def readQrCode(qrcodefile:str):
    """
    读二维码
    :param qrcodefile: 二维码文件名
    :return: 返回二维码内容
    """
    nowpath = os.getcwd()
    scrimg=Image.open(f"{nowpath}\{qrcodefile}.png")
    m=p.decode(scrimg)
    codedata=m[0].data.decode("utf-8")
    return codedata

  

调用:

    nowpath = os.getcwd() #当前项目文件夹
    generateQrCode("http://www.dusystem.com","geovindu")
    codedata=readQrCode("geovindu")
    print("qrcode:",codedata)