Home Web Front-end H5 Tutorial Detailed explanation of examples of implementing the camera function through H5

Detailed explanation of examples of implementing the camera function through H5

May 25, 2018 am 09:21 AM
html5 Photograph

HTML5 Photography First, let’s take a look at the HTML code structure. Of course, this part of the DOM content should be dynamically loaded and generated after the user allows the use of his camera event. Interested friends can learn more Demo address: HTML5 photo taking demo
First, let’s take a look at the HTML code structure. Of course, this part of the DOM content should be dynamically loaded and generated after the user allows the use of their camera event.
Note: We use the resolution of 640X480. If you use JS to dynamically generate it, you can flexibly control the resolution.

The code is as follows:

<!-- 
声明: 此p应该在允许使用webcam,网络摄像头之后动态生成 
宽高: 640 *480,当然,可以动态控制啦! 
--> 
<!-- 
Ide
all
y these elements aren&#39;t created until it&#39;s confirmed that the 
client supports video/camera, but 
for
 the sake of illustrating the 
elements involved, they are created with markup (not 
JavaScript
) 
--> 
<video id="video" 
width
="640" 
height
="480" 
autoplay
></video> 
<button id="snap">Snap Photo</button> 
<
canvas
 id="canvas" width="640" height="480"></canvas>
Copy after login

JavaScript
As long as the above HTML element is created, the JavaScript part will be simpler than you think:

The code is as follows:

// 设置事件监听,DOM内容加载完成,和
jQuery
的$.ready() 效果差不多。 
window.addEvent
List
ener("DOMContentLoaded", function() { 
// canvas 元素将用于抓拍 
var canvas = 
document
.getElementById("canvas"), 
context = canvas.getContext("2d"), 
// video 元素,将用于接收并播放摄像头 的数据流 
video = document.getElementById("video"), 
videoObj = { "video": true }, 
// 一个出错的
回调函数
,在控制台打印出错信息 
errBack = function(error) { 
if
("
object
" === typeof window.console){ 
console.log("Video capture error: ", error.code); 
} 
}; 
// Put video listeners into place 
// 针对标准的浏览器 
if(navigator.getUserMedia) { // Standard 
navigator.getUserMedia(videoObj, function(stream) { 
video.src = stream; 
video.play(); 
}, errBack); 
} 
else
 if(navigator.webkitGetUserMedia) { // WebKit-prefixed 
navigator.webkitGetUserMedia(videoObj, function(stream){ 
video.src = window.webkitURL.createObjectURL(stream); 
video.play(); 
}, errBack); 
} 
// 对拍照
按钮
的事件监听 
document.getElementById("snap").addEventListener("click", function() { 
// 画到画布上 
context.drawImage(video, 0, 0, 640, 480); 
}); 
}, false);
Copy after login

Finally, remember to put your web page under the web server and access it through the http protocol.
In addition, the browser version needs to be newer and support the new features of HTML5.
The translator is not qualified because he did not translate according to the original text. The browser used is chrome 28.
Finally, paste the complete code, which is relatively dull.

The code is as follows:

<!DOCTYPE html> 
<html> 
<head> 
<title> 浏览器webcamera </title> 
<meta name="Generator" content="EditPlus"> 
<meta name="Author" content="renfufei@qq.com"> 
<meta name="Description" content="inveted by: http://davidwalsh.name/browser-camera"> 
<script> 
// 设置事件监听,DOM内容加载完成,和jQuery的$.ready() 效果差不多。 
window.addEventListener("DOMContentLoaded", function() { 
// canvas 元素将用于抓拍 
var canvas = document.getElementById("canvas"), 
context = canvas.getContext("2d"), 
// video 元素,将用于接收并播放摄像头 的数据流 
video = document.getElementById("video"), 
videoObj = { "video": true }, 
// 一个出错的回调函数,在控制台打印出错信息 
errBack = function(error) { 
if("object" === typeof window.console){ 
console.log("Video capture error: ", error.code); 
} 
}; 
// Put video listeners into place 
// 针对标准的浏览器 
if(navigator.getUserMedia) { // Standard 
navigator.getUserMedia(videoObj, function(stream) { 
video.src = stream; 
video.play(); 
}, errBack); 
} else if(navigator.webkitGetUserMedia) { // WebKit-prefixed 
navigator.webkitGetUserMedia(videoObj, function(stream){ 
video.src = window.webkitURL.createObjectURL(stream); 
video.play(); 
}, errBack); 
} 
// 对拍照按钮的事件监听 
document.getElementById("snap").addEventListener("click", function() { 
// 画到画布上 
context.drawImage(video, 0, 0, 640, 480); 
}); 
}, false); 
</script> 
</head> 
<body> 
<p> 
<!-- 
声明: 此p应该在允许使用webcam,网络摄像头之后动态生成 
宽高: 640 *480,当然,可以动态控制啦! 
--> 
<!-- 
Ideally these elements aren&#39;t created until it&#39;s confirmed that the 
client supports video/camera, but for the sake of illustrating the 
elements involved, they are created with markup (not JavaScript) 
--> 
<video id="video" width="640" height="480" autoplay></video> 
<button id="snap">Snap Photo</button> 
<canvas id="canvas" width="640" height="480"></canvas> 
</p> 
</body> 
</html>
Copy after login

[Related recommendations]

1. Html5 free video tutorial

2. HTML5 Detailed explanation of how to implement full-screen playback on WeChat

3. Detailed explanation of browser compatibility issues with H5 new tags

4. Overall overview of how to use H5 production web page

5. The difference between H5 and traditional html

The above is the detailed content of Detailed explanation of examples of implementing the camera function through H5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles