Created
April 4, 2021 06:38
-
-
Save senthilmpro/77a6b7a41025a961c59995a75767d37e to your computer and use it in GitHub Desktop.
webcam-access-in-javascript (js-snippets)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://dev.to/stackfindover/how-to-integrate-webcam-using-javascript-3fji | |
var StopWebCam = function () { | |
var stream = video.srcObject; | |
var tracks = stream.getTracks(); | |
for (var i = 0; i < tracks.length; i++) { | |
var track = tracks[i]; | |
track.stop(); | |
} | |
video.srcObject = null; | |
} | |
var start = function () { | |
var video = document.getElementById("video"), | |
vendorURL = window.URL || window.webkitURL; | |
if (navigator.mediaDevices.getUserMedia) { | |
navigator.mediaDevices.getUserMedia({ video: true }) | |
.then(function (stream) { | |
video.srcObject = stream; | |
}).catch(function (error) { | |
console.log("Something went wrong"); | |
}); | |
} | |
} | |
$(function () { start(); }); | |
/* | |
<!-- HTML --> | |
<div class="webcam"> | |
<div class="video-outer"> | |
<video id="video" height="100%" width="100%" autoplay></video> | |
</div> | |
<div class="webcam-start-stop"> | |
<a href="#!" class="btn-start" onclick="start()">Start</a> | |
<a href="#!" class="btn-stop" onclick="StopWebCam()">Stop</a> | |
</div> | |
</div> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment