话不多说,先上代码:
import camera
import time
import network
import socket
WIFI = network.WLAN(network.STA_IF)
'''
camera.framesize()参数:分辨率范围
1:FRAME_96X96 - 96x96 像素
2:FRAME_QQVGA - 160x120 像素 (QQVGA代表Quarter Quarter VGA)
3:FRAME_QCIF - 176x144 像素 (QCIF代表Quarter CIF)
4:FRAME_HQVGA - 240x160 像素 (HQVGA代表Half Quarter VGA)
5:FRAME_240X240 - 240x240 像素
6:FRAME_QVGA - 320x240 像素 (QVGA代表Quarter VGA)
7:FRAME_CIF - 352x288 像素 (CIF代表Common Intermediate Format)
8:FRAME_HVGA - 480x320 像素 (HVGA代表Half VGA)
9:FRAME_VGA - 640x480 像素 (VGA代表Video Graphics Array)
10:FRAME_SVGA - 800x600 像素 (SVGA代表Super VGA)
11:FRAME_XGA - 1024x768 像素 (XGA代表Extended Graphics Array)
12:FRAME_HD - 1280x720 像素 (HD代表High Definition)
13:FRAME_SXGA - 1280x1024 像素 (SXGA代表Super XGA)
14:FRAME_UXGA - 1600x1200 像素 (UXGA代表Ultra XGA)
15:FRAME_FHD - 1920x1080 像素 (FHD代表Full High Definition)
16:FRAME_P_HD - 1600x1200 像素 (P_HD代表Partial High Definition)
17:FRAME_P_3MP - 2048x1536 像素 (P_3MP代表Partial 3 Megapixels)
18:FRAME_QXGA - 2048x1536 像素 (QXGA代表Quad Extended Graphics Array)
19:FRAME_QHD - 2560x1440 像素 (QHD代表Quad High Definition)
20:FRAME_WQXGA - 2560x1600 像素 (WQXGA代表Wide Quad Extended Graphics Array)
21:FRAME_P_FHD - 2560x1600 像素 (P_FHD代表Partial Full High Definition)
'''
if WIFI.isconnected():
print('WIFI已连接!')
while 1:
cam = camera.init() #长时间初始化失败请手动重启
print("cam:",cam)
time.sleep(1.5)
if cam:
camera.framesize(6)
camera.contrast(2) # 增加对比度
camera.speffect(0) # RGB彩色
break
wlan_inf = WIFI.ifconfig()
HOST = wlan_inf[0]
PORT = 8080
print('在浏览器中输入以下地址查看视频:', wlan_inf[0] + f':{PORT}')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(5)
s.setblocking(True)
client, addr = s.accept()
client.settimeout(2)
print ('成功连接到:', addr[0] + ':' + str(addr[1]))
request = client.recv(1024).decode('utf-8')
client.send("HTTP/1.1 200 OK\r\n"
"Server: Tao\r\n"
"Content-Type: multipart/x-mixed-replace;boundary=Tao\r\n"
"Cache-Control: no-cache\r\n"
"Pragma: no-cache\r\n\r\n".encode())
while 1:
img = camera.capture()
if img == False:
print(img)#长时间拍照失败请手动重启
time.sleep(1.5)
else:
break
while True:
img = camera.capture()
header = "--Tao\r\n" \
"Content-Type: image/jpeg\r\n" \
f"Content-Length: {len(img) + len('--Tao\r\n')}\r\n\r\n"
client.send(header.encode())
client.send(img)
else:
WIFI.active(True)
WIFI.connect('HiWiFi_5B4AC2', 'b8507123')
手搓Socket完成服务端,如果长时间拍照失败,需要点击板子上的重启按钮