<html>
<head>
<style>
/* CSS comes here */
#video {
border: 1px solid black;
width: 320px;
height: 240px;
}
#photo {
border: 1px solid black;
width: 320px;
height: 240px;
}
#canvas {
display: none;
}
.camera {
width: 340px;
display: inline-block;
}
.output {
width: 340px;
display: inline-block;
}
#startbutton {
display: block;
position: relative;
margin-left: auto;
margin-right: auto;
bottom: 36px;
padding: 5px;
background-color: #6a67ce;
border: 1px solid rgba(255, 255, 255, 0.7);
font-size: 14px;
color: rgba(255, 255, 255, 1.0);
cursor: pointer;
}
.contentarea {
font-size: 16px;
font-family: Arial;
text-align: center;
}
</style>
<title>My Favorite Sport</title>
</head>
<body>
<div class="contentarea">
<h1>
Using Javascript to capture Photo
</h1>
<div class="camera">
<video id="video">Video stream not available.</video>
</div>
<div><button id="startbutton">Take photo</button></div>
<canvas id="canvas"></canvas>
<div class="output">
<img id="photo" alt="The screen capture will appear in this box.">
</div>
</div>
<script>
/* JS comes here */
(function() {
var width = 320; // We will scale the photo width to this
var height = 0; // This will be computed based on the input stream
var streaming = false;
var video = null;
var canvas = null;
var photo = null;
var startbutton = null;
function startup() {
video = [Link]('video');
canvas = [Link]('canvas');
photo = [Link]('photo');
startbutton = [Link]('startbutton');
[Link]({
video: true,
audio: false
})
.then(function(stream) {
[Link] = stream;
[Link]();
})
.catch(function(err) {
[Link]("An error occurred: " + err);
});
[Link]('canplay', function(ev) {
if (!streaming) {
height = [Link] / ([Link] / width);
if (isNaN(height)) {
height = width / (4 / 3);
}
[Link]('width', width);
[Link]('height', height);
[Link]('width', width);
[Link]('height', height);
streaming = true;
}
}, false);
[Link]('click', function(ev) {
takepicture();
[Link]();
}, false);
clearphoto();
}
function clearphoto() {
var context = [Link]('2d');
[Link] = "#AAA";
[Link](0, 0, [Link], [Link]);
var data = [Link]('image/png');
[Link]('src', data);
}
function takepicture() {
var context = [Link]('2d');
if (width && height) {
[Link] = width;
[Link] = height;
[Link](video, 0, 0, width, height);
var data = [Link]('image/png');
[Link]('src', data);
} else {
clearphoto();
}
}
[Link]('load', startup, false);
})();
</script>
</body>
</html>