File size: 3,590 Bytes
8e5974c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import qrcode
from io import BytesIO
from PIL import Image
import requests

# Streamlit app
st.title("QR Code Generator")
st.subheader("Generate a QR Code for Text, URLs, or Uploaded Files")

# Choice: Text/URL or File Upload
option = st.radio("Choose an option:", ["Enter Text/URL", "Upload a File"])

if option == "Enter Text/URL":
    user_input = st.text_input("Enter the text or URL for which you want to generate a QR Code:")

    if st.button("Generate QR Code"):
        if user_input:
            # Generate QR code
            qr = qrcode.QRCode(
                version=1,
                error_correction=qrcode.constants.ERROR_CORRECT_L,
                box_size=10,
                border=4,
            )
            qr.add_data(user_input)
            qr.make(fit=True)

            # Create an image from the QR Code instance
            img = qr.make_image(fill_color="black", back_color="white")

            # Convert the image to BytesIO for display and download
            buffer = BytesIO()
            img.save(buffer, format="PNG")
            buffer.seek(0)

            # Display the QR code
            st.image(buffer, caption="Your QR Code", use_column_width=True)

            # Provide a download button
            st.download_button(
                label="Download QR Code",
                data=buffer,
                file_name="qrcode.png",
                mime="image/png"
            )
        else:
            st.error("Please enter some text or URL to generate the QR Code.")

elif option == "Upload a File":
    uploaded_file = st.file_uploader("Upload a file to generate a QR Code link:", type=None)

    if uploaded_file:
        if st.button("Upload and Generate QR Code"):
            # Upload file to File.io
            response = requests.post(
                "https://file.io/",
                files={"file": uploaded_file}
            )

            if response.status_code == 200:
                file_url = response.json().get("link", None)

                if file_url:
                    st.success("File uploaded successfully!")

                    # Generate QR code for the file link
                    qr = qrcode.QRCode(
                        version=1,
                        error_correction=qrcode.constants.ERROR_CORRECT_L,
                        box_size=10,
                        border=4,
                    )
                    qr.add_data(file_url)
                    qr.make(fit=True)

                    # Create an image from the QR Code instance
                    img = qr.make_image(fill_color="black", back_color="white")

                    # Convert the image to BytesIO for display and download
                    buffer = BytesIO()
                    img.save(buffer, format="PNG")
                    buffer.seek(0)

                    # Display the QR code
                    st.image(buffer, caption="Your QR Code", use_column_width=True)

                    # Provide a download button for the QR code
                    st.download_button(
                        label="Download QR Code",
                        data=buffer,
                        file_name="qrcode.png",
                        mime="image/png"
                    )

                    # Display the file link
                    st.markdown(f"[Click here to download your uploaded file]({file_url})")
                else:
                    st.error("Failed to retrieve file link from File.io.")
            else:
                st.error("File upload failed. Please try again.")