用用LED矩阵屏显示网络摄像头图像矩阵屏显示网络摄像头图像
在现代Web浏览器中,JavaScript公开了一些非常强大的特性。其中之一就是webkitGetUserMedia 功能,它可
以让你访问一个电脑的webcam(在允许的情况下)
在这个例子中,我们将用Espruino板子来将服务器连接一个可以访问Webcam的页面,并发送一个低像素的图片给Espruino
板,然后就可以显示在一个LED矩阵屏上。
你将需要
一个RGB123矩阵 — 我用的是16*16的 一个WIZnetW5500模块 一个带有Webcam的笔记本/平板电脑
连线
连接RGB123如RGB123页所示 连接WIZnetW5500如WIZnet 页所示
软件
第一步是做一个页面,可以获取从webcam来的图像。如下的代码是比较基础的,基本没有错误。
<html>
<body>
<!– The video element that will contain the WebCam image –>
<video autoplay></video>
<!– The canvas that we’ll use to make the WebCam image smaller – 16×16 because that’s the size of the RGB123 matrix –
>
<canvas id=’canvas’ width=’16′ height=’16′></canvas>
<!– The script to handle the processing –>
<script language=’javascript’>
// initialise the WebCam – see https://developer.mozilla.org/en-US/docs/Web/API/Navigator.getUserMedia
if(navigator.webkitGetUserMedia!=null) {
var options = { video:true,audio:false }; navigator.webkitGetUserMedia(options, function(stream) {
var video = document.querySelector(‘video’);
video.src = window.webkitURL.createObjectURL(stream);
}, function(e) { console.log(“error”); }
);
}
// Every 5 seconds…
setInterval(function() {
// find the video and canvas elements
var video = document.querySelector(‘video’);
var canvas = document.getElementById(‘canvas’);
var ctx = canvas.getContext(’2d’);
// resample the WebCam image down to 16×16 pixels ctx.drawImage(video,0,0,16,16);
var data = ctx.getImageData(0,0,16,16);
// Now build a string from the image data. There are better ways,
// but all we do here is for each pixel’s red, green and blue values