Skip to content

image-20250412221258252

python
import streamlit as st

import qrcode
from io import BytesIO

st.subheader("生成二维码")

link = st.text_input('请输入链接')

qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )

if st.button("生成"):
    if not link:
        st.error("请提供一个有效的链接")
        st.stop()
    qr.add_data(link)
    qr.make(fit=True)
    img = qr.make_image(fill_color="black", back_color="white")
    buffered = BytesIO()
    img.save(buffered, format="PNG")
    img_bytes = buffered.getvalue()
    st.image(img_bytes, 
             caption=link, 
             width=300,
             use_container_width=False)