-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo2.py
More file actions
278 lines (226 loc) · 8.43 KB
/
Demo2.py
File metadata and controls
278 lines (226 loc) · 8.43 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from PIL import Image, ImageDraw
from pyzbar.pyzbar import decode
import pyautogui
import time
import cv2
import numpy as np
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox, QVBoxLayout, QHBoxLayout, QLabel, QDialog, QLineEdit, QSystemTrayIcon, QMenu
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, QPoint
import qtawesome as qta
import qdarkstyle
import qrcode
import pyperclip
# 全局变量用于鼠标选择
drawing = False
start_point = (-1, -1)
end_point = (-1, -1)
selected = False
img_copy = None
def mouse_callback(event, x, y, flags, param):
global drawing, start_point, end_point, selected, img_copy, img_cv
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
start_point = (x, y)
selected = False
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
end_point = (x, y)
img_copy = img_cv.copy()
cv2.rectangle(img_copy, start_point, end_point, (0, 255, 0), 2)
cv2.imshow("选择区域", img_copy)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
end_point = (x, y)
selected = True
cv2.rectangle(img_copy, start_point, end_point, (0, 255, 0), 2)
cv2.imshow("选择区域", img_copy)
def take_screenshot():
"""
先截取全屏,然后在图片上选择自定义区域
"""
global img_cv, img_copy, selected, drawing, start_point, end_point
# 重置全局变量
drawing = False
start_point = (-1, -1)
end_point = (-1, -1)
selected = False
img_copy = None
screenshot = pyautogui.screenshot()
# 将PIL Image转换为OpenCV格式
img_array = np.array(screenshot)
img_cv = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
img_copy = img_cv.copy()
cv2.namedWindow("选择区域")
cv2.setMouseCallback("选择区域", mouse_callback)
cv2.imshow("选择区域", img_cv)
while True:
cv2.waitKey(1)
if selected:
break
cv2.destroyAllWindows()
if start_point != (-1, -1) and end_point != (-1, -1):
x = min(start_point[0], end_point[0])
y = min(start_point[1], end_point[1])
w = abs(end_point[0] - start_point[0])
h = abs(end_point[1] - start_point[1])
if w > 0 and h > 0:
cropped_img = screenshot.crop((x, y, x + w, y + h))
return cropped_img
else:
print("选择区域无效")
return None
else:
print("未选择区域")
return None
def decode_qr_code(img):
"""
解码二维码图片,返回URL
"""
try:
# 解码二维码
decoded_objects = decode(img)
if decoded_objects:
# 返回第一个解码结果的数据(通常是URL)
return decoded_objects[0].data.decode('utf-8')
else:
return "未检测到二维码"
except Exception as e:
return f"解码失败: {str(e)}"
class CustomTitleBar(QWidget):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.layout = QHBoxLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
self.title = QLabel("二维码工具")
self.title.setStyleSheet("color: white; padding-left: 10px; font-weight: bold;")
self.min_button = QPushButton(qta.icon('fa5s.minus'), "")
self.close_button = QPushButton(qta.icon('fa5s.times'), "")
self.min_button.clicked.connect(self.parent.showMinimized)
self.close_button.clicked.connect(self.parent.close)
self.layout.addWidget(self.title)
self.layout.addStretch()
self.layout.addWidget(self.min_button)
self.layout.addWidget(self.close_button)
self.setLayout(self.layout)
class CustomMessageBox(QDialog):
def __init__(self, title, text):
super().__init__()
self.setWindowTitle(title)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.layout = QVBoxLayout()
self.title_bar = CustomTitleBar(self)
self.layout.addWidget(self.title_bar)
self.text_edit = QLineEdit(text)
self.text_edit.setReadOnly(True)
self.layout.addWidget(self.text_edit)
self.setLayout(self.layout)
self.old_pos = None
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.old_pos = event.globalPos()
def mouseMoveEvent(self, event):
if self.old_pos:
delta = QPoint(event.globalPos() - self.old_pos)
self.move(self.x() + delta.x(), self.y() + delta.y())
self.old_pos = event.globalPos()
def mouseReleaseEvent(self, event):
self.old_pos = None
class QRDecoder(QWidget):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.initUI()
self.initTrayIcon()
self.old_pos = None
def initUI(self):
self.title_bar = CustomTitleBar(self)
self.resize(80, 80) # 设置窗口初始大小
# 初始化位置
screen_geo = QApplication.desktop().availableGeometry()
self.move(screen_geo.width() - self.width() - 120, 80)
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.title_bar)
# 解码按钮
decode_icon = qta.icon('fa5s.qrcode')
decode_button = QPushButton(decode_icon, ' 解码二维码', self)
decode_button.clicked.connect(self.run_program)
layout.addWidget(decode_button)
# 生成按钮
generate_icon = qta.icon('fa5s.barcode')
generate_button = QPushButton(generate_icon, ' 生成二维码', self)
generate_button.clicked.connect(self.generate_qr)
layout.addWidget(generate_button)
self.setLayout(layout)
def initTrayIcon(self):
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon('icon.png'))
tray_menu = QMenu()
# show_action = tray_menu.addAction("显示")
quit_action = tray_menu.addAction("退出")
# show_action.triggered.connect(self.show)
quit_action.triggered.connect(QApplication.instance().quit)
self.tray_icon.setContextMenu(tray_menu)
self.tray_icon.show()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.old_pos = event.globalPos()
def mouseMoveEvent(self, event):
if self.old_pos:
delta = QPoint(event.globalPos() - self.old_pos)
self.move(self.x() + delta.x(), self.y() + delta.y())
self.old_pos = event.globalPos()
def mouseReleaseEvent(self, event):
self.old_pos = None
def run_program(self):
"""
截图并解码
"""
img = take_screenshot()
if img:
url = decode_qr_code(img)
msg = CustomMessageBox('解码结果', url)
msg.exec_()
else:
QMessageBox.warning(self, '错误', '截图失败')
def generate_qr(self):
"""
生成二维码:读取剪贴板内容并展示
"""
try:
content = pyperclip.paste()
if content:
img = qrcode.make(content)
img.show() # 自动展示二维码图片
else:
QMessageBox.warning(self, '错误', '剪贴板为空')
except Exception as e:
QMessageBox.warning(self, '错误', f'生成失败: {str(e)}')
def create_icon():
"""
创建图标文件
"""
try:
img = Image.new('RGB', (64, 64), (20, 20, 20))
draw = ImageDraw.Draw(img)
draw.rectangle([10, 10, 30, 30], fill="white")
draw.rectangle([34, 10, 54, 30], fill="white")
draw.rectangle([10, 34, 30, 54], fill="white")
draw.rectangle([38, 38, 42, 42], fill="white")
draw.rectangle([46, 46, 50, 50], fill="white")
img.save('icon.png')
except Exception as e:
print(f"图标创建失败: {e}")
def main():
create_icon()
app = QApplication(sys.argv)
app.setWindowIcon(QIcon('icon.png'))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
ex = QRDecoder()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()