Getting Webcam input in HTML5 page

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Search for a command to run...

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Hey Mridul! Please tag it with JavaScript for better reach. :) Nice article -- keep them coming.
Any plans to map a custom domain?
Hey Sandeep, added the tag. I got a damn good two letter sub domain.
I will eventually add a custom domain, but I thought it might help people know about hashnode looking at the domain mg.hashnode.dev.
What is gRPC? gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authenti...
Let's assume we have a sample Golang application (in a Go project folder). Contents of main.go package main import "fmt" func main() { fmt.Println("hello world") } In the same folder, run the following commands to initialize go modules # initial...

When you create a new Kubernetes Cluster in EKS - you still need to do several things until you can start using it. This simple set of steps aims to help people create an EKS cluster in simple steps and configure the most essential services like Log ...

To implement a stack in golang we can make use of the slice datatype which golang provides which allows us to modify the size on the fly. If we were doing it in C, we would need to define a max size for the array. Lets start with a simple structure ...

The purpose of this guide is to make a simple telegram bot, which replies with a compliment for each and every message that it receives. Create a bot in telegram Look for a bot named BotFather https://t.me/botfather . This bot is used to create bots...

With HTML5 things have become very easy, we get many platform APIs in javascript which were previously not available. Earlier we made use of Flask with action script to achieve a lot of it.
We will design a simple HTML5 application (webpage) which will get the webcam input and play it in a video tag. To get started, lets make the scaffold HTML elements.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Webcam App</title>
</head>
<body>
<div class="container">
<video autoplay="true" id="videoElement">
</video>
</div>
<div class="container">
<button id="start" onclick="startVideo()">Start</button>
<button id="stop" onclick="stopVideo()">Stop</button>
</div>
<script>
// add code here
</script>
</body>
</html>
We have made a simple HTML page, it has container - which we will use to style the application better. It has a video tag which we will use to display the webcam stream. And it has two buttons start and stop which we will use to start or stop the stream.
Let's add some basic styling to the application. Make a style tag and add the following in it.
.container {
// make it smaller and centered horizontally
margin: 0px auto;
width: 500px;
}
video {
width: 500px;
height: 375px;
background-color: #666;
}
button{
// make buttons look nicer
background: #eee;
color: #333;
border: 0;
padding: 10px;
}
#start{
// make start button a bit different
background-color:#3498db;
color: #fff;
}
Now, we should have a nice looking UI for our webcam stream viewer application -

Next, we add the js code. Specifically, we define the functions startVideo() and stopVideo() which will respectively start and stop the webcam stream. Add the following in the script tag.
var video = document.querySelector("#videoElement");
function startVideo(){
// check if there is a webcam device present
if (navigator.mediaDevices.getUserMedia) {
// try getting the user media stream
navigator.mediaDevices.getUserMedia({ video: true })
.then(function (stream) {
// we got stream, lets set the src to the stream
video.srcObject = stream;
})
.catch(function (e) {
console.log("Something went wrong! " + e);
});
}
}
function stopVideo(){
// get the currently playing tracks and stop all the tracks.
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;
}
When we click on the start button, the browser will ask the user - permission to use the webcam. The application will work only when the user allows the use of the webcam. When the user allows the use, we should see the stream on the video tag.
