diff --git a/face_rec.py b/face_rec.py index 7dbfbfa..4e654b6 100644 --- a/face_rec.py +++ b/face_rec.py @@ -692,9 +692,17 @@ class FaceRecognitionSystem: # 获取二维码图片尺寸 qr_height, qr_width = qr_image.shape[:2] - # 使用屏幕缓存尺寸,如果获取失败则使用图片尺寸 - canvas_width = self.screen_width if self.screen_width > 0 else qr_width - canvas_height = self.screen_height if self.screen_height > 0 else qr_height + # 使用屏幕尺寸,按比例缩放图片 + canvas_width = self.screen_width + canvas_height = self.screen_height + + # 等比例缩放:保持图片宽高比 + scale = min(canvas_width / qr_width, canvas_height / qr_height) + scaled_width = int(qr_width * scale) + scaled_height = int(qr_height * scale) + + # 缩放二维码图片 + qr_image_scaled = cv2.resize(qr_image, (scaled_width, scaled_height), interpolation=cv2.INTER_LINEAR) # 创建二维码窗口 cv2.namedWindow(self.qrcode_window_name, cv2.WINDOW_NORMAL | cv2.WINDOW_GUI_NORMAL) @@ -702,24 +710,19 @@ class FaceRecognitionSystem: x_pos = 0 y_pos = 0 - rendered = self._build_qrcode_instruction_canvas(qr_image, (canvas_width, canvas_height)) + rendered = self._build_qrcode_instruction_canvas(qr_image_scaled, (canvas_width, canvas_height)) if rendered is None: # 回退到原二维码展示 - scale_width = canvas_width / qr_width - scale_height = canvas_height / qr_height - scale = min(scale_width, scale_height) - new_width = int(qr_width * scale) - new_height = int(qr_height * scale) - resized_qr = cv2.resize(qr_image, (new_width, new_height), interpolation=cv2.INTER_LINEAR) + resized_qr = qr_image_scaled # 创建背景画布 rendered = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8) # 计算居中位置 - x_offset = (canvas_width - new_width) // 2 - y_offset = (canvas_height - new_height) // 2 + x_offset = (canvas_width - scaled_width) // 2 + y_offset = (canvas_height - scaled_height) // 2 - rendered[y_offset:y_offset+new_height, x_offset:x_offset+new_width] = resized_qr + rendered[y_offset:y_offset+scaled_height, x_offset:x_offset+scaled_width] = resized_qr cv2.resizeWindow(self.qrcode_window_name, canvas_width, canvas_height) cv2.moveWindow(self.qrcode_window_name, x_pos, y_pos)