1.最简单方法是input取图像:这里需要引导用户进图库后直接拍照 mark-zhq[4]
<input type="file" id="browseFile" onchange="" accept="images/*"> <input type="button" id="saveimg" value="保存图片" />
2.调用navigator.getUserMedia方法:手机端--苹果、安卓浏览器、微信浏览器都不支持,本人测的UC浏览器支持。navigator.getUserMedia浏览器支持情况见:http://caniuse.mojijs.com/Home/Html/item/key/stream/index.html
-
<!doctype html> <html> <head> <meta charset="utf-8" /> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> </head> <body> <video id="video" width="640" height="480" autoplay></video> <button id="snap">Snap Photo</button> <canvas id="canvas" width="640" height="480"></canvas> <script type="text/javascript"> window.addEventListener("DOMContentLoaded", function() { var canvas = document.getElementById("canvas"), context = canvas.getContext("2d"), video = document.getElementById("video"), videoObj = { "video": true }, errBack = function(error) { console.log("Video capture error: ", error.code); }; // 多浏览器兼容调用摄像头 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); } else if(navigator.mozGetUserMedia) { // Firefox-prefixed navigator.mozGetUserMedia(videoObj, function(stream){ video.src = window.URL.createObjectURL(stream); video.play(); }, errBack); } }, false); //截取图像 document.getElementById("snap").addEventListener("click", function() { context.drawImage(video, 0, 0, 640, 480); }); </script> </body> </html>
3.微信开发的可以调用微信JS SDK接口
-
wx.chooseImage({ count: 1, // 默认9 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片 } });
4.还有一种是通过jQuery.webcam去做,需要用户安装flash插件,请自行百度---