1.最簡單方法是input取圖像:這裏須要引導用戶進圖庫後直接拍照 mark-zhq[4]javascript
<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.htmlhtml
<!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接口java
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插件,請自行百度---jquery