def threadCamera(threadName,udpClient):
cap = cv2.VideoCapture(0)
while(True):
#====== 图片获取 ======
#读取原始图
ret,frame=cap.read()
#获取灰度图
# gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#====== 图片保存至本地 ======
#两种图片的保存类型 jpg png
cv2.imwrite('imgTest1.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 50])
# cv2.imwrite('imgTest1.png', frame, [cv2.IMWRITE_PNG_COMPRESSION, 50])
#====== 转换成base64 =====
base64_data=None
with open("imgTest1.jpg","rb") as f:
base64_data = base64.b64encode(f.read())
pass
#====== UDP 发送 =====
if base64_data != None:
sendBytes = getPicMsgHead() + base64_data
sendSize=len(sendBytes)
index=0
while sendSize > 0 :
if sendSize < BUFSIZE :
udpClient.sendBytes(DEFAULT_IP,int(DEFAULT_PORT),sendBytes[index:index+sendSize])
else :
udpClient.sendBytes(DEFAULT_IP,int(DEFAULT_PORT),sendBytes[index:index+ BUFSIZE])
sendSize = sendSize - BUFSIZE
index = index + BUFSIZE
#====== Image对象的两种来源,本地及 opencv mat ======
# 从本地文件获取image对象
# myimg = ImageTk.PhotoImage(file='imgTest1.jpg')
# 从opencv的mat 获取Image对象
#<class 'numpy.ndarray'>
imgData = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
#<class 'PIL.Image.Image'>
imgData2 = Image.fromarray(imgData)
#缩放
imgResized = imgData2.resize((CAMERA_IMG_WIDTH, CAMERA_IMG_HEIGHT), Image.ANTIALIAS)
#<class 'PIL.ImageTk.PhotoImage'>
img = ImageTk.PhotoImage(imgResized)
#====== Label 显示图片 ======
#显示 第一句设置,第二局保持,否则闪烁
mainFrame.lbl_cam.config(image=img, anchor='nw')
mainFrame.lbl_cam.image = img
# mainFrame.lbl_pic.config(image=img, anchor='nw')
# mainFrame.lbl_pic.image = img
pass