Spaces:
Sleeping
Sleeping
File size: 5,877 Bytes
d1460a1 57d50d1 d1460a1 57d50d1 d1460a1 57d50d1 d1460a1 57d50d1 d1460a1 57d50d1 d1460a1 57d50d1 d1460a1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
import qrcode
from qrcode.constants import ERROR_CORRECT_L
import streamlit as st
import io
from PIL import Image
from urllib.parse import urlencode
def generate_contact_qr(contact_info):
"""
Generates a QR code for contact information in the vCard format.
Parameters:
contact_info (dict): A dictionary containing the contact information fields.
Returns:
PIL.Image.Image: The generated QR code image.
"""
vcard = "BEGIN:VCARD\n"
vcard += f"VERSION:4.0\n"
vcard += f"N:{contact_info['last_name']};{contact_info['first_name']}\n"
vcard += f"EMAIL:{contact_info['email']}\n"
vcard += f"TEL:{contact_info['phone']}\n"
vcard += f"ORG:{contact_info['organization']}\n"
vcard += f"TITLE:{contact_info['job_title']}\n"
vcard += "END:VCARD"
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(vcard)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def generate_email_qr(email_info):
"""
Generates a QR code for an email.
Parameters:
email_info (dict): A dictionary containing the email information fields.
Returns:
PIL.Image.Image: The generated QR code image.
"""
email_url = f"mailto:{email_info['to']}?subject={email_info['subject']}&body={email_info['body']}"
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(email_url)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def generate_sms_qr(sms_info):
"""
Generates a QR code for an SMS message.
Parameters:
sms_info (dict): A dictionary containing the SMS information fields.
Returns:
PIL.Image.Image: The generated QR code image.
"""
sms_url = f"sms:{sms_info['to']}?body={sms_info['body']}"
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(sms_url)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def generate_location_qr(latitude, longitude):
"""
Generates a QR code for a geographic location.
Parameters:
latitude (float): The latitude of the location.
longitude (float): The longitude of the location.
Returns:
PIL.Image.Image: The generated QR code image.
"""
location_url = f"https://www.google.com/maps?q={latitude},{longitude}"
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(location_url)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def generate_link_qr(url):
"""
Generates a QR code for a URL.
Parameters:
url (str): The URL to be encoded.
Returns:
PIL.Image.Image: The generated QR code image.
"""
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(url)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def main():
st.set_page_config(page_title="Advanced QR Code Generator")
st.title("Advanced QR Code Generator")
tab = st.sidebar.selectbox("Select QR Code Type", ["Contact", "Email", "SMS", "Location", "Link"])
if tab == "Contact":
st.subheader("Contact QR Code")
contact_info = {
"first_name": st.text_input("First Name"),
"last_name": st.text_input("Last Name"),
"email": st.text_input("Email"),
"phone": st.text_input("Phone"),
"organization": st.text_input("Organization"),
"job_title": st.text_input("Job Title"),
}
if st.button("Generate QR Code"):
qr_code = generate_contact_qr(contact_info)
buf = io.BytesIO()
qr_code.save(buf, format="PNG")
st.image(buf.getvalue(), use_container_width=True)
elif tab == "Email":
st.subheader("Email QR Code")
email_info = {
"to": st.text_input("To"),
"subject": st.text_input("Subject"),
"body": st.text_area("Body"),
}
if st.button("Generate QR Code"):
qr_code = generate_email_qr(email_info)
buf = io.BytesIO()
qr_code.save(buf, format="PNG")
st.image(buf.getvalue(), use_container_width=True)
elif tab == "SMS":
st.subheader("SMS QR Code")
sms_info = {
"to": st.text_input("To"),
"body": st.text_area("Message"),
}
if st.button("Generate QR Code"):
qr_code = generate_sms_qr(sms_info)
buf = io.BytesIO()
qr_code.save(buf, format="PNG")
st.image(buf.getvalue(), use_container_width=True)
elif tab == "Location":
st.subheader("Location QR Code")
latitude = st.number_input("Latitude", min_value=-90.0, max_value=90.0, step=0.000001)
longitude = st.number_input("Longitude", min_value=-180.0, max_value=180.0, step=0.000001)
if st.button("Generate QR Code"):
qr_code = generate_location_qr(latitude, longitude)
buf = io.BytesIO()
qr_code.save(buf, format="PNG")
st.image(buf.getvalue(), use_container_width=True)
elif tab == "Link":
st.subheader("Link QR Code")
url = st.text_input("URL")
if st.button("Generate QR Code"):
qr_code = generate_link_qr(url)
buf = io.BytesIO()
qr_code.save(buf, format="PNG")
st.image(buf.getvalue(), use_container_width=True)
if __name__ == "__main__":
main() |