


Detailed explanation of examples of implementing the camera function through H5
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't created until it'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>
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);
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't created until it'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>
[Related recommendations]
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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.

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

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

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

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

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

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