Files
track/backend/app/services/qrcode_service.py

35 lines
920 B
Python
Raw Normal View History

"""二维码生成服务 — 参考 MOM 系统 label_service.py 的 QR 生成逻辑"""
import io
import qrcode
from qrcode.image.pil import PilImage
def generate_qrcode_png(content: str, size_px: int = 300) -> io.BytesIO:
"""
生成二维码 PNG 图片返回 BytesIO
参数:
content: 二维码内容 16 位序列号
size_px: 输出图片尺寸像素默认 300×300
返回:
io.BytesIO: PNG 格式的图片字节流
"""
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=10,
border=2,
)
qr.add_data(content)
qr.make(fit=True)
img: PilImage = qr.make_image(fill_color="black", back_color="white")
img = img.convert("RGB")
img = img.resize((size_px, size_px))
buf = io.BytesIO()
img.save(buf, format="PNG")
buf.seek(0)
return buf