File size: 3,064 Bytes
6084325
86ef4a9
6084325
 
 
5d52af1
9f0ef40
6084325
 
 
 
 
 
 
 
 
 
 
 
784d954
6084325
 
784d954
86ef4a9
784d954
6084325
784d954
86ef4a9
24c48fb
86ef4a9
24c48fb
784d954
86ef4a9
 
24c48fb
 
8102829
86ef4a9
8102829
6084325
784d954
550ae51
 
 
 
86ef4a9
 
44c3663
550ae51
86ef4a9
 
550ae51
9e1cd24
784d954
6084325
784d954
6084325
86ef4a9
6084325
 
 
 
784d954
550ae51
 
86ef4a9
550ae51
 
 
 
 
 
6084325
550ae51
9f6b576
 
 
550ae51
 
86ef4a9
9310f69
 
 
 
 
 
550ae51
056d44f
08657ec
784d954
550ae51
0125c43
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
import qrcode
import cv2
from PIL import Image
import gradio as gr
import tempfile
import numpy as np

# Function to generate a QR code
def generate_qr(data):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    qr.add_data(data)
    qr.make(fit=True)
    img = qr.make_image(fill="black", back_color="white")

    # Save to a temporary file
    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
    img.save(temp_file.name, format="PNG")
    temp_file.close()  # Ensure file is written to disk
    return temp_file.name, img  # Return the file path and the PIL image


# Function to read a QR code
def read_qr(img):
    # Convert PIL image to a NumPy array
    img = np.array(img)

    # Convert RGB to BGR as OpenCV expects
    if img.ndim == 3:
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

    # Initialize OpenCV QR code detector
    detector = cv2.QRCodeDetector()
    data, _, _ = detector.detectAndDecode(img)
    return data if data else "No QR code found."


# Gradio Interface
def create_gradio_interface():
    # QR Code Generator with Display and Downloadable Link
    def generate_qr_interface(data):
        qr_file, qr_image = generate_qr(data)
        return qr_image, qr_file  # Show image and provide download link

    # QR Code Reader with Copy-to-Clipboard Button
    def read_qr_interface(img):
        decoded_data = read_qr(img)
        return decoded_data  # Return decoded text

    # QR Code Generator Tab
    generate_interface = gr.Interface(
        fn=generate_qr_interface,
        inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
        outputs=[gr.Image(label="Generated QR Code"), gr.File(label="Download QR Code")],
        title="Generate QR Code",
        description="Quickly create a QR code from any text or URL.",
    )

    # QR Code Reader Tab
    read_interface = gr.Interface(
        fn=read_qr_interface,
        inputs=gr.Image(type="pil", label="Upload QR Code Image"),
        outputs=gr.Textbox(label="Decoded Data"),
        title="Read QR Code",
        description="Upload an image with a QR code to decode the embedded data.",
    )

    # Clipboard Functionality for "Read QR Code" Tab
    with gr.Blocks() as demo:
        gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
        with gr.Tab("Generate QR Code"):
            generate_interface.render()
        with gr.Tab("Read QR Code"):
            qr_text = gr.Textbox(label="Decoded Data")  # Single box for decoded data
            copy_button = gr.Button("Copy to Clipboard")
            read_interface.render()  # Corrected placement
            copy_button.click(
                None,
                qr_text,
                qr_text,
                js="() => {navigator.clipboard.writeText(document.querySelector('input[type=text]').value); return 'Copied to Clipboard!'}"
            )

    demo.launch(share=True)


# Run the Gradio interface
create_gradio_interface()