decoded_qr_codes = pyzbar.decode(gray) for qr_code in decoded_qr_codes: print("QR code data: ", qr_code.data) pub.publish(qr_code.data) 保存解码后的二维码数据到文件 file_path = "/home/shuju/decoded_qr_codes.txt" # 文件路径和名称 with open(file_path, "w") as file: for qr_code in decoded_qr_codes: file.write(qr_code.data.decode("utf-8") + "\n") pub.publish(qr_code.data) 原先解码代码如上,现需要更改为如下新代码,如何更改,给出源码: import cv2 import time rtsp_url = "rtsp://192.168.42.142:8554/live" def write_data_to_file(name, data): with open(name, "wb") as fd: fd.write(data) return len(data) return -1 def image_callback(handler, frmdata, frmsize, width, height, pixfmt): print(f"Image {frmsize}@{frmdata} -- {width}x{height} -- {pixfmt}") if frmdata: t = int(time.time() * 1000) if t - ts > 1000: ts = t name = f"pictures/{width}x{height}-{pixfmt}_{counter}.jpg" if pixfmt == 5: write_data_to_file(name, frmdata) def main(rtsp_url): ts = int(time.time() * 1000) service = cv2.VideoCapture(rtsp_url) counter = 0 while True: ret, frame = service.read() if not ret: break t = int(time.time() * 1000) if t - ts > 1000: ts = t counter += 1 name = f"pictures/{frame.shape[1]}x{frame.shape[0]}-5_{counter}.jpg" cv2.imwrite(name, frame, [int(cv2.IMWRITE_JPEG_QUALITY), 80]) service.release() print("done.") if __name__ == "__main__": main(rtsp_url)
时间: 2024-04-16 21:29:02 浏览: 139
php-qrcode-detector-decoder:这是一个PHP库,用于检测和解码QR码。 这是第一个也是唯一一个没有扩展名的QR码阅读器
以下是将您提供的源代码中的解码二维码和保存数据到文件的部分进行修改后的代码:
```python
import cv2
import time
from pyzbar import pyzbar
rtsp_url = "rtsp://192.168.42.142:8554/live"
def write_data_to_file(name, data):
with open(name, "wb") as fd:
fd.write(data)
return len(data)
return -1
def image_callback(handler, frmdata, frmsize, width, height, pixfmt):
print(f"Image {frmsize}@{frmdata} -- {width}x{height} -- {pixfmt}")
if frmdata:
t = int(time.time() * 1000)
if t - ts > 1000:
ts = t
name = f"pictures/{width}x{height}-{pixfmt}_{counter}.jpg"
if pixfmt == 5:
write_data_to_file(name, frmdata)
# 解码二维码
frame = cv2.imdecode(np.frombuffer(frmdata, dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
decoded_qr_codes = pyzbar.decode(frame)
# 发布二维码数据
for qr_code in decoded_qr_codes:
print("QR code data: ", qr_code.data)
pub.publish(qr_code.data)
# 保存解码后的二维码数据到文件
file_path = "/home/shuju/decoded_qr_codes.txt"
with open(file_path, "w") as file:
for qr_code in decoded_qr_codes:
file.write(qr_code.data.decode("utf-8") + "\n")
def main(rtsp_url):
ts = int(time.time() * 1000)
service = cv2.VideoCapture(rtsp_url)
counter = 0
while True:
ret, frame = service.read()
if not ret:
break
t = int(time.time() * 1000)
if t - ts > 1000:
ts = t
counter += 1
name = f"pictures/{frame.shape[1]}x{frame.shape[0]}-5_{counter}.jpg"
cv2.imwrite(name, frame, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
service.release()
print("done.")
if __name__ == "__main__":
main(rtsp_url)
```
上述代码将解码二维码和保存数据到文件的功能添加到了 `image_callback` 函数中。请注意,您需要确保已安装了 `pyzbar` 库,并根据实际情况修改发布数据和保存文件的相关代码。
阅读全文