上传从网络摄像头拍摄的照片

时间:2013-06-30 18:23:59

标签: javascript html5 forms webcam tornado

如何上传我从网络摄像头拍摄的照片i've found a code that worked (only on Chrome)

我已经使用过这段代码,当我拍摄照片时,我得到了照片,但我没有在源头上看到它?

<video autoplay id="vid" style="display:none;"></video>
<canvas id="canvas" width="640" height="480" style="border:1px solid #d3d3d3;"></canvas><br>
<button onclick="snapshot()">Take Picture</button>

<script type="text/javascript">

var video = document.querySelector("#vid");
var canvas = document.querySelector('#canvas');
var ctx = canvas.getContext('2d');
var localMediaStream = null;

var onCameraFail = function (e) {
    console.log('Camera did not work.', e);
};

function snapshot() {
    if (localMediaStream) {
        ctx.drawImage(video, 0, 0);
    }
}

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia({video:true}, function (stream) {
    video.src = window.URL.createObjectURL(stream);
    localMediaStream = stream;
}, onCameraFail);

</script>

如何将其添加到具有图片元素的表单

<input name="avatar" class="avatar" type="file" required>

如您所见,该字段是必填字段,因此如果将其添加到该字段,表单会将其视为非空字段?

为什么它不能用于Opera?我使用Opera 12.15

更新:i found how to convert to canvas to image,但不知道如何填写图片:

a = canvas.toDataURL("image/jpeg")
document.getElementById("avatar").src = a

如果我使用javascript控制台,我会获得源代码,但如果我验证表单,则表单认为图像仍然缺失且为空白。

任何想法?

1 个答案:

答案 0 :(得分:0)

使用在300毫秒内运行的snapshot()构建SetInterval。

该按钮仅停止间隔,;)


<button onclick="clearInterval(ivl)">Take Picture</button>

var ivl = setInterval(function(){  snapshot(); },300);