qrcode_tool.py 797 B

12345678910111213141516171819202122232425
  1. import cv2
  2. def detect_and_decode_qr_code(img_path):
  3. """
  4. 从指定图片检测和提取二维码
  5. :param img_path: 指定图片位置
  6. :return: (二维码信息,二维码位置)
  7. """
  8. image = cv2.imread(img_path)
  9. # Initialize the QRCode detector
  10. qr_code_detector = cv2.QRCodeDetector()
  11. # Detect and decode the QR code
  12. decoded_text, points, _ = qr_code_detector.detectAndDecode(image)
  13. if points is not None:
  14. # Convert to integer type
  15. points = points[0].astype(int)
  16. # Draw the bounding box on the image (optional)
  17. # for i in range(len(points)):
  18. # cv2.line(image, tuple(points[i]), tuple(points[(i + 1) % len(points)]), (255, 0, 0), 2)
  19. return decoded_text, points
  20. else:
  21. return None, None