【问题标题】:Capturing profile pic from webcamera从网络摄像头捕获个人资料图片
【发布时间】:2016-02-11 05:52:00
【问题描述】:
【问题讨论】:
标签:
javascript
php
html
webcam
【解决方案1】:
这里我写了一些示例代码检查
<video id="video" width="500" height="500"></video>
<div class="column">
<canvas id="canvas">
</canvas>
</div>
<button id="startbutton">Take photo</button>
<script>
var video = document.getElementById('video'),
canvas = document.getElementById('canvas'),
startbutton = document.getElementById('startbutton');
navigator.getMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
navigator.getMedia({video: !0, audio: !1}, function (stream)
{
if (navigator.mozGetUserMedia)
video.mozSrcObject = stream;
else
{
var vu = window.URL || window.webkitURL;
video.src = vu.createObjectURL(stream);
}
video.play();
}, function (error)
{
if (window.console)
console.error(error);
});
video.addEventListener('canplay', function (ev)
{
if (!streaming)
{
height = video.videoHeight / (video.videoWidth / width);
video.setAttribute('width', width);
video.setAttribute('height', height);
streaming = !0;
}
}, !1);
startbutton.addEventListener('click', function (ev) {
takepicture();
ev.preventDefault();
}, false);
var context;
function takepicture() {
context = canvas.getContext('2d');
var width = 320, height = 240;
if (width && height) {
canvas.width = width;
canvas.height = height;
context.drawImage(video, 0, 0, width, height);
var dataURL = canvas.toDataURL();
//store dataUrl into database
}
}
</script>