MakePhoto = function (id) { this.id = id; var video_element = $("#" + this.id)[0]; var My = this; if (navigator.getUserMedia) { // opera应使用opera.getUserMedianow navigator.getUserMedia({ video: true }, function (stream) { if (window.URL) { video.src = window.URL.createObjectURL(stream); } else { video.src = stream; } video.play(); }, errocb); } else if (navigator.webkitGetUserMedia) { navigator.webkitGetUserMedia({ video: true }, function (stream) { if (window.URL) { video.src = window.URL.createObjectURL(stream); } else { video.src = stream; } video.play(); }, errocb); } else { alert("没有摄像头或不支持"); } function success(stream) { video_element.src = stream; video_element.play(); } function errocb() { } this.PhotoGraph = function (width, height, quality) { //生成比例 var w = width, h = height, scale = w / h; if (width > 0) { w = width || w; h = w / scale; } var canvas = document.createElement("canvas"); //动态创建画布对象 canvas.width = w; canvas.height = h; var ctx = canvas.getContext("2d"); ctx.drawImage(video, 0, 0, w, h); var base64 = null; if (navigator.userAgent.match(/iphone/i)) { //修复IOS var mpImg = new MegaPixImage(img); mpImg.render(canvas, { maxWidth: w, maxHeight: h, quality: quality || 0.8 }); base64 = canvas.toDataURL('image/jpeg', quality || 0.8); } else if (navigator.userAgent.match(/Android/i)) { //修复android var encoder = new JPEGEncoder(); base64 = encoder.encode(ctx.getImageData(0, 0, w, h), quality * 100 || 80); } else { //其他浏览器 base64 = canvas.toDataURL('image/jpeg', quality || 0.8); } // 生成结果 var result = { base64: base64, clearBase64: base64.substr(base64.indexOf(',') + 1), }; return result; } }