Skip to content

Instantly share code, notes, and snippets.

@rpragana
Created March 17, 2018 21:31
Show Gist options
  • Save rpragana/e121667f24c10d05cf75de6d3cbaf375 to your computer and use it in GitHub Desktop.
Save rpragana/e121667f24c10d05cf75de6d3cbaf375 to your computer and use it in GitHub Desktop.
WebRTC
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="stuff, to, help, search, engines, not" name="keywords">
<meta content="What this page is about." name="description">
<meta content="Display Webcam Stream" name="title">
<title>Display Webcam Stream</title>
<style>
#container {
margin: 0px auto;
width: 320px;
height: 240px;
border: 3px dashed red;
float: left;
}
#vidElem {
width: 320px;
height: 240px;
background-color: #888;
}
#canvElem {
margin-left: 30px;
width: 320px;
height: 240px;
background-color: #ccc;
}
</style>
</head>
<body>
<h1>Captura da webcam - WebRTC</h1>
<div id="container">
<video onClick="javascript:fotoclick()"
autoplay="true" controls id="vidElem"></video>
</div>
<canvas id="canvElem"></canvas>
<a id="downLink" href="" download="captura.jpg">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAABgAAAAYADwa0LPAAAAB3RJTUUH4gMQCSE4K62rlAAABUNJREFUWMPFl02IXFUWx3/n3lffXUmTESej2GhHMA5oEswgfgwxBlyNcTbDMIyiICITslM0GxFxMclAslHcBGGYhZvZiaOitm27cWEPSVBwPnAxHTEm3enqdFV1vXrv3ntm8aq6qtJV1RU3ns2r++695/zr/z/33PPgJzaZdOHr55XcKuQKEDdBA4TQcWLAGChWIG1Dugte2T+Z621X/WVeaW2AteA9+BRKVSKXMqWBIoAIcZSj0WrgbK63tlSGlw7LjwPwzgXl2++zf+dTiHIUveeABg4DB4EZVaodL3WBJWBRDPM24pxLiG0EqjB7C/xxn0wO4NS8Ejez4PkiNmlxRJ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+cKcdw5r7cD6TXv9A0UD3Ybk7f6eQBXijXUatUt47xhnRmQgD0TkXTH2WVVdERHeeP6uzbkBVctVqK+BwIoYjhM6HU2HhVJlB9YI9dolwjYgBoKLPa4hrChKWQYZ/FFNadKqTwJiXcScFWNPavArITiKlWnOPHPbeAAApz5T4sb4tnwMiDURWUDMW7l8ac4lLR80UCzv5PTTt25laBT8v15QLl7KqB/1YRI3r1XrtR/QEOoIFwVZRORTY6Nz3iWxtTlUlV2VNq89ec9wibbTcMunmYNShcg7pkKgWLu8hEvi2Eb5RtKqO2NzGBsRvCNfqnDmmZmx/if/OP1KyV3Juqe4lV3hznmuLX+HSxKMMeQKFbxLuLrP87f7Zid1/dPa/wHC3rWnDc40oQAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxOC0wMy0xNlQxMjozNDowNS0wMzowME/VrNMAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTgtMDMtMTZUMTI6MzM6NTYtMDM6MDClXBvvAAAAAElFTkSuQmCC">
</a>
<script>
var video = document.querySelector("#vidElem");
var canvas = document.querySelector("#canvElem");
var download = document.querySelector("#download");
if (navigator.getUserMedia) {
navigator.getUserMedia({video: true}, handleVideo, videoError);
}
function handleVideo(stream) {
video.src = window.URL.createObjectURL(stream);
}
function videoError(e) {
// algo deu errado?
}
navigator.mediaDevices.enumerateDevices().then( function (deviceInfos) {
for (var i = 0; i !== deviceInfos.length; ++i) {
var deviceInfo = deviceInfos[i];
//console.log(JSON.stringify(deviceInfo));
console.log(deviceInfo.deviceId+" # "+deviceInfo.kind+" # "
+deviceInfo.label);
}
})
function fotoclick() {
var context = canvas.getContext('2d');
canvas.width = 320;
canvas.height = 240;
context.drawImage(video, 0, 0, 320, 240);
}
downLink.addEventListener('click', function(ev){
var dataURL = canvas.toDataURL('image/jpeg');
downLink.href = dataURL;
});
</script>
</body>
</html>

Veja como capturar uma stream de video de uma webcam, tirar snapshots (congelar um quadro do vídeo) e salvar em arquivos de imagem, usando a API do WebRTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment