现在我尝试使用树莓派制作服务器,它可以将实时图像数据发送到浏览器。 服务器端是用Python&Tornado编写的,而客户端是用HTML和javascript编写的。两者都使用WebSocket。(我是javascript的初学者。)
这些是密码
服务器端:
class WSHandler(WebSocketHandler):
def initialize(self, camera):
self.camera = camera
cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_WIDTH, 480)
cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 360)
def open(self):
print("connection opened")
while True:
self.loop()
def loop(self):
img = self.camera.takeImage()
self.write_message(img, binary=True)
class Camera():
def __init__(self):
self.capture = cv.CaptureFromCAM(0)
def takeImage(self):
img = cv.QueryFrame(self.capture)
img = cv.EncodeImage(".jpg", img).tostring()
return img
def main():
camera = Camera()
app = tornado.web.Application([
(r"/camera", WSHandler, dict(camera=camera)),
])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(8080)
IOLoop.instance().start()
if __name__ == "__main__":
main()
客户端:
javascript(客户端.js)
^{pr2}$html格式(索引.html)
<html>
<head>
<title>livecamera</title>
<canvas id="liveCanvas" width="480" height="360"></canvas>
<script type="text/javascript" src="./client.js"></script>
</head>
</html>
当我访问这个'索引.html'在服务器运行时,出现下一个错误。在
Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': No function was found that matched the signature provided.
我想,这是由于服务器发送的数据格式错误造成的。在
我的问题是, 应该使用什么数据格式? 服务器应该如何发送数据? 客户端应该如何接收数据?在
服务器端和以前一样。在
客户端,'二进制类型“”必须设置为“arraybuffer”才能接收blob对象。 它应该由base64和“encode”函数进行编码,这是我在上面写的链接中引用的。在
代码:
javascript
html
我把canvas标签换成了img标签
^{pr2}$相关问题 更多 >
编程相关推荐