Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
io_py
app
提交
c194cfe7
A
app
项目概览
io_py
/
app
与 Fork 源项目一致
Fork自
inscode / Streamlit
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c194cfe7
编写于
4月 10, 2024
作者:
Z
zhangbaoxiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Wed Apr 10 01:51:00 CST 2024 inscode
上级
0520122c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
96 addition
and
16 deletion
+96
-16
main.py
main.py
+96
-16
未找到文件。
main.py
浏览文件 @
c194cfe7
import
streamlit
as
st
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应用程序的标题
#
Define app title and favicon
st
.
set_page_config
(
page_title
=
"
app name"
,
layout
=
"wide
"
)
st
.
set_page_config
(
page_title
=
"
QR Code File Transfer"
,
page_icon
=
":floppy_disk:
"
)
menu1
=
"菜单1"
# Define constants
menu2
=
"菜单2"
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
:
def
take_screenshot
(
self
,
x
,
y
,
w
,
h
):
st
.
subheader
(
f
"
{
menu1
}
"
)
# 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
:
def
generate_qr_code
(
self
):
st
.
subheader
(
f
"
{
menu2
}
"
)
# 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__'
:
def
save_file
(
self
):
main
()
# 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
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录