From c194cfe7a13021f105b068c4d07cb5484c098397 Mon Sep 17 00:00:00 2001 From: zhangbaoxiang Date: Wed, 10 Apr 2024 01:51:00 +0800 Subject: [PATCH] Wed Apr 10 01:51:00 CST 2024 inscode --- main.py | 112 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 96 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index fb9ab94..648cf3e 100644 --- a/main.py +++ b/main.py @@ -1,24 +1,104 @@ import streamlit as st -from streamlit_option_menu import option_menu +from PIL import ImageGrab, Image +import cv2 +import numpy as np +import qrcode +import io +import base64 -# 设置Streamlit应用程序的标题 -st.set_page_config(page_title="app name", layout="wide") +# Define app title and favicon +st.set_page_config(page_title="QR Code File Transfer", page_icon=":floppy_disk:") -menu1="菜单1" -menu2="菜单2" +# Define constants +SCREENSHOT_BTN = "Take Screenshot" +QR_CODE_BTN = "Generate QR Code" +SELECT_FILE_BTN = "Select File" +SEND_FILE_BTN = "Send File" +RECEIVE_FILE_BTN = "Receive File" +FILE_TYPES = ["png", "jpg", "txt"] -with st.sidebar: - menu = option_menu("菜单", [menu1, menu2], - icons=['house', "list-task"], - menu_icon="cast", default_index=0) -def main(): +class QRCodeFileTransfer: + def __init__(self): + self.screenshot = None + self.qr_code = None + self.file_path = None + self.file_type = None + self.file_data = None - if menu == menu1: - st.subheader(f"{menu1}") + def take_screenshot(self, x, y, w, h): + # Use OpenCV to capture screenshot of specific region + img = cv2.cvtColor( + np.array(ImageGrab.grab(bbox=(x, y, x + w, y + h))), cv2.COLOR_BGR2RGB) + self.screenshot = Image.fromarray(img) - if menu == menu2: - st.subheader(f"{menu2}") + def generate_qr_code(self): + # Convert file data to base64 string + file_data_str = base64.b64encode(self.file_data).decode("utf-8") + # Generate QR code with base64 string + self.qr_code = qrcode.make(file_data_str) -if __name__ == '__main__': - main() + def save_file(self): + # Save file to disk + with open(self.file_path, "wb") as f: + f.write(self.file_data) + + def receive_file(self, qr_code_data): + # Decode base64 string from QR code data + file_data_str = base64.b64decode(qr_code_data).decode("utf-8") + # Split base64 string to get file type and file data + self.file_type, file_data_str = file_data_str.split(",") + # Convert file data from string to bytes + self.file_data = base64.b64decode(file_data_str.encode("utf-8")) + # Prompt user to select file path to save file + uploaded_file = st.file_uploader("Select a file path to save file", + type=self.file_type) + if uploaded_file is not None: + self.file_path = uploaded_file.name + # Save file to disk + self.save_file() + st.write("File saved successfully!") + + def run(self): + # Display title and instructions + st.title("QR Code File Transfer") + st.write("This app allows you to transfer files using QR codes.") + st.write("To send a file, take a screenshot of the file and generate a QR code.") + st.write("To receive a file, scan the QR code and select the file path to save the file.") + + # Display screenshot button and capture screenshot + if st.button(SCREENSHOT_BTN): + st.write("Click and drag to select screenshot area.") + x, y, w, h = st.select_rectangle("Select Screenshot Area", source=self.screenshot) + self.take_screenshot(x, y, w, h) + st.image(self.screenshot, caption="Screenshot") + + # Display QR code button and generate QR code + if self.screenshot is not None and st.button(QR_CODE_BTN): + # Prompt user to select file to transfer + uploaded_file = st.file_uploader("Select file to transfer", type=FILE_TYPES) + if uploaded_file is not None: + self.file_path = uploaded_file.name + self.file_data = uploaded_file.read() + self.generate_qr_code() + # Display QR code + st.image(self.qr_code, caption="QR Code") + + # Display file upload button and receive file + if st.button(RECEIVE_FILE_BTN): + # Prompt user to upload QR code image file + qr_code_img = st.file_uploader("Select QR Code Image File", type=["png", "jpg"]) + if qr_code_img is not None: + # Decode QR code data from image + img_bytes = io.BytesIO(qr_code_img.read()) + qr_code = qrcode.QRCode() + qr_code.add_data(img_bytes.getvalue()) + qr_code.make(fit=True) + qr_code_data = qr_code.data.decode("utf-8") + # Receive file from QR code data + self.receive_file(qr_code_data) + + +# Initialize app and run it +qrcode_transfer = QRCodeFileTransfer() +qrcode_transfer.run() \ No newline at end of file -- GitLab