35 lines
920 B
Python
35 lines
920 B
Python
"""二维码生成服务 — 参考 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
|