Home > Web Front-end > H5 Tutorial > body text

Detailed explanation of examples of implementing the camera function through H5

Y2J
Release: 2018-05-25 09:21:43
Original
5697 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template