How to load the camera in HTML

How to load the camera in HTML

Effect diagram: Overall effect:

Video loading:


Photograph:

Step 1: Create HTML elements

First, we need to create an HTML5 document.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

Then insert the following code inside <body></body> :

<video id="video" width="640" height="480" autoplay></video>
<button id="snap">Screenshot</button>
<canvas id="canvas" width="640" height="480"></canvas>

Step 2: Create the JavaScript

First, create a JavaScript in <head></head> :

<script language="javascript">
	// Grab elements, create settings, etc.
var video = document.getElementById('video');

// Get access to the camera!
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
    // Not adding `{ audio: true }` since we only want video now
    navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) {
        //video.src = window.URL.createObjectURL(stream);
        video.srcObject = stream;
        video.play();
    });
}
/* Legacy code below: getUserMedia 
else if(navigator.getUserMedia) { // Standard
    navigator.getUserMedia({ video: true }, function(stream) {
        video.src = stream;
        video.play();
    }, errBack);
} else if (navigator.webkitGetUserMedia) { // WebKit-prefixed
    navigator.webkitGetUserMedia({ video: true }, function(stream){
        video.src = window.webkitURL.createObjectURL(stream);
        video.play();
    }, errBack);
} else if (navigator.mozGetUserMedia) { // Mozilla-prefixed
    navigator.mozGetUserMedia({ video: true }, function(stream){
        video.srcObject = stream;
        video.play();
    }, errBack);
}
*/
</script>

Then, insert the following code after the HTML element you just created:

<script language="javascript">
	// Elements for taking the snapshot
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var video = document.getElementById('video');

// Trigger photo take
document.getElementById("snap").addEventListener("click", function() {
	context.drawImage(video, 0, 0, 640, 480);
});
</script>

Now, this HTML can complete the functions of opening the camera and taking pictures!

This is the end of this article on how to load the camera in HTML. For more relevant html loading camera content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Front-end implementation of GBK and GB2312 encoding and decoding of strings (summary)

>>:  Parse CSS to extract image theme color function (tips)

Recommend

Explore how an LED can get you started with the Linux kernel

Table of contents Preface LED Trigger Start explo...

MySQL encryption and decryption examples

MySQL encryption and decryption examples Data enc...

Interviewers often ask questions about React's life cycle

React Lifecycle Two pictures to help you understa...

Pure HTML+CSS to achieve typing effect

This article mainly introduces the typing effect ...

WeChat applet tab left and right sliding switch function implementation code

Effect picture: 1. Introduction Your own applet n...

Detailed explanation of the principle and usage of MySQL stored procedures

This article uses examples to explain the princip...

HTTP Status Codes

This status code provides information about the s...

CSS achieves colorful and smart shadow effects

background Ever wondered how to create a shadow e...

Detailed configuration of Nginx supporting both Http and Https

It is almost a standard feature for websites nowa...

Detailed usage of kubernetes object Volume

Overview Volume is the abstraction and virtualiza...

Detailed installation steps for MySQL 8.0.11

This article shares the installation steps of MyS...

How to write CSS elegantly with react

Table of contents 1. Inline styles 2. Use import ...

Ubuntu 16.04 mysql5.7.17 open remote port 3306

Enable remote access to MySQL By default, MySQL u...

Design and implementation of Vue cascading drop-down box

Table of contents 1. Database design 2. Front-end...