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

Things You Don’t Know About the CSS ::before and ::after Pseudo-Elements

CSS has two pseudo-classes that are not commonly ...

Ideas and codes for implementing waterfall flow layout in uniapp applet

1. Introduction Is it considered rehashing old st...

Essential conditional query statements for MySQL database

Table of contents 1. Basic grammar 2. Filter by c...

JS implements simple calendar effect

This article shares the specific code of JS to ac...

Linux kernel device driver Linux kernel basic notes summary

1. Linux kernel driver module mechanism Static lo...

Docker container connection implementation steps analysis

Generally speaking, after the container is starte...

Sample code for programmatically processing CSS styles

Benefits of a programmatic approach 1. Global con...

Install Python 3.6 on Linux and avoid pitfalls

Installation of Python 3 1. Install dependent env...

MySQL GTID comprehensive summary

Table of contents 01 Introduction to GTID 02 How ...

Detailed explanation of root directory settings in nginx.conf

There are always some problems when configuring n...

Screen command and usage in Linux

Screen Introduction Screen is a free software dev...

Summary of problems that may occur when using JDBC to connect to Mysql database

First, clarify a few concepts: JDBC: Java databas...

Summary of common knowledge points required for MySQL

Table of contents Primary key constraint Unique p...

Teach you how to implement Vue3 Reactivity

Table of contents Preface start A little thought ...