How to Integrate Webcam using JavaScript on HTML5 ? - GeeksforGeeks

11 May, 2020
In this article, we will share a very simple JavaScript code snippet through which you can easily integrate your webcam into an HTML5 web page. Now, a day most of the website provides real-time webcam integration for profile picture upload or any account verification step.
First, create an HTML DOM structure using the following code snippet. To integrate webcam with webpage we will use HTML <video> tag. We will use Bootstrap and jQuery to make our web page interactive.
<video id="video" width="100%" height="100%" autoplay></video>
Example: This example uses HTML5, Bootstrap, JavaScript and jQuery to Integrate Webcam with the page.
<!DOCTYPE html><html lang="en">  <head>    <title>        How to Integrate Webcam using        JavaScript on HTML5 ?    </title>    <link rel="stylesheet" href="css/style.css">    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"            rel="stylesheet">      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"     type="text/javascript">      </script>      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js">    </script></head>  <body>    <div class="container">        <div class="row">            <div class="col-sm-12">                <div class="card">                    <h5 class="card-header h5 text-center">                        HTML 5 & JS live Cam                    </h5>                    <div class="card-body">                        <div class="booth">                            <video id="video" width="100%"                                 height="100%" autoplay>                            </video>                        </div>                          <div class="text-right">                            <a href="#!" class="btn btn-danger"                                 onClick="stop()">                                Stop Cam                            </a>                            <a href="#!" class="btn btn-success"                                onClick="start()">                                Start Cam                            </a>                        </div>                    </div>                </div>            </div>        </div>    </div>      <script>        var stop = 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();        });      </script></body>  </html>
Output:
Git source: https://github.com/gauravnewton/html5-js-live-cam
CSS-Misc
HTML-Misc
JavaScript-Misc
jQuery-Misc
CSS
HTML
JavaScript
JQuery
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to create footer to stay at the bottom of a Web page?
Types of CSS (Cascading Style Sheet)
Create a Responsive Navbar using ReactJS
Design a web page using HTML and CSS
How to Upload Image into Database and Display it using PHP ?
How to set the default value for an HTML <select> element ?
How to set input type date in dd-mm-yyyy format using HTML ?
Hide or show elements in HTML using display property
How to Insert Form Data into Database using PHP ?
REST API (Introduction)